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);  

}


侃侃尔雅
浏览 493回答 3
3回答

慕盖茨4494581

我想要一个简单的解决方案,但在这里找不到我喜欢的解决方案,因此我修改了一个库以实现自己想要的功能。您可能会发现SSZipArchive有用。(它也可以顺便创建zip文件。)用法: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&nbsp; NSLog(@"testing unzip of database");&nbsp; start = [NSDate date];&nbsp; NSString *zippedDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"foo.db.gz"];&nbsp; NSString *unzippedDBPath = [documentsDirectory stringByAppendingPathComponent:@"foo2.db"];&nbsp; gzFile file = gzopen([zippedDBPath UTF8String], "rb");&nbsp; FILE *dest = fopen([unzippedDBPath UTF8String], "w");&nbsp; unsigned char buffer[CHUNK];&nbsp; int uncompressedLength;&nbsp; while (uncompressedLength = gzread(file, buffer, CHUNK) ) {&nbsp; &nbsp; // got data out of our file&nbsp; &nbsp; if(fwrite(buffer, 1, uncompressedLength, dest) != uncompressedLength || ferror(dest)) {&nbsp; &nbsp; &nbsp; NSLog(@"error writing data");&nbsp; &nbsp; }&nbsp; }&nbsp; fclose(dest);&nbsp; gzclose(file);&nbsp; NSLog(@"Finished unzipping database");顺便说一句,我可以在77秒内将33MB解压缩到130MB,或者每秒压缩约1.7MB。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS