2016-06-01 2 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

와우, 그런 간단한 수정; 고맙습니다!! 만약 내가 할 수 있으면 이것을 두 번 똑딱 거릴 것이다! – user3069232

+0

와우, 방금 날 만들었 어. – tentmaking

관련 문제