8

InputStreamを使用して圧縮し、ファイルをディスクに保存せずに圧縮されたものをZipOutputStream取得したい。それは可能ですか?InputStreamZipOutputStream

4

1 に答える 1

29

私はそれを考え出した:

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());
}
于 2013-11-27T09:56:36.990 に答える