【问题标题】:DeflateStream.Read always returns zeroDeflateStream.Read 总是返回零
【发布时间】:2017-04-15 22:32:43
【问题描述】:

我有一个完全有效的 zip 文件,它只包含一个打包文件,可以通过外部实用程序解压缩,它不会让 DeflateStream 有任何有用的东西。我已经删除了前两个字节前缀(“50h 4Bh”,这些前缀总是产生“块长度与其补码不匹配”异常),但是没有任何进展 - unzipped[] 总是包含零并且 Read 也返回 0。

byte[] zipped = new byte[] { 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x08, 0x00, 0xe4, 0x6e, 0x89, 0x4a, 0x1d, 0x42, 0x8f, 0xb7, 0x1d, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x30, 0x63, 0x64, 0x60, 0x60, 0xa8, 0xd7, 0x0b, 0x62, 0x60, 0xa8, 0x65, 0xc4, 0x8e, 0x41, 0x0a, 0x18, 0x18, 0xeb, 0x41, 0x14, 0xa3, 0x08, 0x03, 0x8c, 0x0f, 0x27, 0x40, 0x00, 0x00, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x00, 0x14, 0x00, 0x00, 0x00, 0x08, 0x00, 0xe4, 0x6e, 0x89, 0x4a, 0x1d, 0x42, 0x8f, 0xb7, 0x1d, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x2f, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00 };
MemoryStream zipstream = new MemoryStream(zipped);
byte[] unzipped = new byte[1024];
using (MemoryStream unzipstream = new MemoryStream(unzipped))
{
    using (DeflateStream worker = new DeflateStream(zipstream, CompressionMode.Decompress))
    {
        try
        {
            int length = worker.Read(unzipped, 0, 1024);

            //worker.CopyTo(unzipstream);
            //Console.Write(unzipstream.Length);
            //int length = unzipstream.Read(unzipped,0,1024);
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
    }
}

【问题讨论】:

    标签: .net compression deflate


    【解决方案1】:

    这是一个 zip 文件,而不是 zlib 流。使用ZipFile 类。

    【讨论】:

    • 谢谢,那会奏效的。 ZipArchive ziparc = ZipFile.Open(openFileDialog1.FileName, ZipArchiveMode.Read); DeflateStream defstream0 = (DeflateStream)ziparc.Entries[0].Open(); byte[] buffer = new byte[1024]; defstream0.Read(buffer, 0, 1024); 但我应该更好地描述我的情况。我试图避免磁盘操作,因为我收到这个存档是一个字节[]。
    • 您可以在这种情况下使用ZipInputStream,它可以在您包装在ByteArrayInputStream 中的内存数组上工作。 @ZuOverture
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    相关资源
    最近更新 更多