1
20 package org.crosswire.common.compress;
21
22 import java.io.BufferedInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.zip.Deflater;
27 import java.util.zip.DeflaterOutputStream;
28 import java.util.zip.Inflater;
29 import java.util.zip.InflaterInputStream;
30
31
37 public class Zip extends AbstractCompressor {
38
44 public Zip(InputStream input) {
45 super(input);
46 }
47
48
53 public ByteArrayOutputStream compress() throws IOException {
54 BufferedInputStream in = new BufferedInputStream(input);
55 ByteArrayOutputStream bos = new ByteArrayOutputStream();
56 DeflaterOutputStream out = new DeflaterOutputStream(bos, new Deflater(), BUF_SIZE);
57 byte[] buf = new byte[BUF_SIZE];
58
59 for (int count = in.read(buf); count != -1; count = in.read(buf)) {
60 out.write(buf, 0, count);
61 }
62 in.close();
63 out.flush();
64 out.close();
65 return bos;
66 }
67
68
73 public ByteArrayOutputStream uncompress() throws IOException {
74 return uncompress(BUF_SIZE);
75 }
76
77
82 public ByteArrayOutputStream uncompress(int expectedLength) throws IOException {
83 ByteArrayOutputStream out = new ByteArrayOutputStream(expectedLength);
84 InflaterInputStream in = new InflaterInputStream(input, new Inflater(), expectedLength);
85 byte[] buf = new byte[expectedLength];
86
87 for (int count = in.read(buf); count != -1; count = in.read(buf)) {
88 out.write(buf, 0, count);
89 }
90 in.close();
91 out.flush();
92 out.close();
93 return out;
94 }
95
96 }
97