我嘗試解壓僅包含一個項目(超過100MB)的大zip文件,並希望在解壓縮過程中顯示進度。如何解壓縮包含一個文件的大型壓縮文件並用swift獲取以字節爲單位的進度?
我發現解決方案的進度可以根據解壓縮文件的數量來確定,但在我的情況下,我只有一個大文件。所以我想這一定是由解壓縮的字節數決定的?
其實我使用SSZipArchive用下面的代碼工作正常:
var myZipFile:NSString="/Users/user/Library/Developer/CoreSimulator/Devices/mydevice/ziptest/testzip.zip";
var DestPath:NSString="/Users/user/Library/Developer/CoreSimulator/Devices/mydevice/ziptest/";
let unZipped = SSZipArchive.unzipFileAtPath(myZipFile as! String, toDestination: DestPath as! String);
我發現這個無解。
有沒有人有提示,樣本或鏈接到樣本?
UPDATE: 下面的代碼看起來如預期它會工作,但處理程序將只調用一次(在解壓結束)時,只有一個文件解壓縮:
func unzipFile(sZipFile: String, toDest: String){
SSZipArchive.unzipFileAtPath(sZipFile, toDestination: toDest, progressHandler: {
(entry, zipInfo, readByte, totalByte) -> Void in
println("readByte : \(readByte)") // <- This will be only called once, at the end of unzipping. My 500MB Zipfile holds only one file.
println("totalByte : \(totalByte)")
//Asynchrone task
dispatch_async(dispatch_get_main_queue()) {
println("readByte : \(readByte)")
println("totalByte : \(totalByte)")
//Change progress value
}
}, completionHandler: { (path, success, error) -> Void in
if success {
//SUCCESSFUL!!
} else {
println(error)
}
})
}
更新2:
作爲「馬丁R」在SSArchive分析,它不可能。 是否有任何其他方式來解壓文件並顯示基於千字節的進度?
UPDATE 3:
我改變了SSZipArchive.m溶液用 「魯普」 解釋如下之後。也許別人可以用這個太:
FILE *fp = fopen((const char*)[fullPath UTF8String], "wb");
while (fp) {
int readBytes = unzReadCurrentFile(zip, buffer, 4096);
if (readBytes > 0) {
fwrite(buffer, readBytes, 1, fp);
totalbytesread=totalbytesread+4096;
// Added by me
if (progressHandler)
{
progressHandler(strPath, fileInfo, currentFileNumber, totalbytesread);
}
// End added by me
} else {
break;
}
}
你正在使用什麼解壓縮庫? – chedabob
我與其他人一起使用SSZipArchive –
如果我正確理解了SSZipArchive的源代碼,則會爲每個文件調用一次進度處理程序,並且在解壓縮單個文件時不提供任何選項以獲取進度。 –