gpt4 book ai didi

objective-c - 尝试解压缩 zlib 数据时 Z_BUF_ERROR -5

转载 作者:行者123 更新时间:2023-11-29 13:23:06 24 4
gpt4 key购买 nike

我正在尝试使用来自 ObjectiveZlib 的 dataByDecompressingData: 方法解压一些 zlib 数据。示例代码,我得到了一些帮助将其转换为与 Objective-c 而不是 C++ 一起使用。代码如下

- (NSData*) dataByDecompressingData:(NSData*)data{
Byte* bytes = (Byte*)[data bytes];
NSInteger len = [data length];
NSMutableData *decompressedData = [[NSMutableData alloc] initWithCapacity:COMPRESSION_BLOCK];
Byte* decompressedBytes = (Byte*) malloc(COMPRESSION_BLOCK);

z_stream stream;
int err;
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
stream.opaque = (voidpf)0;

stream.next_in = bytes;
err = inflateInit(&stream);
CHECK_ERR(err, @"inflateInit");

while (true) {
stream.avail_in = len - stream.total_in;
stream.next_out = decompressedBytes;
stream.avail_out = COMPRESSION_BLOCK;
err = inflate(&stream, Z_NO_FLUSH);
[decompressedData appendBytes:decompressedBytes length:(stream.total_out-[decompressedData length])];
if(err == Z_STREAM_END)
break;
CHECK_ERR(err, @"inflate");
}

err = inflateEnd(&stream);
CHECK_ERR(err, @"inflateEnd");

free(decompressedBytes);
return decompressedData;
}

一旦运行,我就会得到 Z_BUF_ERROR,我已阅读 here我知道它指的是 ASIHTTPReqest 但我认为可能使用 xcode 提供的相同 zlib 类来解压缩,他们说这是一个错误,缓冲区的大小由于空间而无法处理文件的解压缩。

我不完全确定这是否正确,他们提供了两种解决方案来解决问题,第一种是拆分数据包……在我看来这不是一个选项,另一种方法是增加缓冲区大小。 .我在想A,我怎么能这样呢? B、有没有更好的第三种选择?

如有任何帮助,我们将不胜感激。

最佳答案

请引用http://www.zlib.net/zlib_how.html

Z_BUF_ERROR will be explained further below, but suffice it to say that this is simply an indication that inflate() could not consume more input or produce more output. inflate() can be called again with more output space or more available input, which it will be in this code.

我看到的一些错误:

1/每次你设置stream.next_in,你也应该设置stream.avail_in。请注意,当您不提供下一 block 输入时,您不应更改 stream.avail_in。在调用 inflateInit 之前设置初始 avail_in 可能是个好主意。

2/使用 inflateInit2(&stream, -MAX_WBITS) 可能是个好主意。请注意,inflateInit 的普通版本不会检查数据的压缩类型(gzip 或 zlib),如果它选择 gzip,您的解压将失败。

(应该可以,未经测试编写)

-(NSData*)decompressData:(NSData*)compressedData {
z_stream stream;

stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;

//we set the input, no need to change the values later
stream.next_in = (Bytef *) [compressedData bytes];
stream.avail_in = compressedData.length;

int result = inflateInit2(&stream, -MAX_WBITS);

if (result != Z_OK) {
return nil;
}

NSMutableData* uncompressedData = [NSMutableData data];
//make buffer big enough - 128KB
int bufferLength = 128 * 1024;
char *buffer = malloc(bufferLength);

while (true) {
stream.next_out = buffer;
stream.avail_out = bufferLength;

//calling with Z_FINISH because we already have all the input
result = inflate(&stream, Z_FINISH);

if (result != Z_OK && result != Z_STREAM_END) {
inflateEnd(&stream);
free(buffer);
return nil;
}

[uncompressedData appendBytes:buffer length:(bufferLength - stream.avail_out)];

if (result == Z_STREAM_END) {
break;
}
}


inflateEnd(&stream);
free(buffer);

return uncompressedData;
}

关于objective-c - 尝试解压缩 zlib 数据时 Z_BUF_ERROR -5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13868211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com