如何将 ICSharpCode.ZipLib 与流一起使用?
我对保守的标题和我的问题本身感到非常抱歉,但我迷路了。
ICsharpCode.ZipLib 提供的示例不包含我正在搜索的内容。 我想通过将 byte[] 放入 InflaterInputStream(ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream) 中来解压缩它
,我找到了一个解压缩函数,但它不起作用。
public static byte[] Decompress(byte[] Bytes)
{
ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream stream =
new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(new MemoryStream(Bytes));
MemoryStream memory = new MemoryStream();
byte[] writeData = new byte[4096];
int size;
while (true)
{
size = stream.Read(writeData, 0, writeData.Length);
if (size > 0)
{
memory.Write(writeData, 0, size);
}
else break;
}
stream.Close();
return memory.ToArray();
}
它在 line(size = stream.Read(writeData, 0, writeData.Length);) 处抛出异常,表示它有一个无效的标头。
我的问题不是如何修复该函数,该函数没有随库提供,我只是在谷歌上搜索到它。我的问题是,如何以与 InflaterStream 函数相同的方式解压缩,但没有例外。
再次感谢 - 对于保守的问题感到抱歉。
I'm very sorry for the conservative title and my question itself,but I'm lost.
The samples provided with ICsharpCode.ZipLib doesn't include what I'm searching for.
I want to decompress a byte[] by putting it in InflaterInputStream(ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream)
I found a decompress function ,but it doesn't work.
public static byte[] Decompress(byte[] Bytes)
{
ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream stream =
new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(new MemoryStream(Bytes));
MemoryStream memory = new MemoryStream();
byte[] writeData = new byte[4096];
int size;
while (true)
{
size = stream.Read(writeData, 0, writeData.Length);
if (size > 0)
{
memory.Write(writeData, 0, size);
}
else break;
}
stream.Close();
return memory.ToArray();
}
It throws an exception at line(size = stream.Read(writeData, 0, writeData.Length);) saying it has a invalid header.
My question is not how to fix the function,this function is not provided with the library,I just found it googling.My question is,how to decompress the same way the function does with InflaterStream,but without exceptions.
Thanks and again - sorry for the conservative question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
lucene中的代码非常好。
the code in lucene is very nice.
嗯,听起来数据不合适,否则代码可以正常工作。 (诚然,我会为流使用“using”语句,而不是显式调用
Close
。)您从哪里获取数据?
Well it sounds like the data is just inappropriate, and that otherwise the code would work okay. (Admittedly I'd use a "using" statement for the streams instead of calling
Close
explicitly.)Where did you get your data from?
为什么不使用 System.IO.Compression.DeflateStream 类(自 .Net 2.0 起可用)? 这使用相同的压缩/解压缩方法,但不需要额外的库依赖项。
从 .Net 2.0 开始,如果您需要文件容器支持,则只需要 ICSharpCode.ZipLib。
Why don't you use the System.IO.Compression.DeflateStream class (available since .Net 2.0)? This uses the same compression/decompression method but doesn't require an extra library dependency.
Since .Net 2.0 you only need the ICSharpCode.ZipLib if you need the file container support.