Index: lutinutil/src/java/org/codelutin/util/ZipStreamEncoder.java diff -u /dev/null lutinutil/src/java/org/codelutin/util/ZipStreamEncoder.java:1.1 --- /dev/null Tue Feb 5 15:40:49 2008 +++ lutinutil/src/java/org/codelutin/util/ZipStreamEncoder.java Tue Feb 5 15:40:44 2008 @@ -0,0 +1,83 @@ +/* +* ##% Copyright (C) 2008 Code Lutin, Gabriel Landais +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package org.codelutin.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Map; +import java.util.zip.Deflater; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +/** + * The Class ZipStreamEncoder. + */ +public class ZipStreamEncoder extends Thread { + + /** The Constant BUFFER. */ + static final int BUFFER = 2048; + + /** The files. */ + private Map files; + + /** The zos. */ + private ZipOutputStream zos; + + /** + * Instantiates a new zip stream encoder. + * + * @param files + * the files + * @param os + * the os + */ + public ZipStreamEncoder(Map files, OutputStream os) { + super(); + this.files = files; + + zos = new ZipOutputStream(os); + zos.setMethod(ZipOutputStream.DEFLATED); + zos.setLevel(Deflater.BEST_COMPRESSION); + } + + /* (non-Javadoc) + * @see java.lang.Thread#run() + */ + @Override + public void run() { + byte data[] = new byte[BUFFER]; + try { + for (Map.Entry kv : files.entrySet()) { + ZipEntry entry = new ZipEntry(kv.getKey()); + InputStream origin = kv.getValue(); + zos.putNextEntry(entry); + int count; + while ((count = origin.read(data, 0, BUFFER)) != -1) { + zos.write(data, 0, count); + } + origin.close(); + } + + zos.close(); + } catch (IOException e) { + throw new RuntimeException("Impossible to compress in stream"); + } + } + +}