macos - 如何在 swift 4 和 Mac OS App 的可可中将数据(类型数据)转换为 zip 文件(类型文件)?
问题描述
我正在开发一个 mac os 应用程序,我必须将数据从服务器 API 转换为 zip 文件。API 以 Data 类型的编码格式返回一个 zip 文件本身(我正在从服务器获取一个 zip 文件),但我想将该数据转换为一个 zip 文件并希望存储在磁盘中。
我的功能:
func DownloadExamZip(){
let request = NSMutableURLRequest(url: NSURL(string: "http://localhost:5000/api/DownloadExamZip/EX0000018/ST000000195/874059")! as URL)
request.httpMethod = "GET"
let AuthorizationToken = "Hidden"
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.setValue(AuthorizationToken, forHTTPHeaderField: "Authorization")
let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
do {
guard data != nil else {
print("data is nil")
return
}
//Here i want to convert the type data to a zip file
}
catch {
print("Error -> \(error)")
}
}
task.resume()
}
谁能帮我把这些数据转换成一个 zip 文件。我还必须将该文件存储在磁盘中。
解决方案
您可以使用write(to:options:)
方法将数据写入特定 URL 的文件
let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("yourfile.zip")
do {
try data.write(to: fileURL, options: .atomic)
} catch {
print(error)
}
推荐阅读
- php - 使用 cookie 登录且没有 php 会话时重新登录?
- python-3.x - 基于逗号将值 0 添加到现有列
- git - 避免将 png 图像添加到 git
- c# - VS Code IntelliSense 在 C# 9.0 上失败
- debugging - 开始调试 xslt 应用程序。.net 核心
- python-3.x - 如何从列表中找到单词的所有组合
- javascript - SFU 和客户端机器之间存在什么类型的连接?
- javascript - 如何从两个firestore集合中获取数据,合并并显示它
- swift - 为什么用户单击通知时 userInfo 不存在?
- php - 管理员中最昂贵的 woocomerce 产品的价格