Error: You don’t have permission to save the file in the folder

14wml picture 14wml · Aug 1, 2017 · Viewed 12.4k times · Source

I've successfully downloaded the file but I am unable to save the file. Because I keep getting the error(s):

[SSZipArchive] Error: You don’t have permission to save the file “fileName” in the folder “Folder_Name”.
[SSZipArchive] Error: You don’t have permission to save the file “fileName” in the folder “__MACOSX”.

Any help would be appreciated!

Code

Unzip file function call

 ZipManager.unzipFile(atPath: filePath, delegate: self)

ZipManager.swift

private static let documentsURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

static func unzipFile(atPath path: String, delegate: SSZipArchiveDelegate)
    {
        let destFolder = "/Folder_Name"
        let destPath = documentsURL.appendingPathComponent(destFolder, isDirectory: true)
        let destString = destPath.absoluteString
        
        if ( !FileManager.default.fileExists(atPath: destString) )
        {
            try! FileManager.default.createDirectory(at: destPath, withIntermediateDirectories: true, attributes: nil)
        }
        
        SSZipArchive.unzipFile(atPath: path, toDestination: destString, delegate: delegate)
    }

Answer

14wml picture 14wml · Aug 2, 2017

Thanks to this post I realized its because of this line:

let destString = destPath.absoluteString

I had to change it to:

let destString = documentsURL.relativePath

Which allowed me to greatly simplify my function:

static func unzipFile(atPath path: String) -> Bool
    {
        let destString = documentsURL.relativePath

        let success: Void? = try? SSZipArchive.unzipFile(atPath: path, toDestination: documentsURL.relativePath, overwrite: true, password: nil)

        if success == nil
        {
            return false
        }

        return true
    }