【发布时间】:2015-07-25 18:24:24
【问题描述】:
我尝试解压缩一个仅包含一个项目(超过 100MB)的大 zip 文件,并希望在解压缩过程中显示进度。
我找到了可以根据解压缩文件的数量确定进度的解决方案,但在我的情况下,我里面只有一个大文件。所以我猜它必须由解压缩的字节数决定?
实际上我正在使用 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);
我没有找到解决方案。
有人有提示、示例或示例链接吗?
更新: 下面的代码看起来可以按预期工作,但是当只有一个文件被解压缩时,处理程序只会被调用一次(在解压缩结束时):
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:
正如“Martin R”在 SSArchive 中分析的那样,这是不可能的。 有没有其他方法可以解压文件并显示基于 kbytes 的进度?
更新 3:
在“roop”解释解决方案后,我更改了 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;
}
}
【问题讨论】:
-
你用的是什么解压库?
-
我和别人一起用过 SSZipArchive
-
如果我正确理解了 SSZipArchive 的源代码,则每个文件都会调用一次进度处理程序,并且在解压缩单个文件时不提供任何获取进度的选项。
-
感谢您的确认。这就是我所怀疑的。有没有其他方法可以实现这一目标?我的意思是我不固定到 SSZipArchive。
-
@mcfly soft,我认为这可以在zlib的帮助下实现。
标签: ios swift streaming archive unzip