Home:ALL Converter>Problems with decompressing GZipStream

Problems with decompressing GZipStream

Ask Time:2018-06-08T22:08:25         Author:Pasha Z

Json Formatter

public void CompressFile(string fileName, string outFile)
    {
        var sync = new object();
        using (var inStream =
            new FileStream(fileName, FileMode.Open, FileAccess.Read))
        using (var bReader = new BinaryReader(inStream))
        using (var outStream =
            new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.ReadWrite))
        {
            var uncompressedBytes = new byte[1000000];
            while (bReader.Read(uncompressedBytes, 0, uncompressedBytes.Length) > 0)
            {
                var thr = new Thread(
                    () =>
                    {
                        lock (sync)
                        {
                            CompressedBytes = CompressGzip(uncompressedBytes);
                            ThreadsCount++;
                        }
                    });
                thr.Start();
                thr.Join();
                outStream.Write(CompressedBytes, 0, CompressedBytes.Length);
            }

            BeforeCompressionBytes = inStream.Length;
            AfterCompressionBytes = outStream.Length;
            inStream.Close();
            outStream.Close();
        }
    }

    public void DecompressFile(string fileName, string outFile)
    {
        var sync = new object();
        using (var inStream =
            new FileStream(fileName, FileMode.Open, FileAccess.Read))
        using (var bReader = new BinaryReader(inStream))
        using (var outStream =
            new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.ReadWrite))
        {
            var uncompressedBytes = new byte[1000000];

            while (bReader.Read(uncompressedBytes, 0, uncompressedBytes.Length) > 0)
            {
                    var thr = new Thread(
                        () =>
                        {
                            lock (sync)
                            {
                                CompressedBytes = DecompressGzip(uncompressedBytes);
                                ThreadsCount++;
                            }
                        });
                    thr.Start();
                    thr.Join();
                    outStream.Write(CompressedBytes, 0, CompressedBytes.Length);
            }

            BeforeCompressionBytes = inStream.Length;
            AfterCompressionBytes = outStream.Length;
            inStream.Close();
            outStream.Close();
        }
    }


    public byte[] CompressGzip(byte[] uncompressedBytes)
    {
        using (var memory = new MemoryStream())
        {
            using
                (var gZipStream =
                    new GZipStream(memory, CompressionMode.Compress, true))
            {
                gZipStream.Write
                    (uncompressedBytes, 0, uncompressedBytes.Length);
            }
            return memory.ToArray();
        }
    }

    public byte[] DecompressGzip(byte[] compressedBytes)
    {
        var decompressed = new byte[compressedBytes.Length];
        using (var memory = new MemoryStream(compressedBytes))
        {
            using
            (var gZipStream =
                new GZipStream(memory, CompressionMode.Decompress, true))
            {
                gZipStream.Read
                    (decompressed, 0, compressedBytes.Length);
            }
            return memory.ToArray();
        }
    }

Here is my compression/decompression code. I have a task to read files by blocks (blocksize 1MB) into others threads and compress and write every block separately. And have an "System.IO.InvalidDataException was caught Message=The magic number in GZip header is not correct. Make sure you are passing in a GZip stream" while decompressing.

What's wrong?

Author:Pasha Z,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762488/problems-with-decompressing-gzipstream
yy