1
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)
'zipArch.write(dataStr.data(using:String.Encoding.utf8)!, filename:「blah.txt」,withPassword:nil)''就是你'重新尋找。你使用的是最新版本的SSZipArchive?您可能想要將錯誤請求提交到https://github.com/ZipArchive/ZipArchive/issues –