错误:您无权将文件保存在文件夹

时间:2017-08-01 20:21:54

标签: ios swift ssziparchive

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

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

1 个答案:

答案 0 :(得分:6)

感谢这个post我意识到了这一点:

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
    }