I want to compress an InputStream
using ZipOutputStream
and then get the InputStream
from compressed ZipOutputStream
without saving file on disc. Is that possible?
Asked
Active
Viewed 3.2k times
8

user1766169
- 1,932
- 3
- 22
- 44

yasser
- 401
- 1
- 4
- 10
1 Answers
31
I figured it out:
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());
}

informatik01
- 16,038
- 10
- 74
- 104

yasser
- 401
- 1
- 4
- 10
-
1Came here to see if my code would work and found that I did just the same as you – Groben May 15 '17 at 15:27