| Line | |
|---|
| 1 | package omq.common.util; |
|---|
| 2 | |
|---|
| 3 | import java.io.ByteArrayInputStream; |
|---|
| 4 | import java.io.ByteArrayOutputStream; |
|---|
| 5 | import java.io.IOException; |
|---|
| 6 | import java.util.zip.GZIPInputStream; |
|---|
| 7 | import java.util.zip.GZIPOutputStream; |
|---|
| 8 | |
|---|
| 9 | public 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.