View Javadoc
1   /*
2    * #%L
3    * Nuiton Utils
4    * %%
5    * Copyright (C) 2004 - 2010 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.util;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.io.OutputStream;
31  import java.util.Map;
32  import java.util.zip.Deflater;
33  import java.util.zip.ZipEntry;
34  import java.util.zip.ZipOutputStream;
35  
36  /** The Class ZipStreamEncoder. */
37  public class ZipStreamEncoder extends Thread {
38  
39      /** Logger. */
40      private static final Log log = LogFactory.getLog(ZipStreamEncoder.class);
41  
42      /** The Constant BUFFER. */
43      static final int BUFFER = 2048;
44  
45      /** The files. */
46      private Map<String, InputStream> files;
47  
48      /** The zos. */
49      private ZipOutputStream zos;
50  
51      /**
52       * Instantiates a new zip stream encoder.
53       *
54       * @param files the files
55       * @param os    the os
56       */
57      public ZipStreamEncoder(Map<String, InputStream> files, OutputStream os) {
58          this.files = files;
59  
60          zos = new ZipOutputStream(os);
61          zos.setMethod(ZipOutputStream.DEFLATED);
62          zos.setLevel(Deflater.BEST_COMPRESSION);
63      }
64  
65      /* (non-Javadoc)
66       * @see java.lang.Thread#run()
67       */
68      @Override
69      public void run() {
70          byte data[] = new byte[BUFFER];
71          try {
72              for (Map.Entry<String, InputStream> kv : files.entrySet()) {
73                  ZipEntry entry = new ZipEntry(kv.getKey());
74                  InputStream origin = kv.getValue();
75                  zos.putNextEntry(entry);
76                  int count;
77                  while ((count = origin.read(data, 0, BUFFER)) != -1) {
78                      zos.write(data, 0, count);
79                  }
80                  origin.close();
81              }
82  
83              zos.close();
84          } catch (IOException e) {
85              for (Map.Entry<String, InputStream> kv : files.entrySet()) {
86                  InputStream origin = kv.getValue();
87                  try {
88                      origin.close();
89                  } catch (IOException ioe) {
90                      log.error("Impossible to close " + kv.getKey());
91                  }
92              }
93              log.error("Impossible to compress in stream");
94              throw new RuntimeException("Impossible to compress in stream");
95          }
96      }
97  
98  }