简体   繁体   English

如何使用 SharpZipLib BZip2 提取超过 900,000 字节

[英]How to extract more than 900,000 bytes using SharpZipLib BZip2

When using ICSharpCode.SharpZipLib to decompress BZip2 files I am having an issue where only the first 900,000 uncompressed bytes are extracted.当使用ICSharpCode.SharpZipLib解压缩 BZip2 文件时,我遇到了一个问题,即只提取了前 900,000 个未压缩字节。 I've tried both the BZip2InputStream and the BZip2.Decompress static method to no avail.我已经尝试了BZip2InputStreamBZip2.Decompress static 方法无济于事。 The compressed data is 8,518 bytes which I have confirmed is the length of the byte array that I pass in as compressedDataByteArray , and the decompressed data should be 1,134,592 bytes - so I can see that it is being truncated.压缩数据是 8,518 字节,我已经确认这是我作为compressedDataByteArray传入的字节数组的长度,解压后的数据应该是 1,134,592 字节 - 所以我可以看到它被截断了。

My attempt with BZip2InputStream - observe that the console writes out "900000" instead of "1134592":我尝试使用 BZip2InputStream - 观察控制台写出“900000”而不是“1134592”:

        static void Main(string[] args)
        {
            var compressedDataByteArray = File.ReadAllBytes("data.bz2");

            using (var mstream = new MemoryStream(compressedDataByteArray))
            using (var zstream = new BZip2InputStream(mstream))
            using (var reader = new StreamReader(zstream))
            {
                string uncompressedData = reader.ReadToEnd();
                Console.WriteLine(uncompressedData.Length);
            }

            Console.ReadKey();
        }

Alternatively, I have tried the BZip2.Decompress method - observe that the console also writes out "900000" instead of "1134592":或者,我尝试了BZip2.Decompress方法 - 观察控制台也写出“900000”而不是“1134592”:

        static void Main(string[] args)
        {
            var compressedDataByteArray = File.ReadAllBytes("data.bz2");

            using (var indata = new MemoryStream(compressedDataByteArray))
            using (var outdata = new MemoryStream())
            {
                BZip2.Decompress(indata, outdata, false);
                string uncompressedData = Encoding.UTF8.GetString(outdata.ToArray());
                Console.WriteLine(uncompressedData.Length);
            }

            Console.ReadKey();
        }

Is there some flag or option I am missing?我缺少一些标志或选项吗? Does the library need to be licensed?图书馆需要获得许可吗? I'm not clear why the uncompressed data always stops there.我不清楚为什么未压缩的数据总是停在那里。 For reference I am using #SharpZipLib 1.3.0 Nuget package作为参考,我正在使用#SharpZipLib 1.3.0 Nuget package

Here is the bz2 file I am using: https://drive.google.com/uc?id=1CD0XnJjAITxIrBqD90Msnygc4xnDXk5X&export=download这是我正在使用的 bz2 文件: https://drive.google.com/uc?id=1CD0XnJjAITxIrBqD90Msnygc4xnDXk5X&export=download

Evidently, it may be a shortcoming with the SharpZipLib, so I migrated to SharpCompress and it now works as expected:显然,这可能是 SharpZipLib 的一个缺点,所以我迁移到了 SharpCompress,它现在可以按预期工作:

static void Main(string[] args)
{
    var compressedDataByteArray = File.ReadAllBytes("data.bz2");

    using (var mstream = new MemoryStream(compressedDataByteArray))
    using (var unzipstream = new BZip2Stream(mstream, SharpCompress.Compressors.CompressionMode.Decompress, true))
    using (var reader = new StreamReader(unzipstream))
    {
        string uncompressedData = reader.ReadToEnd();
        Console.WriteLine(uncompressedData.Length);
    }

    Console.ReadKey();
}

Much thanks to @CodeCaster.非常感谢@CodeCaster。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM