gzip stream

Use GZIPOutputStream ZipOutputStream packing the output
maybe it’s destination a file or a socket, it’s not the point. usually we use generic paradigm the reprecent the data source in stream and the compressed data out stream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void compress(InputStream is, OutputStream os)
throws Exception {

GZIPOutputStream gos = new GZIPOutputStream(os);

int count;
byte data[] = new byte[BUFFER];
while ((count = is.read(data, 0, BUFFER)) != -1) {
gos.write(data, 0, count);
}

gos.finish();
gos.flush();
gos.close();
}