简体   繁体   中英

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. I've tried both the BZip2InputStream and the BZip2.Decompress static method to no avail. 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.

My attempt with BZip2InputStream - observe that the console writes out "900000" instead of "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":

        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

Here is the bz2 file I am using: 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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