8

我已成功下载文件,但无法保存文件。因为我不断收到错误:

[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”.

任何帮助,将不胜感激!

代码

解压文件函数调用

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

1 回答 1

16

感谢这篇文章,我意识到这是因为这条线:

let destString = destPath.absoluteString

我不得不将其更改为:

let destString = documentsURL.relativePath

这让我大大简化了我的功能:

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
    }
于 2017-08-02T01:40:03.870 回答