解压缩Zip文件后,如何显示其内容

时间:2019-07-09 13:00:10

标签: ios swift zipfile

在我的应用中,我首先下载一个文件,这是一个zip文件。当我尝试将其解压缩时。它显示了一个错误。

我使用此Cocoapod SSZipArchive实现了解压缩过程

首先,我获得下载的文件URL,并将该URL传递给此解压缩功能

Here is url for downloading a file
let urls = "https://insermcc.org/media/assignments/easy-dl-dev-0.2.0.zip"

    let destination: DownloadRequest.DownloadFileDestination = { _, _ in
        let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let fileURL = documentsURL.appendingPathComponent("pig.png")

        return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
    }

    Alamofire.download(urls, to: destination).response { response in

        if response.error == nil, let imagePath = response.destinationURL?.path {
            let image = UIImage(contentsOfFile: imagePath)
            print("IMAGEPATH ==  \(response.destinationURL))")
        }
    }

Here is for Unzipping that downloaded File which is
file:///var/mobile/Containers/Data/Application/96B1F9C8-E9C6-4A2B-89E3-1F28B12AC6EA/Documents/pig.png

函数是

func unzip() {

        let durl = dURL as! URL

        let stringURL = String(describing: dURL)

        guard let unzipPath = tempUnzipPath() else {
            return
        }
        print("Unzip path:", unzipPath)
        let success: Bool = SSZipArchive.unzipFile(atPath: dURL.path,
                                                   toDestination: unzipPath,
                                                   preserveAttributes: true,
                                                   overwrite: true,
                                                   nestedZipLevel: 1,
                                                   password: nil,
                                                   error: nil,
                                                   delegate: nil,
                                                   progressHandler: nil,
                                                   completionHandler: nil)

        if success != false {
            print("Success unzip")
        } else {
            print("No success unzip")
            return
        }

 func tempUnzipPath() -> String? {
    var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    print("tempUnzipPath() -> String? *********   \(path)")
    path += "/\(UUID().uuidString)"
    let url = URL(fileURLWithPath: path)

    do {
        try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
    } catch {
        return nil
    }
    return url.path
}

我需要显示zip文件的内容。 那是从easy-dl-dev-0.2.0.zip到 像这样

easy-dl-dev-0.2.0.zip
    easy-dl-dev-0.2.0
      -  Package.swift
      -  README.md

0 个答案:

没有答案