Using GZipStream to compress empty input results in invalid gz file in C #

I am using the C # GZipStream class to compress some input. The problem is that this input is empty. In this case, it creates a file with 0 bytes. When I try to use 7zip to unzip the resulting .gz file, it gives an error stating that the format is invalid. If I have non-empty input, it works fine. Tell me, how can I create a valid .gz file that will be unpacked into a file with byte 0?

var file = new FileStream("foo.txt.gz", FileMode.Create, FileAccess.ReadWrite); var gzip = new GZipStream(file, CompressionMode.Compress); var writer = new StreamWriter(gzip); for (string line in input) { writer.Write(line); } writer.Close(); gzip.Close(); file.Close(); 

In the above code, if my "input" array is empty, I end up writing a file called foo.txt.gz with 0 bytes, and 7zip says the file is not valid. But if I have a non-empty array, I get a valid file. Please tell me how I can change my code to solve the problem, so that I get a valid .gz file, even if the input is empty. Thanks!


EDIT: This may be a bug in .NET. If you notice the same problem and agree that this is a mistake, vote: https://connect.microsoft.com/VisualStudio/feedback/details/888912/gzipstream-creates-invalid-gz-files-when-input-is-empty

+6
source share
1 answer

Unfortunately, this looks like a bug with the implementation of GZipStream in the .NET library.

According to the documentation, it should "appear as a valid, empty compressed file" in accordance with MSDN ( http://msdn.microsoft.com/en-ca/library/as1ff51s.aspx ). But, when I tested your code and some options, I also get a completely empty file.

For comparison, if I create an empty gzip file using Cygwin (echo -n | gzip -9> empty.gz), I get a 20-byte file.

I suppose you could get around this by finding that your input is empty and manually writing out an empty gzip file. You can refer to the documentation for GZIP files (Wikipedia will be a good place to start) to create a file manually or hard-code the 20 bytes needed for an empty file in your program (with this solution, the internal timestamp and some other flags may be wrong, but this may not affect you in practice).

Alternatively, use a third-party compression library such as SharpZipLib ( http://icsharpcode.imtqy.com/SharpZipLib/ ) or DotNetZip ( http://dotnetzip.codeplex.com/ ), which implements GZIP and uses their implementation instead GZipStream.

+3
source

Source: https://habr.com/ru/post/970274/


All Articles