检测流是否用Java压缩的最佳方法 [英] Best way to detect if a stream is zipped in Java
本文介绍了检测流是否用Java压缩的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
找出java.io.InputStream
包含压缩数据的最佳方法是什么?
What is the best way to find out i java.io.InputStream
contains zipped data?
推荐答案
魔术字节的ZIP格式为50 4B
.您可以测试流(使用 mark 和重置-您可能需要缓冲区),但是我不希望这是100%可靠的方法.无法将其与以字母PK
开头的US-ASCII编码文本文件区分开.
The magic bytes for the ZIP format are 50 4B
. You could test the stream (using mark and reset - you may need to buffer) but I wouldn't expect this to be a 100% reliable approach. There would be no way to distinguish it from a US-ASCII encoded text file that began with the letters PK
.
最佳方法是在打开流之前,先以内容格式提供元数据,然后对其进行适当处理.
The best way would be to provide metadata on the content format prior to opening the stream and then treat it appropriately.
这篇关于检测流是否用Java压缩的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文