iPhone解压缩代码

真的停留在尝试编写代码来解压缩iPhone上的文件或目录。

以下是我正在使用的一些示例代码来尝试解压简单的文本文件。

它解压缩文件,但它的腐败。

(void)loadView { NSString *DOCUMENTS_FOLDER = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSString *path = [DOCUMENTS_FOLDER stringByAppendingPathComponent:@"sample.zip"]; NSString *unzipeddest = [DOCUMENTS_FOLDER stringByAppendingPathComponent:@"test.txt"]; gzFile file = gzopen([path UTF8String], "rb"); FILE *dest = fopen([unzipeddest UTF8String], "w"); unsigned char buffer[CHUNK]; int uncompressedLength = gzread(file, buffer, CHUNK); if(fwrite(buffer, 1, uncompressedLength, dest) != uncompressedLength || ferror(dest)) { NSLog(@"error writing data"); } else{ } fclose(dest); gzclose(file); } 

有“sample.zip”真的用gZip创build? .zip扩展名通常用于由WinZip创build的存档。 这些也可以使用zLib解压缩,但是你必须parsing头文件并使用其他例程。

要检查,看看文件的前两个字节。 如果是“PK”,则是WinZip,如果是0x1F8B,则为gZip。

因为这是iPhone专用的,请看这个提到miniZip的 iPhone SDK论坛讨论 。 看来这可以处理WinZip文件。

但是,如果它确实是一个WinZip文件,你应该看看WinZip规范,并尝试自己parsing文件。 它基本上应该parsing一些头值,寻找压缩的stream位置,并使用zLib例程来解压。

我想要一个简单的解决scheme,并没有find我喜欢的地方,所以我修改了一个图书馆来做我想要的。 你可能会发现SSZipArchive有用。 (它也可以创buildzip文件。)

用法:

 NSString *path = @"path_to_your_zip_file"; NSString *destination = @"path_to_the_folder_where_you_want_it_unzipped"; [SSZipArchive unzipFileAtPath:path toDestination:destination]; 

这段代码适合我gzip:

数据库是这样准备的:gzip foo.db

关键是在gzread()上循环。 上面的例子只读取了第一个CHUNK字节。

 #import <zlib.h> #define CHUNK 16384 NSLog(@"testing unzip of database"); start = [NSDate date]; NSString *zippedDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"foo.db.gz"]; NSString *unzippedDBPath = [documentsDirectory stringByAppendingPathComponent:@"foo2.db"]; gzFile file = gzopen([zippedDBPath UTF8String], "rb"); FILE *dest = fopen([unzippedDBPath UTF8String], "w"); unsigned char buffer[CHUNK]; int uncompressedLength; while (uncompressedLength = gzread(file, buffer, CHUNK) ) { // got data out of our file if(fwrite(buffer, 1, uncompressedLength, dest) != uncompressedLength || ferror(dest)) { NSLog(@"error writing data"); } } fclose(dest); gzclose(file); NSLog(@"Finished unzipping database"); 

顺便说一下,我可以在77秒内将33MB解压缩到130MB,或者每秒解压1.7MB左右。

这段代码会将任何.zip文件解压缩到你的应用程序文档目录中,并从应用程序资源中获取文件。

 self.fileManager = [NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSLog(@"document directory path:%@",paths); self.documentDirectory = [paths objectAtIndex:0]; NSString *filePath = [NSString stringWithFormat:@"%@/abc", self.documentDirectory]; NSLog(@"file path is:%@",filePath); NSString *fileContent = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"data.zip"]; NSData *unzipData = [NSData dataWithContentsOfFile:fileContent]; [self.fileManager createFileAtPath:filePath contents:unzipData attributes:nil]; // here we go, unzipping code ZipArchive *zipArchive = [[ZipArchive alloc] init]; if ([zipArchive UnzipOpenFile:filePath]) { if ([zipArchive UnzipFileTo:self.documentDirectory overWrite:NO]) { NSLog(@"Archive unzip success"); [self.fileManager removeItemAtPath:filePath error:NULL]; } else { NSLog(@"Failure to unzip archive"); } } else { NSLog(@"Failure to open archive"); } [zipArchive release]; 

解压任何zip文件真的很难。 这是一个复杂的文件格式,可能有许多不同的压缩程序可以在文件内部使用。 Info-ZIP有一些免费的代码可以做到这一点( http://www.info-zip.org/UnZip.html ),可以使用一些黑客在iPhone上工作,但API是坦率的可怕的 – 它涉及将命令行parameter passing给模拟UnZIP运行的假“主”(公平地说,因为他们的代码从来没有被devise成像这样使用,库函数之后就被闩上了)。

如果您可以控制要解压缩的文件的位置,我强烈build议使用另一个压缩系统而不是ZIP。 它的灵活性和无处不在使得它可以很好地将文件的档案以人对人的forms传递,但是自动化却非常尴尬。

zlib并不打算打开.zip文件,但是你很幸运:zlib的contrib目录包括minizip,它能够使用zlib打开.zip文件。

它可能不捆绑在SDK中,但你可以使用捆绑版本的zlib来使用它。 抓住zlib源文件的副本并查看contrib / minizip。

我还没有使用iPhone,但是您可能想看看GZIP ,这是一个非常便携的开放源码zip库,可用于许多平台。

我有一些运气在iPhone模拟器上testing:

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *saveLocation = [documentsDirectory stringByAppendingString:@"myfile.zip"]; NSFileManager* fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:saveLocation]) { [fileManager removeItemAtPath:saveLocation error:nil]; } NSURLRequest *theRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://example.com/myfile.zip"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; NSData *received = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil]; if ([received writeToFile:saveLocation atomically:TRUE]) { NSString *cmd = [NSString stringWithFormat:@"unzip \"%@\" -d\"%@\"", saveLocation, documentsDirectory]; // Here comes the magic... system([cmd UTF8String]); } 

它看起来比zlib的摆弄容易…