Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top