objective-c - 使用 objective zip 解压 NSData

标签 objective-c ios zlib unzip

我最近找到了Objective Zip我一直在阅读说明以在我的项目中进行设置。但是我不太确定如何使用它来解压我想要解压的一些 NSData。

我查看了示例解决方案,它似乎正在对 zip 文件执行解压缩,代码大致如下所示

ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:filePath mode:ZipFileModeUnzip];

[unzipFile goToFirstFileInZip];
ZipReadStream *read1= [unzipFile readCurrentFileInZip];

给出或采取一些其他说明,这是他们向您展示的使用方法,他们的示例代码是 here

我想知道如何使用 NSData 做同样的事情?还是我必须将 NSData 转换为 zipFile?如果是这样,那是如何正确执行的?

如果 zlib 已压缩,我将尝试解压缩 NSData...任何示例代码都会有所帮助

最佳答案

这里是https://stackoverflow.com/a/6466832/751885

我用下面两种方法处理NSData

并调用

saveToFile 

方法写入磁盘。

[[self compressData:uncompressedData] writeToFile:@"fileName.zip" atomically:YES];

压缩:

-(NSData*) compressData:(NSData* )uncompressedData {
if ([uncompressedData length] == 0) return uncompressedData;

z_stream strm;

strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.total_out = 0;
strm.next_in=(Bytef *)[uncompressedData bytes];
strm.avail_in = (unsigned int)[uncompressedData length];

// Compresssion Levels:
//   Z_NO_COMPRESSION
//   Z_BEST_SPEED
//   Z_BEST_COMPRESSION
//   Z_DEFAULT_COMPRESSION

if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY) != Z_OK) return nil;

NSMutableData *compressed = [NSMutableData dataWithLength:16384];  // 16K chunks for expansion

do {

    if (strm.total_out >= [compressed length])
        [compressed increaseLengthBy: 16384];

    strm.next_out = [compressed mutableBytes] + strm.total_out;
    strm.avail_out = (unsigned int)([compressed length] - strm.total_out);

    deflate(&strm, Z_FINISH);  

} while (strm.avail_out == 0);

deflateEnd(&strm);

[compressed setLength: strm.total_out];
return [NSData dataWithData:compressed];
}

解压缩:

-(NSData*) uncompressGZip:(NSData*) compressedData {
if ([compressedData length] == 0) return compressedData;

NSUInteger full_length = [compressedData length];
NSUInteger half_length = [compressedData length] / 2;

NSMutableData *decompressed = [NSMutableData dataWithLength: full_length + half_length];
BOOL done = NO;
int status;

z_stream strm;
strm.next_in = (Bytef *)[compressedData bytes];
strm.avail_in = (unsigned int)[compressedData length];
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;

if (inflateInit2(&strm, (15+32)) != Z_OK) return nil;

while (!done) {
    // Make sure we have enough room and reset the lengths.
    if (strm.total_out >= [decompressed length]) {
        [decompressed increaseLengthBy: half_length];
    }
    strm.next_out = [decompressed mutableBytes] + strm.total_out;
    strm.avail_out = (unsigned int)([decompressed length] - strm.total_out);

    // Inflate another chunk.
    status = inflate (&strm, Z_SYNC_FLUSH);
    if (status == Z_STREAM_END) {
        done = YES;
    } else if (status != Z_OK) {
        break;
    }
}
if (inflateEnd (&strm) != Z_OK) return nil;

// Set real length.
if (done) {
    [decompressed setLength: strm.total_out];
    return [NSData dataWithData: decompressed];
} else {
    return nil;
}
}

关于objective-c - 使用 objective zip 解压 NSData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13831953/

相关文章:

objective-c - 在 OpenGL objective c os x 中绘制纹理

ios - Swift Eureka 隐藏更改部分

javascript - 如何从另一个应用程序将图像共享到我的 Cordova/PhoneGap 应用程序?

python - zlib 和 PYTHONPATH 的 Linux Mint 9 Virtualenv ImportError 为空?

node.js - 如何在 node.js 中捕获 zlib 错误

objective-c - swift 类 : Fatal error: Use of unimplemented initializer 'init()' for class

ios - 如何在 objective-c 中的头文件中访问定义的常量数组?

ios - 实时麦克风声级监测

php - 通知 : ob_end_flush(): failed to send buffer of zlib output compression (1) in

ios - 使用临时配置编译应用失败,但调试配置在Xcode 5.1中有效