source: trunk/src/main/java/omq/common/util/Zipper.java @ 49

Last change on this file since 49 was 44, checked in by stoda, 11 years ago

Objectmq converted to maven project

File size: 1.1 KB
Line 
1package omq.common.util;
2
3import java.io.ByteArrayInputStream;
4import java.io.ByteArrayOutputStream;
5import java.io.IOException;
6import java.util.zip.GZIPInputStream;
7import java.util.zip.GZIPOutputStream;
8
9public class Zipper {
10
11        public static byte[] zip(byte[] b) throws IOException {
12                ByteArrayOutputStream baos = new ByteArrayOutputStream();
13
14                GZIPOutputStream zos = null;
15                try {
16                        zos = new GZIPOutputStream(baos);
17                        zos.write(b);
18                } finally{
19                        if(zos != null){
20                                zos.close();
21                        }
22                       
23                        baos.close();
24                }
25               
26                return baos.toByteArray();
27        }
28
29        public static byte[] unzip(byte[] b) throws IOException {
30                ByteArrayOutputStream baos = new ByteArrayOutputStream();
31                ByteArrayInputStream bais = new ByteArrayInputStream(b);
32
33                GZIPInputStream zis = null;
34                try {
35                        zis = new GZIPInputStream(bais);
36                       
37                        byte[] tmpBuffer = new byte[256];
38                        int n;
39                        while ((n = zis.read(tmpBuffer)) >= 0) {
40                                baos.write(tmpBuffer, 0, n);
41                        }
42                } finally {             
43                        if(zis != null){
44                                zis.close();
45                        }
46                       
47                        bais.close();
48                        baos.close();
49                }               
50               
51                return baos.toByteArray();
52        }       
53}
Note: See TracBrowser for help on using the repository browser.