Unzip the zip file saved into the app in xcode with swift 5.0

4.8k views Asked by At

I have saved one zip file in my app, which is having .json files in it. I have to unzip this zip file at some path and read the file after unzip.

I tried using package managers like ZipFoundation

If I run the app on my mac with OS 10.15.2 (macOS Catalina) I get the following output

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

if I run the app on device (iPhone XS Max iOS 13.3.1) I get the following error,

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

This is my code

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

Can you please help me out to get out from this issue. I need to unzip the zip file.

for sourceURL I tried both, by giving currentWorkingPath and by giving actual path to file. but both the time i get same error.

@note :- I don't want to use any pods or Carthage. I want default code or external dependancies are from package managers only.

Any help will be appreciated. Thank you.

1

There are 1 answers

0
Sagar On

All Thanks to @vadian I used URL(fileURLWithPath: currentWorkingPath)

Here is my sample code which is working well now,

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