1

我正在使用SSZipArchive从 URL ( http://www.colorado.edu/conflict/peace/download/peace.zip ) 解压缩文件。为此,我正在使用以下功能:

SSZipArchive.unzipFile(atPath: path, toDestination: documentsPath, progressHandler: {  
  1. 是否可以使用从 Url 提取文件SSZipArchive
  2. 是的,我如何在atPath中传递 Url ?
4

1 回答 1

0

如果不从 URL 下载文件,则无法直接解压缩。
我就是这样做的,

func downLoad() {
    let url : String = "http://www.colorado.edu/conflict/peace/download/peace.zip"
    var localPath: NSURL?
    Alamofire.download(.GET,url,
        destination: { (temporaryURL, response) in
            let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
            let pathComponent = response.suggestedFilename
            localPath = directoryURL.URLByAppendingPathComponent(pathComponent!)
            return localPath!
    })
        .response { (request, response, _, error) in
            SwiftEventBus.post("DownloadedSuccessfully", sender: localPath!)
    }  
}

下载内容后,将其解压缩到您想要的位置。

SwiftEventBus.onMainThread(self, name:"DownloadedSuccessfully")
    {
        result in

        let resultStatus = result.object as! NSURL

        guard let zipPath: String = resultStatus.path! as String else {
            return
        }
        guard let unZipPath = unzipPath() else {
            return
        }

        let success = SSZipArchive.unzipFileAtPath(zipPath, toDestination: unZipPath)
        print(unZipPath)
        if !success {
            return
        }
    }

我解压缩文件并将其存储在文档目录中

func unzipPath() -> String? {
    let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
    let url = NSURL(fileURLWithPath: path)
    do {
        try NSFileManager.defaultManager().createDirectoryAtURL(url, withIntermediateDirectories: true, attributes: nil)
    } catch {
        return nil
    }
    if let path = url.path {
        return path
    }
    return nil
}

从那里您可以访问文件的数据。这里我使用了第三方框架 Alamofire 和 swiftEventBus。希望这对你有用。

于 2017-05-17T11:52:57.327 回答