package commons.utility; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Enumeration; import java.util.zip.DataFormatException; import java.util.zip.Deflater; import java.util.zip.Inflater; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; import commons.utility.database.NullValue; import commons.exception.ZCompressException; public class Compression { /** Factory per la creazione dei file in uscita */ public final static class OutputZipFileFactory { /** Crea un file zip aperto in scrittura a partire da un OutputStream * @param outstream lo stream su cui wrappare lo zip * @param sizeBuffer se != null && > 0 allora lo stream viene wrappato all'interno di un BufferedOutputStream * @return Una impl wrappata nel seguente modo: * se outstream instanceof ZipOutputStream wrap diretto su outstream * se outstream instanceof BufferedOutputStream allora wrap di un ZipOutputStream intorno a outstrema * negli altri casi,se sizeBuffer > 0 allora wrapping su un BufferedOutputStream(outstream,sizeBuffer) * altrimenti wrapping diretto su outstream * @throws ZCompressException */ public final static OutputZipFile create(final OutputStream outstream,final Integer sizeBuffer) throws ZCompressException { OutputZipFile impl = null; // Se lo stream ? quello di un file zip... if(outstream instanceof ZipOutputStream) // ...aggancialo e basta! impl = new OutputZipFile((ZipOutputStream) outstream); else { OutputStream _outstream = outstream; if(null != sizeBuffer && sizeBuffer.intValue() > 0 && !(outstream instanceof BufferedOutputStream)) _outstream = new BufferedOutputStream(outstream,sizeBuffer); impl = new OutputZipFile ( new ZipOutputStream(_outstream) ); } return impl; } /** Crea un file zip aperto in scrittura,se il file esiste viene sovrascritto * @param filename * @return * @throws ZCompressException */ public final static OutputZipFile create(String filename) throws ZCompressException { FileOutputStream fos = null; try { fos = new FileOutputStream(filename); return create(fos,10); } catch(IOException e) { if(null != fos) { try { fos.close(); } catch (IOException e1) { } fos = null; } throw new ZCompressException(e); } finally { } } } public Compression() {} private static final int BUFFER= 2048; ///////////////////////////////////////////////////////////////////////////// //Compressione Decompressione FILE /** Comprime un blocco di dati all'interno di un file zip * @param dataToStore il blocco di dati da comprimere * @param dataToStoreName il nome che il blocco di dati avr? all'interno del file zip * @param outputFile il nome del file di uscita * @throws ZCompressException */ public static void CompressFile(byte[] dataToStore, String dataToStoreName, String outputFile) throws ZCompressException { ZipOutputStream out = null; try { out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile))); ZipEntry entry = new ZipEntry(dataToStoreName); out.putNextEntry(entry); out.write(dataToStore); } catch(IOException e) { throw new ZCompressException(e); } finally { try { if(null != out) out.close(); } catch(IOException e) { throw new ZCompressException(e); } } } public static String CompressFile(String inputFile, String outputFile) throws ZCompressException { try { String err= "OK"; BufferedInputStream origin= null; ZipOutputStream out= null; FileInputStream fi= null; try { FileOutputStream dest= new FileOutputStream(outputFile); out= new ZipOutputStream(new BufferedOutputStream(dest)); byte data[]= new byte[BUFFER]; File f= new File(inputFile); fi= new FileInputStream(f); origin= new BufferedInputStream(fi, BUFFER); ZipEntry entry= new ZipEntry(f.getName()); out.putNextEntry(entry); int count; while ((count= origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } } catch (IOException ex) { err= ex.getMessage(); } finally { origin.close(); out.close(); fi.close(); } return err; } catch (IOException e) { throw new ZCompressException(e); } } public static String CompressFile(String[] inputFile, String outputFile) throws ZCompressException { try { String err= "OK"; BufferedInputStream origin= null; ZipOutputStream out= null; FileInputStream fi= null; try { FileOutputStream dest= new FileOutputStream(outputFile); out= new ZipOutputStream(new BufferedOutputStream(dest)); byte data[]= new byte[BUFFER]; for (int i= 0; i < inputFile.length; i++) { File f= new File(inputFile[i]); fi= new FileInputStream(f); origin= new BufferedInputStream(fi, BUFFER); ZipEntry entry= new ZipEntry(f.getName()); out.putNextEntry(entry); int count; while ((count= origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } fi.close(); } } catch (IOException ex) { err= ex.getMessage(); } finally { if (origin != null) { origin.close(); } if (out != null) { out.close(); } if (fi != null) { fi.close(); } } return err; } catch (IOException e) { throw new ZCompressException(e); } } public static String DecompressFile(String inputFile, String outputFile) throws ZCompressException { try { String err= "OK"; BufferedOutputStream dest= null; BufferedInputStream is= null; FileOutputStream fos= null; ZipFile zipfile= null; try { ZipEntry entry; zipfile= new ZipFile(inputFile); Enumeration e= zipfile.entries(); while (e.hasMoreElements()) { entry= (ZipEntry)e.nextElement(); is= new BufferedInputStream(zipfile.getInputStream(entry)); int count; byte data[]= new byte[BUFFER]; if (null == outputFile || NullValue.STRING.equalsIgnoreCase(outputFile)) { String path = inputFile.substring(0, inputFile.lastIndexOf(File.separator)); fos= new FileOutputStream(path + File.separator + entry.getName()); } else { fos= new FileOutputStream(outputFile); } dest= new BufferedOutputStream(fos, BUFFER); while ((count= is.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } if(dest != null) { dest.flush(); dest.close(); } if(is != null) { is.close(); } if(fos != null) { fos.flush(); fos.close(); } } } catch (IOException ex) { err= ex.getMessage(); } finally { if(dest != null) { dest.flush(); dest.close(); } if(is != null) { is.close(); } if(fos != null) { fos.flush(); fos.close(); } if(zipfile != null) { zipfile.close(); } } return err; } catch (IOException e) { throw new ZCompressException(e); } } public static String DecompressFile(String inputFile) throws ZCompressException { return DecompressFile(inputFile, null); } ///////////////////////////////////////////////////////////////////////////// //Compressione Decompressione BYTE[] public static byte[] Compress(byte[] input) throws ZCompressException { try { Deflater compressor= new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); // Give the compressor the data to compress compressor.setInput(input); compressor.finish(); ByteArrayOutputStream bos= new ByteArrayOutputStream(input.length); // Compress the data byte[] buf= new byte[BUFFER]; while (!compressor.finished()) { int count= compressor.deflate(buf); bos.write(buf, 0, count); } //try { bos.close(); //} catch (IOException e){ //} // Get the compressed data byte[] compressedData= bos.toByteArray(); return compressedData; } catch (IOException e) { throw new ZCompressException(e); } } public static byte[] Decompress(byte[] input) throws ZCompressException { try { Inflater decompressor= new Inflater(); decompressor.setInput(input); // Create an expandable byte array to hold the decompressed data ByteArrayOutputStream bos= new ByteArrayOutputStream(input.length); // Decompress the data byte[] buf= new byte[BUFFER]; while (!decompressor.finished()) { //try { int count= decompressor.inflate(buf); bos.write(buf, 0, count); //} catch (DataFormatException e) { //} } //try { bos.close(); //} catch (IOException e) { //} // Get the decompressed data byte[] decompressedData= bos.toByteArray(); return decompressedData; } catch (DataFormatException e) { throw new ZCompressException(e); } catch (IOException e) { throw new ZCompressException(e); } } public static void CompressFile(String pathRedix, String pathSingleFile, ZipOutputStream out) throws IOException { BufferedInputStream origin = null; FileInputStream fi = null; try { byte data[] = new byte[BUFFER]; File f = new File(pathRedix + pathSingleFile); fi = new FileInputStream(f); origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry(pathSingleFile); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } fi.close(); } catch (IOException ex) { throw ex; } finally { if (origin != null) { origin.close(); origin = null; } if (fi != null) { fi.close(); fi = null; } } } }