1

I am using the below method to create a Gzip file by compressing the data.

        public static void GZipCompress(Stream dataToCompress, Stream outputStream)
        {

            GZipStream gzipStream = new GZipStream(outputStream, CompressionMode.Compress);

            int readBufferSize = 10000;

            byte[] data_ = new byte[readBufferSize];
            int bytesRead = dataToCompress.Read(data_, 0, readBufferSize);
            while (bytesRead > 0)
            {
                gzipStream.Write(data_, 0, bytesRead);
                data_ = new byte[readBufferSize];
                bytesRead = dataToCompress.Read(data_, 0, readBufferSize);
            }

            try
            {
                gzipStream.Flush();
                gzipStream.Close();
            }
            catch (ObjectDisposedException ioException)
            {

            }

        }

But if its already in a zip format, I should not compress it. Just have to append the data to the output GZip file without compressing. I am using the below method for that.

    public static void CopyStream(Stream input, Stream output)
    {
        byte[] buffer = new byte[16 * 1024]; // Fairly arbitrary size
        int bytesRead;

        while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, bytesRead);
        }

        output.Flush();
        output.Close();
    }

When the extension of the file doesn't contain "zip" I'm calling the GZipCompress method. When the extension of the file is "zip" I'm calling the CopyStream method. But after copying the content to the output stream through CopyStream into a GZip file, when I try to decompress this file, I'm getting the below exception.

 An unhandled exception of type 'System.IO.InvalidDataException' occurred in System.dll
 The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.

Is it possible to write uncompressed data to a GZip file? If so, what am I doing wrong here? If not, are there any other ways of achieving this? any help would be much appreciated.

AnOldSoul
  • 4,017
  • 12
  • 57
  • 118
  • You may be using the Stream classes Write method. Make sure your passing into your CopyStream method two GZipStreams or even better change your Parameter types to GZipStream and enforce this. – dannyhut Mar 26 '17 at 03:09
  • Even if I pass GZipStreams, I still cannot call GzipStream's write method. Because that'll compress data right? I do not want already compressed data to be compressed again :( – AnOldSoul Mar 26 '17 at 03:10
  • Not by the Documentation. Writes using GZipStreams write method writes data that is already compressed. Not sure if this is what you need though. – dannyhut Mar 26 '17 at 03:13
  • Just read further, my above comment appears to be wrong, the data does appear to get compressed by the method. Sorry. – dannyhut Mar 26 '17 at 03:20

2 Answers2

1

While it is likely to be possible, I am pretty sure it will make the file partially or completely unreadable because the uncompressed data you append will not match gzip format specification. I should also note that GzipStream does not provide any means of adding or extracting files from the archive and you might want to look into ZipArchive class.

Barsik the Cat
  • 363
  • 1
  • 2
  • 10
1

GZipStream and ZIP file format are different things - so you can't use ZIP as content of GZipStream.

ZIP is container for multiple potentially compressed files, while GZipStream is compressed content of single file without any additional headers. So when you use try to read content of ZIP with GZipStream it will find ZIP's container header instead expected GZip signature - and hence fail to parse.

If you want to package multiple files into one - use ZIP format - see Create normal zip file programmatically. If you could move to newer version of .Net from 3.5 you can use built in ZipFile and related classes instead of external libraries. It also supports adding non-compressed files - ZipArchive.CreateEntry(..., CompressionLevel.NoCompression).

If you could move to 4.5 version of the framework GZipStream supports compression levels - so you can write .GZ files without compression - GZipStream.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179