views:

747

answers:

4

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?

A: 

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.

RichardOD
A: 

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.

http://msdn.microsoft.com/en-us/magazine/cc164129.aspx

That's microsofts answer on the topic.

Mech Software
Personally I'd rather use a library like SharpZipLib (http://www.icsharpcode.net/OpenSource/SharpZipLib/) and stick to C#
RichardOD
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
+3  A: 

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.

Jon Grant
A: 

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?

Kev