Swift - 提取后下载的zip文件创建.cpgz文件。

时间:2018-05-29 10:35:08

标签: ios swift

从Dropbox或Google驱动器URL下载zip文件会生成一个解压缩为cpgz文件的zip文件。使用下面的代码来完成zip文件的下载。我想知道是否有任何特定的方式来下载任何zip文件,以便它不会导致cpgz循环。

if let zipUrl = URL(string: "https://www.dropbox.com/s/n785nwy2tbaxgz8/app_dfu_package_1.zip?dl=0") {
        // create your document folder url
        let documentsUrl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        // your destination file url
        let destination = documentsUrl.appendingPathComponent(zipUrl.lastPathComponent)
        //print(destination)

        var urlRequest = URLRequest.init(url: zipUrl)
        urlRequest.httpMethod = "get"
        urlRequest.setValue("application/json", forHTTPHeaderField: "content-Type")

        // check if it exists before downloading it
        if FileManager().fileExists(atPath: destination.path) {
            print("The file already exists at path")
        } else {
            //  if the file doesn't exist just download the data from your url
            URLSession.shared.downloadTask(with: urlRequest, completionHandler: { (location, response, error) in
                // after downloading your data you need to save it to your destination url
                guard
                    let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
                    let location = location, error == nil
                    else { return }
                do {
                    try FileManager.default.moveItem(at: location, to: destination)
                    print("file saved")
                } catch {
                    print(error)
                }
            }).resume()
        }
    }

我们如何在Swift代码中修复它

2 个答案:

答案 0 :(得分:0)

分享解决了我的问题的链接。我也得到了zip文件的mime type text / html。更改Dropbox网址解决了我的问题。

  

How to download a Dropbox share linkage (aimed at a zip file) to local

if let zipUrl = URL(string: "https://www.dropbox.com/s/n785nwy2tbaxgz8/app_dfu_package_1.zip?dl=1") {
        // create your document folder url
        let documentsUrl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        // your destination file url
        let destination = documentsUrl.appendingPathComponent(zipUrl.lastPathComponent)
        //print(destination)

        var urlRequest = URLRequest.init(url: zipUrl)
        urlRequest.httpMethod = "get"
        urlRequest.setValue("application/json", forHTTPHeaderField: "content-Type")

        // check if it exists before downloading it
        if FileManager().fileExists(atPath: destination.path) {
            print("The file already exists at path")
        } else {
            //  if the file doesn't exist just download the data from your url
            URLSession.shared.downloadTask(with: urlRequest, completionHandler: { (location, response, error) in
                // after downloading your data you need to save it to your destination url
                guard
                    let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
                    let mimeType = response?.mimeType,
                    let location = location, error == nil
                    else { return }
                do {
                    try FileManager.default.moveItem(at: location, to: destination)
                    print("file saved")
                    print("Mime Type:- \(mimeType)")
                } catch {
                    print(error.localizedDescription)
                }
            }).resume()
        }
    }

进行URL更改(dl = 0到dl = 1)后,我得到的mime类型是application / binary。它在解压缩zip文件时解决了.zip到.cpgz循环。

答案 1 :(得分:0)

首先,通过添加以下代码,确保服务器本身是否返回了zip文件:

let response = response as! HTTPURLResponse  
guard (200...299).contains(response.statusCode) else {  
    … deal with the server-side error …  
}  
guard response.mimeType == "application/zip" else {  
    … deal with the bogus `Content-Type` …  
}

如果它是一个zip文件,它将向前执行,您必须执行以下操作将该文件保存在磁盘中:

let destinationDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
            let destination = destinationDir.appendingPathComponent("SomeFileName.zip")
            try data!.write(to: destination)
相关问题