简体   繁体   English

使用 SharpZipLib 解压 zlib 压缩数据

[英]Decompressing zlib compressed data using SharpZipLib

I am looking at the following line of c++ code, where "zlib.h" is included我正在查看 c++ 代码的以下行,其中包含“zlib.h”

int err = compress2(compressed.get(), &dest_len,
                            reinterpret_cast<const unsigned char *>(plain_text.c_str()),
                            source_len, 9);

I am trying to undo this compression using C#.我正在尝试使用 C# 撤消此压缩。 I figure I should use a C# library because writing my own decompression algorithm seems wasteful and difficult.我想我应该使用 C# 库,因为编写自己的解压缩算法似乎既浪费又困难。 The library I have seem recommended is SharpZipLib (even by Mark Adler himself,) However.我似乎推荐的库是 SharpZipLib(甚至是 Mark Adler 本人),但是。 it seems like compress2 with these args is using a preset dictionary: Here is my attempt at decompressing:似乎带有这些参数的 compress2 正在使用预设字典:这是我解压缩的尝试:

Inflater inflater = new Inflater(false);
inflater.SetInput(bytes);
int ret = inflater.Inflate(outBuffer, 0, 2047);
if (ret == 0)
{
    Console.WriteLine(inflater.IsNeedingDictionary);
    Console.WriteLine(inflater.IsNeedingInput);
    Console.WriteLine(inflater.Adler);
}

This says it needs a dictionary.这说它需要一本字典。 I know how to set the dictionary, but I am clueless about how to set it based on the zlib.h compress function.我知道如何设置字典,但我不知道如何根据 zlib.h 压缩 function 进行设置。 How can I create the necessary preset dictionary to decrypt?如何创建必要的预设字典来解密?

Notes: Here is the line of code I am trying to essentially replicate in C#注意:这是我试图在 C# 中复制的代码行

int err = uncompress(uncompressed.get(), &dest_len, plain_text.get(), src_len);

Where uncompressed is empty, dest len is simply 4*src_len, and src_len is compressed length.其中 uncompressed 为空,dest len 就是 4*src_len,src_len 是压缩长度。 Compressed text stored in plain_text.存储在 plain_text 中的压缩文本。

Here are the detected first 2 bytes in my C# program: 78 3f (0111 1000 0011 1111)这是在我的 C# 程序中检测到的前 2 个字节:78 3f (0111 1000 0011 1111)

Someone indicated that this doesn't make sense given the compress2 code given...有人指出,鉴于给出的 compress2 代码,这没有意义......

Sorry for any mistakes in my question, I haven't asked many questions on here.对不起,我的问题有任何错误,我在这里没有问很多问题。

No, compress2() does not use a dictionary.不, compress2()不使用字典。 Your data may not be making it over intact.您的数据可能无法完好无损。 You should put the first several bytes of what you're trying to decompress, as seen on the C# side, in hex in your question.您应该将尝试解压缩的内容的前几个字节(如 C# 一侧所见)以十六进制形式放在您的问题中。

If anyone is trying to use compress2 from zlib, and trying to deal with this input in C#, beware how you take in the compressed data.如果有人尝试使用 zlib 中的 compress2,并尝试在 C# 中处理此输入,请注意如何获取压缩数据。 Compress2() does not use a preset dictionary. Compress2()使用预设字典。

Specifically, make sure that the first 2 bytes make sense.具体来说,确保前 2 个字节有意义。 I shrugged off a strange second byte because the first byte was correct, but really read it and compare against the RFC 1950我对奇怪的第二个字节不屑一顾,因为第一个字节是正确的,但真的阅读它并与RFC 1950进行比较

There are many ways to properly read the bytes, but my mistake was trying to read them using Encoding.Ascii.GetBytes.有很多方法可以正确读取字节,但我的错误是尝试使用 Encoding.Ascii.GetBytes 读取它们。 Don't do that!不要那样做!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用C#打开Delphi ZLib压缩数据 - Open Delphi ZLib compressed data with c# 解压缩压缩的二进制文件 - decompressing compressed binary file sharpziplib 压缩文件要在外部解压缩 - sharpziplib compressed files to be uncompressed externally 使用 NodeJS 中的 Zlib 和 .Net 中的 SharpZipLib 等不同库时,GZip 类型的压缩比会发生变化吗? - Will the GZip type compression ratio change when using different libraries like Zlib in NodeJS and SharpZipLib in .Net? 将压缩流解压缩到网络位置 - Decompressing a compressed stream to a network location SharpZipLib:将单个文件压缩为单个压缩文件 - SharpZipLib : Compressing a single file to a single compressed file 如何使用 ICSharpCode.SharpZipLib 压缩文件数据并使用 HttpClient 上传? - How to compress file data with ICSharpCode.SharpZipLib and upload using HttpClient? 如何在不实际压缩的情况下获得压缩文件的大小[SharpZipLib] - How to get the size of a compressed file without actually compressing it [SharpZipLib] 如何使用zlib python从未压缩的字符串中取回压缩字符串? - how to get back the compressed string back from uncompressed string using zlib python? SharpZipLib解压缩压缩文件时返回垃圾字符 - SharpZipLib returning junk characters when unzipping compressed file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM