| Zip.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 or later
5 * as published by the Free Software Foundation. This program is distributed
6 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
7 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * © CrossWire Bible Society, 2007 - 2016
18 *
19 */
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 /**
32 * Zip manages the compression and uncompression of Zip files.
33 *
34 * @see gnu.lgpl.License The GNU Lesser General Public License for details.
35 * @author DM Smith
36 */
37 public class Zip extends AbstractCompressor {
38 /**
39 * Create a Zip that is capable of transforming the input.
40 *
41 * @param input
42 * to compress or uncompress.
43 */
44 public Zip(InputStream input) {
45 super(input);
46 }
47
48 /*
49 * (non-Javadoc)
50 *
51 * @see org.crosswire.common.compress.Compressor#compress()
52 */
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 /*
69 * (non-Javadoc)
70 *
71 * @see org.crosswire.common.compress.Compressor#uncompress()
72 */
73 public ByteArrayOutputStream uncompress() throws IOException {
74 return uncompress(BUF_SIZE);
75 }
76
77 /*
78 * (non-Javadoc)
79 *
80 * @see org.crosswire.common.compress.Compressor#uncompress(int)
81 */
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