如何读取swift
我有一个由gZip制成的 .zip 文件,我需要阅读 .zip 文件的内容而不解压缩该文件
let zipData = NSData(contentsOfFile: zipfilePath) as Data?
不使用第三方库
答案 0 :(得分:1)
您可以使用Swift本机框架,该框架允许您创建,读取和更新ZIP存档文件:ZIP Foundation。
解压缩文件基本上只是一行:
try fileManager.unzipItem(at: sourceURL, to: destinationURL)
具有某些上下文的完整示例如下所示:
let fileManager = FileManager()
let currentWorkingPath = fileManager.currentDirectoryPath
var sourceURL = URL(fileURLWithPath: currentWorkingPath)
sourceURL.appendPathComponent("archive.zip")
var destinationURL = URL(fileURLWithPath: currentWorkingPath)
destinationURL.appendPathComponent("directory")
do {
try fileManager.createDirectory(at: destinationURL, withIntermediateDirectories: true, attributes: nil)
try fileManager.unzipItem(at: sourceURL, to: destinationURL)
} catch {
print("Extraction of ZIP archive failed with error:\(error)")
}
您可以检出GitHub存储库:ZIPFoundation
ZIPFoundation博客:ZIPFoundation
答案 1 :(得分:0)
".gz file made from gZip"
import Gzip
let ZipFile = Bundle.main.path(forResource: "Your-File-Name", ofType: "gz")
print(ZipFile as Any)
let Data = NSData(contentsOfFile: ZipFile!) as Data?
let decompressedData: Data
if Data!.isGzipped {
decompressedData = try! Data!.gunzipped()
} else {
decompressedData = Data!
}
var Decompress_String = ""
if let string = String(bytes: decompressedData, encoding: .utf8) {
Decompress_String = string
} else {
print("not a valid UTF-8 sequence")
}
print("Decompress_String")