ios - 如何在swift中解压带有密码的json文件?

标签 ios json swift zip

解决方案:

致出现此错误的人:

[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/

相关文章:

swift - 将文本从 Textfield 传递到另一个 ViewController

ios - 如何在不更改导航栏色调的情况下更改底部工具栏色调颜色?

iphone - 如何通过 objective-c 中的编码将pdf文件转换为图像

java - Swagger API 通过 json 验证使用 json

python - flask jsonify : object is not iterable

swift - 在我的 swift 实现中逐个读取字符串 char 非常慢

swift - 对 Collection 扩展函数的调用不明确

ios - 如何使用 Cyber​​ Source SDK iOS 添加金额?

ios - 如何在 Swift 中从数组中的数组中获取字符串

javascript - 从 API 检索 JSON 数据