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)")
}
createDir
功能是:
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
直接从字符串创建一个而不必将文件保存到数据容器中?
更新
我还尝试了以下方法并得到完全相同的结果:
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)