1

I download a zip file with Alamofire4 and unzip it with SSZipArchive but the unzip does not work. I am not sure if the path of the downloaded file is good with Alamofire.

Here is the code:

let destination: DownloadRequest.DownloadFileDestination = { _, _ in
    var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    return (documentsURL, [.removePreviousFile])

}
Alamofire.download(urlString, method: .get, parameters: parameters, encoding: JSONEncoding.default, to: destination)
.response{ response in

   if response.error == nil {

     let filename = response.response?.suggestedFilename
     var folderDestination=response.destinationURL?.path
     folderDestination=folderDestination?.appending("/\(nameCategory)")

     archiveToUnzip=(folderDestination?.appending("/\(filename!)"))!

     //unzip
     let successUnZip=SSZipArchive.unzipFile(atPath: archiveToUnzip, toDestination:folderDestination!)

     if !successUnZip {
        SpeedLog.print("Problem unzip")
     }
  }
}

It displays "Problem unzip", so am I wrong in the path to the zip file?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ludo
  • 743
  • 1
  • 10
  • 25

2 Answers2

0

Before unzipping try to check that all paths were correct:

    guard let zipPath = (folderDestination?.appending("/\(filename!)"))! else {
         print("Error: zipPath are not correct: \(zipPath)")    
         return
    }

    guard let unzipPath = folderDestination! else {
        print("Error: unzipPath are not correct: \(unzipPath)")
        return
    }

    let success = SSZipArchive.unzipFile(atPath: zipPath, toDestination: unzipPath)
    if !success {
        print("Error: unzipFile operation failed")
        return
    }
a-a-m
  • 96
  • 1
  • 5
0

Simply you can't create folder name by appending the path, You need to create folder separately. Here is the code try this!

 let filename = response.response?.suggestedFilename
 var folderDestination=response.destinationURL?.path
 folderDestination=folderDestination?.appending("/\(nameCategory)")
 try! FileManager.default.createDirectory(at: folderDestination!, withIntermediateDirectories: false, attributes: nil)

 archiveToUnzip=(folderDestination?.appending("/\(filename!)"))!

 //unzip
 let successUnZip=SSZipArchive.unzipFile(atPath: archiveToUnzip,toDestination:folderDestination!)
Karthikeyan Bose
  • 1,244
  • 3
  • 17
  • 26
  • I have tried this but the try throw have this error : Domain=NSCocoaErrorDomain Code=512 "Impossible to save file « Images » in folder « Documents »." UserInfo={NSFilePath=/Users/username/Library/Developer/CoreSimulator/Devices/3D7DE09C-CA8B-4E32-A048-2B33887CE47B/data/Containers/Data/Application/xxxx/Documents/Images, NSUnderlyingError=0x60800005bfc0 {Error Domain=NSPOSIXErrorDomain Code=20 "Not a directory"}}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.58.6/src/swift/stdlib/public/core/ErrorType.swift, line 178 Why it is not a directory ? – Ludo Nov 05 '16 at 15:58