我想使用InputStream
压缩ZipOutputStream
,然后从压缩的InputStream
获取ZipOutputStream
,而不将文件保存在光盘上。那可能吗?
最佳答案
我想到了:
public InputStream getCompressed(InputStream is) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(bos);
zos.putNextEntry(new ZipEntry(""));
int count;
byte data[] = new byte[2048];
BufferedInputStream entryStream = new BufferedInputStream(is, 2048);
while ((count = entryStream.read(data, 0, 2048)) != -1) {
zos.write( data, 0, count );
}
entryStream.close();
zos.closeEntry();
zos.close();
return new ByteArrayInputStream(bos.toByteArray());
}
关于compression - 将ZipOutputStream转换为ByteArrayInputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20221128/