简体   繁体   English

在没有文件I / O的情况下压缩Java中的内容

[英]Compressing content in Java without file I/O

I would like to perform repeated compression task for CPU profiling without doing any file I/O but strictly reading a byte stream. 我想在不进行任何文件I / O但严格读取字节流的情况下执行CPU分析的重复压缩任务。 I want to do this in Java (target of my benchmark). 我想用Java(我的基准测试的目标)来做这件事。

Does anyone have a suggestion how to do this? 有没有人有建议怎么做? I used Zip API that uses ZipEntry but ZipEntry triggers file I/O. 我使用了ZipEntry的Zip API,但ZipEntry触发了文件I / O.

Any suggestions or code samples are highly appreciated. 任何建议或代码示例都非常感谢。

I used Zip API that uses ZipEntry but ZipEntry triggers file I/O. 我使用了ZipEntry的Zip API,但ZipEntry触发了文件I / O.

I wouldn't expect it to if you use a ByteArrayOutputStream as the underlying output: 如果你使用ByteArrayOutputStream作为底层输出,我不会指望它:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zipStream = new ZipOutputStream(baos);
... write to zipStream ...

Likewise wrap your byte array for reading data from in a ByteArrayInputStream . 同样包装你的字节数组以从ByteArrayInputStream 读取数据。

Of course, ZipOutputStream is appropriate if you want to create content using the zip compression format, which is good for (or at least handles :) multiple files. 当然,如果你想使用zip压缩格式创建内容, ZipOutputStream是合适的,这对于(或至少处理:)多个文件是有益的。 For a single stream of data, you may want to use DeflaterOutputStream or GZIPOutputStream , again using a ByteArrayOutputStream as the underlying output. 对于单个数据流,您可能需要使用DeflaterOutputStreamGZIPOutputStream ,再次使用ByteArrayOutputStream作为基础输出。

您可以使用ByteArrayInputStream和ByteArrayOutputStream,而不是使用FileInputStream或FileOutputStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM