解压缩错误操作无法完成。 (Zip.ZipError 错误 1.) ViewControllers 之间

Unzipping Error The operation couldn’t be completed. (Zip.ZipError error 1.) between ViewControllers

我从 ViewControllerA 中的 URL 下载了一个 .zip 文件,并使用以下命令将其放入文档目录:

let documentsUrl:URL =  (FileManager.default.urls(for: .documentDirectory, in: .allDomainsMask).first as URL?)!
let destinationFileUrl = documentsUrl.appendingPathComponent("zipFile.zip")

当我尝试从 ViewControllerB 检索文件并使用以下方法解压缩时:

let documentsUrl:URL =  (FileManager.default.urls(for: .documentDirectory, in: .allDomainsMask).first as URL?)!
let destinationFileUrl = documentsUrl.appendingPathComponent("zipFile.zip")
do{
     let file = try Zip.quickUnzipFile(destinationFileUrl)
}catch {
     print("Error: \(error.localizedDescription)")
}

给我一个错误:

Error: The operation couldn’t be completed. (Zip.ZipError error 1.)

但是当我尝试在同一个 ViewController 中进行操作时。即,如果我尝试下载 ViewControllerA 中的文件并立即解压缩文件,它工作正常:

let documentsUrl:URL =  (FileManager.default.urls(for: .documentDirectory, in: .allDomainsMask).first as URL?)!
let destinationFileUrl = documentsUrl.appendingPathComponent("zipFile.zip")

Downloader.load(url: remoteURL, to: destinationFileUrl, completion: {
        print("Downloaded.")
        do{
            let file = try Zip.quickUnzipFile(destinationFileUrl)
        }catch {
            print("Error: \(error.localizedDescription)")
        }                     
})

我已经验证的事情:

是什么阻止了两个不同 ViewController 之间的解压缩过程?

首先解压操作不要放在VC.In其实跟VC.

没有半毛钱的关系

你可以在代码中做一些你可以正常运行的修改,然后制作方法public,然后调用这个方法在你需要的任何地方获取数据。

Downloader.load(url: remoteURL, to: destinationFileUrl, completion: {
        print("Downloaded.")
        do{
            let file = try Zip.quickUnzipFile(destinationFileUrl)
            completion(file)
        }catch {
            print("Error: \(error.localizedDescription)")
        }                     
})

我发现了问题。不是因为两个不同的View Controller,而是静态函数的错误。由于 Downloader.load() 是一个静态函数,它在下载后保留文件。这就是它无法解析 Zip 文件的原因,因为它无法打开它。

我将下载器 class 加载函数更改为非静态:

let downloader: Downloader = Downloader()
downloader.load()

现在可以正常使用了。不幸的是,Zip 生成的错误不是描述性的,也没有帮助我找出问题所在。我创建了一个带有两个视图控制器的简单游乐场应用程序,并研究了为什么 Zip 在那里工作而不是在我的应用程序中工作。这是唯一的区别。