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 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * 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   * Copyright: 2007
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: Zip.java 2027 2010-11-26 11:33:50Z dmsmith $
21   */
22  
23  package org.crosswire.common.compress;
24  
25  import java.io.BufferedInputStream;
26  import java.io.ByteArrayOutputStream;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.util.zip.Deflater;
30  import java.util.zip.DeflaterOutputStream;
31  import java.util.zip.Inflater;
32  import java.util.zip.InflaterInputStream;
33  
34  /**
35   * Zip manages the compression and uncompression of Zip files.
36   * 
37   * @see gnu.lgpl.License for license details.<br>
38   *      The copyright to this program is held by it's authors.
39   * @author DM Smith [dmsmith555 at yahoo dot com]
40   */
41  public class Zip extends AbstractCompressor {
42      /**
43       * Create a Zip that is capable of transforming the input.
44       * 
45       * @param input
46       *            to compress or uncompress.
47       */
48      public Zip(InputStream input) {
49          super(input);
50      }
51  
52      /*
53       * (non-Javadoc)
54       * 
55       * @see org.crosswire.common.compress.Compressor#compress()
56       */
57      public ByteArrayOutputStream compress() throws IOException {
58          BufferedInputStream in = new BufferedInputStream(input);
59          ByteArrayOutputStream bos = new ByteArrayOutputStream();
60          DeflaterOutputStream out = new DeflaterOutputStream(bos, new Deflater(), BUF_SIZE);
61          byte[] buf = new byte[BUF_SIZE];
62  
63          for (int count = in.read(buf); count != -1; count = in.read(buf)) {
64              out.write(buf, 0, count);
65          }
66          in.close();
67          out.flush();
68          out.close();
69          return bos;
70      }
71  
72      /*
73       * (non-Javadoc)
74       * 
75       * @see org.crosswire.common.compress.Compressor#uncompress()
76       */
77      public ByteArrayOutputStream uncompress() throws IOException {
78          return uncompress(BUF_SIZE);
79      }
80  
81      /*
82       * (non-Javadoc)
83       * 
84       * @see org.crosswire.common.compress.Compressor#uncompress(int)
85       */
86      public ByteArrayOutputStream uncompress(int expectedLength) throws IOException {
87          ByteArrayOutputStream out = new ByteArrayOutputStream(expectedLength);
88          InflaterInputStream in = new InflaterInputStream(input, new Inflater(), expectedLength);
89          byte[] buf = new byte[expectedLength];
90  
91          for (int count = in.read(buf); count != -1; count = in.read(buf)) {
92              out.write(buf, 0, count);
93          }
94          in.close();
95          out.flush();
96          out.close();
97          return out;
98      }
99  
100 }
101