Compress a file with GZipStream while maintaining its meta-data

Go To


How can I get the extension of compressed file after being compressed with System.IO.Compression.GZipStream?

For example, if the original file is named test.doc and compresses to test.gz, how do I know what file extension to use when decompressing?

2009-06-16 14:06
by dinesh


I had to do this some time ago. The solution is to use the J# libraries to do it. You still write it in C# however.

That's microsofts answer on the topic.

2009-06-16 14:08
by Mech Software
Personally I'd rather use a library like SharpZipLib ( and stick to C - RichardOD 2009-06-16 14:14
There are times when it's not possible to use open source in closed source development. This may not be the case but offers a solution from within the existing Microsoft SDK - Mech Software 2009-07-08 18:00


There is no way to get the file name - in fact there may never be a filename at all, if for example a piece of data is created in memory and then send over a network connection.

Instead of replacing the file extension, why not append it, for example: test.doc.gz Then you can simply strip it off when decompressing.

2009-06-16 14:09
by Jon Grant


Not sure what is your question- I assume you want a mechanism to "remember" what the extension was before the compression took place?

If that is the question then the convention of test.doc compressing into test.doc.gz will work.

2009-06-16 14:08
by RichardOD


The test.gz is just a raw byte stream with no meta-data about what has been compressed (for example, original file name, extension etc). What you'd need to do is create an archive that contains the gzip stream and meta-data about each file contained in the archive.

The article linked to in Mech Software's answer provides a pretty reasonable way to implement this.

There was also this question (vaguely related) asked some time back which may help out:

How to compress a directory with the built in .net compression classes?

2009-06-16 14:16
by Kev