为了账号安全,请及时绑定邮箱和手机立即绑定

iPhone解压码

iPhone解压码

iOS
侃侃尔雅 2019-11-26 10:30:09
确实坚持尝试编写代码来解压缩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);  }
查看完整描述

3 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

我想要一个简单的解决方案,但在这里找不到我喜欢的解决方案,因此我修改了一个库以实现自己想要的功能。您可能会发现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];


查看完整回答
反对 回复 2019-11-26
?
子衿沉夜

TA贡献1828条经验 获得超3个赞

对于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。


查看完整回答
反对 回复 2019-11-26
  • 3 回答
  • 0 关注
  • 487 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信