使用 GZipStream 压缩文件,同时保留其元数据
使用System.IO.Compression.GZipStream
压缩后如何获取压缩文件的扩展名?
例如,如果原始文件名为 test.doc
并压缩为 test.gz
,我如何知道解压时使用什么文件扩展名?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无法获取文件名 - 事实上,如果例如在内存中创建一段数据然后通过网络连接发送,则可能根本不存在文件名。
与其替换文件扩展名,为什么不附加它,例如:test.doc.gz
然后解压的时候直接把它剥掉就可以了。
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.
我前段时间不得不这样做。 解决方案是使用 J# 库来完成此操作。 不过,您仍然用 C# 编写它。
http://msdn.microsoft.com/en-us/magazine/cc164129。 aspx
这是微软对此主题的回答。
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.
不确定你的问题是什么 - 我假设你想要一种机制来“记住”压缩发生之前的扩展名是什么?
如果这是问题,那么将 test.doc 压缩到 test.doc.gz 的约定将起作用。
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.
test.gz 只是一个原始字节流,没有关于压缩内容的元数据(例如,原始文件名、扩展名等)。 您需要做的是创建一个存档,其中包含 gzip 流以及有关存档中包含的每个文件的元数据。
Mech Software 的答案中链接的文章提供了一种非常合理的方法来实现此目的。
不久前也有人提出过这个问题(模糊相关),这可能会有所帮助:
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: