0

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

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

It is giving me an error:

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

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

Things i have verified:

  • Zip file exists in the document directory.
  • Zip file has valid file size.
  • Zip file has read and write permissions.

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

Usama Aftab
  • 47
  • 1
  • 9
  • Unrelated but you can remove the `as URL?` cast. `first` already returns a `URL?`. And consider using `userDomainMask` instead of `allDomainsMask`. – rmaddy May 31 '18 at 22:29
  • I can make the change for URL. I first tried using userDomainMask, but it doesn't solve the problem. – Usama Aftab May 31 '18 at 22:50

2 Answers2

0

First, the unzip operation should not be placed in the VC.In fact, it doesn't have a half - money relationship with VC.

You can make some changes in the code that you can run normally and make the method public, and then call this method to get data anywhere you need.

Downloader.load(url: remoteURL, to: destinationFileUrl, completion: {
        print("Downloaded.")
        do{
            let file = try Zip.quickUnzipFile(destinationFileUrl)
            completion(file)
        }catch {
            print("Error: \(error.localizedDescription)")
        }                     
})
J.Doe
  • 86
  • 1
  • 5
  • I need to separate these two processes of Downloading and unzipping. I already have a manager class where i am performing this operation which is called by VC A. – Usama Aftab Jun 04 '18 at 16:09
0

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.

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

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

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.

Usama Aftab
  • 47
  • 1
  • 9