2016-11-02 107 views
1

我用Alamofire4下载一个zip文件并用SSZipArchive解压缩,但解压缩不起作用。我不确定下载文件的路径对Alamofire是否合适。用Alamofire4下载文件的路径(Swift3)

下面是代码:

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") 
    } 
    } 
} 

它显示“问题解压”,所以我错了路径到zip文件?

回答

0

解压缩尝试检查所有的路径是正确的之前:

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 
    } 
0

只要你无法通过附加路径创建文件夹的名称,您需要单独创建的文件夹。这里是代码尝试这个!

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!) 
+0

我都试过,但在试投有这样的错误: 域= NSCocoaErrorDomain代码= 512“无法保存文件«图像»文件夹中«文件»” 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代码= 20“不是目录”}}:file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.58.6/src/swift/stdlib/public/core/ErrorType.swift,第178行 为什么它不是一个目录? – Ludo

相关问题