gpt4 book ai didi

iphone - iPhone中的ZLib无法解压数据

转载 作者:行者123 更新时间:2023-12-03 11:16:17 26 4
gpt4 key购买 nike

我正在尝试使用 解压缩数据ZLib 在 iPhone 中,但它总是通过 "Invalid header Check" 的错误.

为了压缩我在 Java 中使用以下内容的数据

实现:Zlib 的标准 Java 实现

Deflator : java.util.zip.Deflater

1.45 版,06 年 4 月 7 日

压缩级别:BEST_COMPRESSION

iPhone中解压代码如下:

- (NSData *)zlibInflate
{
if ([self length] == 0) return self;

unsigned full_length = [self length];
unsigned half_length = [self length] / 2;

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

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

if (inflateInit (&strm) != 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 = [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) {
NSLog(@"%s", strm.msg);
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;
}

以下是一个示例压缩字符串:
xÚÝUko²Jþ~?­ó?¥¾?¤?©?´ÚjCMX,Òµ?ª?µßVX¹È­?¿.øë_?¯¶ZÏ%íùxHH&Ã<ÏÌ3ÌÎ
@2.ðE?ºqþpéEzÏ09IoÒ?ª? ?®?£àÌönì$brÛ#fl95?¿»a//Tçáò?¢?¿½
µ©ÊÃÉPÔ¼:8y¦ý.äÎ?µ?¥?¼y?©ã¯9ö?¥½?¢±ÝûwÛ?§ãga?©á8?¨?­m\Õ?»6,'Îe?¬}(L}7ÆÅ6#gJ(¥7´s?¬d.ó,Ë°¦prßýÕÖ? 

以下是压缩器的功能:
public static byte[] compress(String s) {
Deflater comp = new Deflater();
//comp.setLevel(Deflater.BEST_COMPRESSION);

comp.setInput(s.getBytes());

comp.finish();
ByteArrayOutputStream bos = new ByteArrayOutputStream(s.length());

// Compress the data
byte[] buf = new byte[1024];
try {
while (!comp.finished()) {
int count = comp.deflate(buf);
bos.write(buf, 0, count);
}
bos.close();
} catch (Exception e) {
//Log.d(TAG, e.getMessage());
e.printStackTrace();
}

// Get the compressed data
byte[] compressedData = bos.toByteArray();

// put in this fix for Symbol scanners
byte[] compressedDataForSymbol = mungeForSymbol(compressedData);
/*
* byte[] decompressedDataForSymbol =
* decompressedDataAfterSymbol(compressedDataForSymbol); // check they
* are the same for(int i=0;i<compressedData.length;i++) { if
* (compressedData[i] != decompressedDataForSymbol[i]) {
* //System.out.println("Error at " + i); } }
*/
return compressedDataForSymbol;
// return s.getBytes();

}

最佳答案

使用具有默认压缩级别的 Java Deflater 创建输出编码数据,其前两个字节为 0x78 0x9c 的 header 。这些在 IOS 中不使用。只需去掉前两个字节,然后在IOS 中尝试Inflate 即解压。它应该工作。
我遇到了同样的问题,但我想要从 IOS(压缩)到 android(解压缩)的数据。

关于iphone - iPhone中的ZLib无法解压数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14575923/

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