0
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?

  • not the answer, but in `DecompressGzip`: 1. note that the `decompressed` array obviously needs to be much larger than`compressedBytes.Length` and 2. you return `memory.ToArray()` where you probably meant `decompressed`. – René Vogt Jun 08 '18 at 14:39
  • 3
    Why are you reading a large chunk of bytes, then creating a thread, getting the thread to compress the bytes, waiting for the thread to finish and then writing out the bytes? Why bother with the thread? – Neil Jun 08 '18 at 14:47

0 Answers0