2017-10-18 108 views
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) 
+0

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

回答

0

你可以使用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中。)