问题
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?
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
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?
来源:https://stackoverflow.com/questions/1001711/compress-a-file-with-gzipstream-while-maintaining-its-meta-data