创建从一个字符串中迅速
问题描述:
let data = "InPractiseThisWillBeAReheallyLongString"
createDir()
let docsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let ourDir = docsDir.appendingPathComponent("ourCustomDir/")
let tempDir = ourDir.appendingPathComponent("temp/")
let unzippedDir = tempDir.appendingPathComponent("unzippedDir/")
let unzippedfileDir = unzippedDir.appendingPathComponent("unZipped.txt")
let zippedDir = tempDir.appendingPathComponent("Zipped.zip")
do {
try data.write(to: unzippedfileDir, atomically: false, encoding: .utf8)
let x = SSZipArchive.createZipFile(atPath: zippedDir.path, withContentsOfDirectory: unzippedfileDir.path)
var zipData: NSData! = NSData()
do {
zipData = try NSData(contentsOfFile: unzippedfileDir.path, options: NSData.ReadingOptions.mappedIfSafe)
//once I get a readable .zip file, I will be using this zipData in a multipart webservice
}
catch let err as NSError {
print("err 1 here is :\(err.localizedDescription)")
}
}
catch let err as NSError {
print("err 3 here is :\(err.localizedDescription)")
}
一个zip文件和createDir FUNC是:创建从一个字符串中迅速
func createDir(){
let docsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let ourDir = docsDir.appendingPathComponent("ourCustomDir/")
let tempDir = ourDir.appendingPathComponent("temp/")
let unzippedDir = tempDir.appendingPathComponent("unzippedDir/")
let fileManager = FileManager.default
if fileManager.fileExists(atPath: tempDir.path) {
deleteFile(path: tempDir)
deleteFile(path: unzippedDir)
} else {
print("file does not exist")
do {
try FileManager.default.createDirectory(atPath: tempDir.path, withIntermediateDirectories: true, attributes: nil)
try FileManager.default.createDirectory(atPath: unzippedDir.path, withIntermediateDirectories: true, attributes: nil)
print("creating dir \(tempDir)")
} catch let error as NSError {
print("here : " + error.localizedDescription)
}
}
}
现在我没有得到任何错误,但是当我下载我的AppData的容器,得到的zip文件,并尝试解压,我马告诉zip文件是空的。我可以看到unzipped.text文件确实存在。
任何想法我做错了什么?
是否有一种方法可以直接从字符串创建.zip而不必将文件保存到数据容器?
UPDATE 我也试过以下,并有相同的结果:
let zipArch = SSZipArchive(path: zippedDir.path)
print(zipArch.open)
print(zipArch.write(dataStr.data(using: String.Encoding.utf8)!, filename: "blah.txt", withPassword: ""))
print(zipArch.close)
答
你可以使用ZIPFoundation,这是另一个斯威夫特ZIP库,可以读取,创建和修改ZIP文件。它的优点之一是它允许您“即时”添加ZIP条目。在创建存档之前,您不必将字符串写入磁盘。它提供了一个基于API封在那里你可以直接喂串入一个新创建的档案:
func zipString() {
let string = "InPractiseThisWillBeAReheallyLongString"
var archiveURL = URL(fileURLWithPath: NSTemporaryDirectory())
archiveURL.appendPathComponent(ProcessInfo.processInfo.globallyUniqueString)
archiveURL.appendPathExtension("zip")
guard let data = string.data(using: .utf8) else { return }
guard let archive = Archive(url: archiveURL, accessMode: .create) else { return }
try? archive.addEntry(with: "unZipped.txt", type: .file, uncompressedSize: UInt32(data.count), provider: { (position, size) -> Data in
return data
})
}
的addEntry
方法也可以用来进行分块的加法(让你不可选bufferSize
参数必须将整个数据对象加载到RAM中。)
'zipArch.write(dataStr.data(using:String.Encoding.utf8)!, filename:“blah.txt”,withPassword:nil)''就是你'重新寻找。你使用的是最新版本的SSZipArchive?您可能想要将错误请求提交到https://github.com/ZipArchive/ZipArchive/issues –