2016-06-01 12 views
3

私はSSZipArchiveを使用して、インターネットからダウンロードしてDocumentsに保存するファイルの内容を抽出しています。私がダウンロードしているリソースは、zipファイルの形であり、成功したダウンロード時に解凍されます。ファイルの内容を抽出しないでSSZipArchiveを使用してファイルを解凍します

@IBAction func downloadData(sender: UIButton) 
{  if let myAudioDataFromUrl = NSData(contentsOfURL: audioUrl){ 
       // after downloading your data you need to save it to your destination url 
       if myAudioDataFromUrl.writeToURL(destinationUrl, atomically: true) { 
        print("file saved") 
        printTimestamp() 
        let folderName = "Aspire Brochure"//audioUrl.lastPathComponent!.stringByReplacingOccurrencesOfString(".zip", withString: "") 
        let destination = documentsUrl.URLByAppendingPathComponent(folderName) 
        let fileManager = NSFileManager.defaultManager() 
        print("This is the folderName \(destinationUrl)") 
        print("This is the destination \(destination)") 
        let success = fileManager.fileExistsAtPath(destination.absoluteString) as Bool 
        if success == false { 
         do { 
          print("it is here") 
          try! fileManager.createDirectoryAtPath(destination.absoluteString.stringByReplacingOccurrencesOfString("file://", withString: ""), withIntermediateDirectories: true, attributes: nil) 
          print("it hascome here ") 
         } 
        } 
       // let success = fileManager.fileExistsAtPath(destPath) as Bool 
        print("trying to unzip the file") 

        //let fileManager = NSFileManager.defaultManager() 
        let checkFile = NSFileManager.defaultManager().fileExistsAtPath(destination.absoluteString.stringByReplacingOccurrencesOfString("file://", withString: "")) 
        print("this is the check value response \(checkFile)") 
        var tmp = SSZipArchive.unzipFileAtPath(destinationUrl.absoluteString, toDestination: destination.absoluteString) 
        //var tmp = SSZipArchive.unzipFileAtPath(destination.absoluteString.stringByReplacingOccurrencesOfString("file://", withString: ""), toDestination: destination.absoluteString) 
        print("unzipped the file \(tmp)") 

        } else { 
         print("error saving file") 
        } 
       } 
      } 

ファイルは作成されていますが、何も抽出されていません。

+0

目的のパスとダウンロードパスを確認してください。指定されたパスでファイルが見つからない場合、SSZipArchive.unzipFileAtPathはfalseを返します。 – Jush

+0

@Jush、ジップがあり、私はプログラムで目的地のディレクトリを作成しています。 – onkar

答えて

4

今日はこれを理解するのに数時間かかりました。 SSZipArchiveは、zipファイルが見つからない場合はfalseを返します。これはおそらく、あなたが返すURLが[NSURL absoluteString];で、最初にfile://になっているためです。 URLに[NSURL path];と電話するとうまくいくはずです

+0

うわー、そのような単純な修正。ありがとうございました!!もし私ができるなら、これを2度刻むでしょう! – user3069232

+0

うわー、あなたはちょうど私の一日を作った。 – tentmaking

関連する問題