Ton*_*lon 26 compression zip objective-c ios
真的坚持尝试编写代码来解压缩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);
}
Run Code Online (Sandbox Code Playgroud)
Sam*_*fes 41
我想要一个简单的解决方案,但没有找到我喜欢的,所以我修改了一个库来做我想要的.您可能会发现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];
Run Code Online (Sandbox Code Playgroud)
sch*_*der 13
"sample.zip"真的是用gZip创建的吗?.zip扩展名通常用于WinZip创建的档案.这些也可以使用zLib解压缩,但您必须解析标头并使用其他例程.
要检查,请查看文件的前两个字节.如果它是'PK',它是WinZip,如果它是0x1F8B,那就是gZip.
因为这是iPhone具体的,看看这个iPhone SDK的论坛讨论,其中miniZip被提及.这似乎可以处理WinZip文件.
但如果它真的是一个WinZip文件,你应该看看WinZip规范并尝试自己解析文件.它基本上应解析一些标头值,寻找压缩的流位置并使用zLib例程对其进行解压缩.
Car*_*tin 12
这段代码对我来说非常适合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");
Run Code Online (Sandbox Code Playgroud)
顺便说一下,我可以在77秒内将33MB解压缩到130MB,或者在未压缩/秒内解压缩约1.7 MB.
| 归档时间: |
|
| 查看次数: |
44553 次 |
| 最近记录: |