yas*_*ser 6 compression zipoutputstream
我想压缩一个InputStream使用ZipOutputStream,然后InputStream从压缩ZipOutputStream而不保存光盘上的文件.那可能吗?
yas*_*ser 18
我想到了:
public InputStream getCompressed( InputStream is )
throws IOException
{
byte data[] = new byte[2048];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream( bos );
BufferedInputStream entryStream = new BufferedInputStream( is, 2048);
ZipEntry entry = new ZipEntry( "" );
zos.putNextEntry( entry );
int count;
while ( ( count = entryStream.read( data, 0, 2048) ) != -1 )
{
zos.write( data, 0, count );
}
entryStream.close();
zos.closeEntry();
zos.close();
return new ByteArrayInputStream( bos.toByteArray() );
}
Run Code Online (Sandbox Code Playgroud)