Ben*_*ins 5 .net c# compression zlib
我正在使用zlib.NET库来尝试和扩充由zlib压缩的文件(也许在Linux机器上).这是我正在做的事情:
zlib.ZInputStream zinput =
new zlib.ZInputStream(File.Open(path, FileMode.Open, FileAccess.Read));
while (stopByte != (data = zinput.ReadByte()))
{
// check data here
}
zinput.Close();
Run Code Online (Sandbox Code Playgroud)
数据字节与压缩数据字节匹配,因此我必须做错事.
跳过 zlib 标头(前两个字节78 9C),然后使用DeflateStream内置的 .net 对我有用。
using(var input = File.OpenRead(...))
using(var output = File.Create(...))
{
// if there are additional headers before the zlib header, you can skip them:
// input.Seek(xxx, SeekOrigin.Current);
if (input.ReadByte() != 0x78 || input.ReadByte() != 0x9C)//zlib header
throw new Exception("Incorrect zlib header");
using (var deflateStream = new DeflateStream(decryptedData, CompressionMode.Decompress, true))
{
deflateStream.CopyTo(output);
}
}
Run Code Online (Sandbox Code Playgroud)
除了在面对异常时未使用"using"语句来关闭流,除了我看起来没问题.数据肯定是压缩的吗?你能在Linux机器上用zlib解压缩吗?
查看了源代码后,它非常可怕 - 例如,调用int Read(buffer, offset, length)最终将调用其内部int Read()方法length时间.鉴于这种不稳定的开始,我不确定我是否会特别信任这些代码,但我预计它至少会有点起作用!你尝试过使用SharpZipLib吗?
看来我错误地假设所有虚拟方法都被覆盖了,事实并非如此.我使用的是zlib.ZInputStream.ReadByte(),它只是继承的Stream.ReadByte(),它不会进行任何膨胀.
我使用zlib.ZInputStream.Read()代替它,它的工作方式应该如此.
| 归档时间: |
|
| 查看次数: |
19127 次 |
| 最近记录: |