解决方案:
致出现此错误的人:
[SSZipArchive] Failed to open file on unzipping.(Error Domain=NSPOSIXErrorDomain Code=20 "Not a directory")
使用 unzipFile 函数时,请确保目标是目录。
SSZipArchive.unzipFile(atPath: String, toDestination: String, overwrite: Bool, password: String?)
问题:
如何在 Swift 中解压带密码的 json 文件?
我使用第三方SDK下载了一个文件,文件类型为Data
。
该文件被声明为zip文件,需要解压为带有密码的json
文件。
我将文件保存到 FileManager.documentDirectory
并使用 .zip
扩展名命名。
然后我尝试使用 ZipArchive
解压缩该文件,但出现以下错误:
[SSZipArchive] Failed to open file on unzipping.(Error Domain=NSPOSIXErrorDomain Code=20 "Not a directory")
我还保存了带有 .json
扩展名的文件,然后将其解压缩,但得到了与上面相同的错误。
我将保存的文件下载到容器中并打开,结果如下:
(1) 到 .zip
文件:
我无法打开它。
Can't decompressed xxx.json.zip to Documents.
(error -2: No similar file or directory)
(2) 到 .json
文件:
我收到一些乱码,例如
∫ôFk;|T|D˚®è•Éjê‹¡Ò;X≈∞)˘–7ÑØZl≥MÇz-n·ù!ê}˜fflêÔæv\£F>RBZ1Myfª—Q˘∏àúˇá≥˙Ïèãd”
这里显示我的代码。
func saveZipFile(filename: String, file: Data) {
let documentDirURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = documentDirURL.appendingPathComponent("\(filename).zip")
print("File Path: \(fileURL.path)")
do {
try file.write(to: fileURL, options: .atomic)
try SSZipArchive.unzipFile(atPath: fileURL.path, toDestination: fileURL.path, overwrite: true, password: finalKey)
} catch let err {
print("Error: \(err.localizedDescription)")
}
}
如果正确解压,该文件应该是人类可读的 json 文件。
最佳答案
试试这个:
func saveZipFile(filename: String, file: Data) {
let documentDirURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = documentDirURL.appendingPathComponent("\(filename).zip")
let destination = documentDirURL.path + "/MyZipFiles"
print("File Path: \(fileURL.path)")
do {
try file.write(to: fileURL, options: .atomic)
try SSZipArchive.unzipFile(atPath: fileURL.path, toDestination: destination, overwrite: true, password: finalKey)
} catch let err {
print("Error: \(err.localizedDescription)")
}
}
关于ios - 如何在swift中解压带有密码的json文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58764052/