Compress a file with GZipStream while maintaining its meta-data

五迷三道 提交于 2019-12-11 02:52:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!