解压错误 操作无法完成.(Zip.ZipError 错误 1.)在 ViewControllers 之间 [英] Unzipping Error The operation couldn’t be completed. (Zip.ZipError error 1.) between ViewControllers

查看:45
本文介绍了解压错误 操作无法完成.(Zip.ZipError 错误 1.)在 ViewControllers 之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I download a .zip file from a URL in ViewControllerA, and put it in the documents directory using:

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

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

When i am trying to retrieve the file from ViewControllerB and unzipping it using:

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 中下载文件并立即解压缩文件,则它工作正常:

But when i am trying it to do it in the same ViewController. i.e. If i am trying to download the file in ViewControllerA and unzip the file right away, it is working fine:

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)")
        }                     
})

我已经证实的事情:

  • 文档目录中存在 Zip 文件.
  • Zip 文件具有有效的文件大小.
  • Zip 文件具有读写权限.

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

What is it that is preventing unzip process between two different ViewControllers?

推荐答案

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

I found the problem. It was not because of two different View Controllers, it was a mistake of the static function. Since Downloader.load() was a static function, it was holding on to the file after downloading. That is why it was unable to parse the Zip file as it was unable to open it.

我将 Downloader 类加载函数更改为非静态:

I changed the Downloader class load function to non-static:

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

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

and it works fine now. Unfortunately the error generated by Zip was not descriptive and did not help me to figure out the problem. I created a simple playground application with two view controllers and studied why Zip worked there and not in my app. This was the only difference.

这篇关于解压错误 操作无法完成.(Zip.ZipError 错误 1.)在 ViewControllers 之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆