首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用swift 5.0在xcode中解压保存到应用程序的zip文件

使用swift 5.0在xcode中解压保存到应用程序的zip文件
EN

Stack Overflow用户
提问于 2020-04-17 15:18:11
回答 1查看 2K关注 0票数 1

我已经在我的应用程序中保存了一个压缩文件,里面有.json文件。我必须在某个路径解压这个zip文件,并在解压后读取该文件。

我尝试使用像ZipFoundation这样的包管理器

如果我在我的macOS 10.15.2 (macOS Catalina)上运行这个应用程序,我会得到以下输出

代码语言:javascript
复制
"Extraction of ZIP archive failed with error:Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “directory” in the folder “Macintosh HD”." UserInfo={NSFilePath=/directory, NSUnderlyingError=0x600002f39530 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}"

如果我在设备上运行应用程序(iPhone XS Max iOS 13.3.1),我会得到以下错误,

代码语言:javascript
复制
Extraction of ZIP archive failed with error:Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “directory” in the folder “System@snap-8290536”." UserInfo={NSFilePath=/directory, NSUnderlyingError=0x2811d54d0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

这是我的代码

代码语言:javascript
复制
import ZIPFoundation


func unarchiveFile(){
    let fileManager = FileManager()
    let currentWorkingPath = fileManager.currentDirectoryPath
    var sourceURL = URL(fileURLWithPath: "/Users/sagarchavan/Documents/Project/Swift-POC/iOS-CD-POCs/CDSwiftDemo/")
    //var sourceURL = URL(fileURLWithPath:currentWorkingPath)
    sourceURL.appendPathComponent("countryList.zip")
    print("source url is :- :\(sourceURL)")
    var destinationURL = URL(fileURLWithPath: currentWorkingPath)
    destinationURL.appendPathComponent("directory")
    print("destinationURL is :- :\(destinationURL)")
    do {
        try fileManager.createDirectory(at: destinationURL, withIntermediateDirectories: true, attributes: nil)
        try fileManager.unzipItem(at: sourceURL, to: destinationURL)
    } catch {
        print("Extraction of ZIP archive failed with error:\(error)")
    }
}

你能帮我摆脱这个问题吗?我需要解压缩这个zip文件。

对于sourceURL,我通过给出currentWorkingPath和给出文件的实际路径,尝试了这两种方法。但这两次我都得到了相同的错误。

@备注:-我不想使用任何豆荚或迦太基。我希望默认代码或外部依赖只来自包管理器。

任何帮助都将不胜感激。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-04-17 19:29:33

感谢@vadian我使用了URL(fileURLWithPath: currentWorkingPath)

下面是我的示例代码,它现在运行得很好。

代码语言:javascript
复制
func unarchiveFile(){
        let fileManager = FileManager()
        let currentWorkingPath = "/Users/sagarchavan/Documents/Project/Swift-POC/iOS-CD-POCs/CDSwiftDemo/CDSwiftDemo/Zipfile/"
        var sourceURL = URL(fileURLWithPath:currentWorkingPath)
        sourceURL.appendPathComponent("countryList.zip")
        var destinationURL = URL(fileURLWithPath: currentWorkingPath)
        destinationURL.appendPathComponent("unzipData")
        do {
            try fileManager.createDirectory(at: destinationURL, withIntermediateDirectories: true, attributes: nil)
            try fileManager.unzipItem(at: sourceURL, to: destinationURL)
           parseJSONFromFile(destinationPath: destinationURL)
        } catch {
            print("Extraction of ZIP archive failed with error:\(error)")
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61266011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档