Swift - 下载了 zip 文件,解压缩后创建 .cpgz 文件



从 Dropbox 或 Google 云端硬盘网址下载 zip 文件会导致 zip 文件被解压缩到 cpgz 文件中。使用以下代码完成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 代码中修复它

分享解决我问题的链接。我也得到了zip文件的mime类型text/html。更改保管箱网址解决了我的查询。

如何将 Dropbox 共享链接(针对 zip 文件(下载到本地

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 类型是应用程序/二进制。它解决了提取zip文件时.zip到.cpgz循环的问题。

首先,通过添加以下代码来确保您的服务器是否返回 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)

最新更新