java - 如何使用 Java DeflaterOutputStream

标签 java gzip

编辑:我真的只需要知道 Deflater 派生类何时决定写入页眉和页脚数据,以及如何利用这些事实。我真的很想做以下事情:

  1. 用一些字节填充 Deflater 派生类的字典(我想我明白了)。

  2. 将一些要压缩的数据发送到 Deflater 派生类(我想我明白了)。

  3. 将所有压缩数据(没有页眉或页脚数据)输出到我想要的任何地方(不确定如何执行此操作,也可以同时具有页眉/页脚,或只有一个,就像只要它是一致的)。

  4. 从 1 开始重新使用对象。

原问:我正在使用 Java DeflaterOutputStream 来压缩一些数据。我还通过修改页眉和页脚来修改此压缩数据。我想给 DeflaterOutputStream 一些输入,让它只输出压缩数据部分,而不是 gzip 格式的页眉或页脚。我该怎么做?

到目前为止,我一直在尝试做这样的事情:

internalWriter.write(storage, 0, amountRead);
internalWriter.finish();
internalWriter.getDef().reset();

这里的internalWriter是DeflaterOutputStream的扩展。这将输出带有页眉和页脚的压缩数据。但是,在对同一对象的后续调用中,它会输出压缩数据和页脚。我基本上只想要压缩数据,或者每次都发生同样的事情。有任何想法吗?快速解释压缩流如何使用 close、flush、finish 也可能对我有所帮助,重点是何时创建和输出页眉和页脚。

每次我使用 DeflaterOutputStream 时,我都希望它立即输出所有内容。这就是为什么我在右侧之后完成了如上所示...

最佳答案

您可以在 Java Almanac 中看到很好的示例

--- 编辑 ---

让我尝试提供更多帮助。本书Java I/O埃利奥特·拉斯蒂·哈罗德 (Elliote Rusty Harold) 的著作可能是我找到的最好的引用资料。您可以从 OReilly Books 获得它。我将为您提供书中的一些引述和示例。

压缩数据

The Deflater class contains methods to compress blocks of data. You can choose the compression format, the level of compression, and the compression strategy. Deflating data with the Deflater class requires nine steps:

  1. Construct a Deflater object.
  2. Choose the strategy (optional).
  3. Set the compression level (optional).
  4. Preset the dictionary (optional).
  5. Set the input.
  6. Deflate the data repeatedly until needsInput( ) returns true.
  7. If more input is available, go back to step 5 to provide additional input data. Otherwise, go to step 8.
  8. Finish the data.
  9. If there are more streams to be deflated, reset the deflater.

More often than not, you don’t use this class directly. Instead, you use a Deflater object indirectly through one of the compressing stream classes like DeflaterInputStream or DeflaterOutputStream. These classes provide more convenient programmer interfaces for stream-oriented compression than the raw Deflater methods.

膨胀数据

  1. Construct an Inflater object.
  2. Set the input with the compressed data to be inflated.
  3. Call needsDictionary( ) to determine if a preset dictionary is required.
  4. If needsDictionary( ) returns true, call getAdler( ) to get the Adler-32 checksum of the dictionary. Then invoke setDictionary( ) to set the dictionary data.
  5. Inflate the data repeatedly until inflate( ) returns 0.
  6. If needsInput( ) returns true, go back to step 2 to provide additional input data.
  7. The finished( ) method returns true.

现在,这本书用了一整章来介绍压缩和解压缩数据,我认为不可能在这里解释所有内容。您必须完成部分任务,如果需要,请返回一个更窄的问题。

关于java - 如何使用 Java DeflaterOutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13059533/

相关文章:

java - 如何在struts属性字符串中插入java表达式

java - XML 解析器和 xpath 表达式

python - 在 Python 中读取 .tar.gz 文件

angular - 在 Angular 2+ 中压缩传出请求

java - 无法解析 fragment 类中的方法 getsystemservice

java - 处理 Future onSuccess 作为 Akka Actor 的回应

linux - gzip 和管道输出(性能考虑)

c# - 在 C# 中解压缩 BIG .tar.gz 的最快方法?

javascript - GZIP 压缩和缓存我的 JS 和 CSS 文件

Java 对象数组 Foreach 方法访问