quarta-feira, 12 de junho de 2013

Utilitário para zipar byte[]

Pessoal,

Gostaria de compartilhar um código que implementei, são algumas funções para zipar arrays de byte, ou seja, zipa file, zipa byte[] e etc...

Segue o codigo do ZipUtil :

 import java.io.*;  
 import java.util.*;  
 import java.util.zip.*;  
 import java.nio.channels.*;  
 import java.nio.*;  
 public class ZipUtil {  
   public static byte[] compressByteArray(byte[] input) throws Exception {  
     Deflater compressor = new Deflater();  
     compressor.setLevel(Deflater.BEST_COMPRESSION);  
     compressor.setInput(input);  
     compressor.finish();  
     ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);  
     byte[] buf = new byte[1024];  
     while (!compressor.finished()) {  
       int count = compressor.deflate(buf);  
       bos.write(buf, 0, count);  
     }  
     try {  
       bos.close();  
     } catch (IOException e) {  
     }  
     byte[] compressedData = bos.toByteArray();  
     return compressedData;  
   }  
   public static byte[] uncompressByteArray(byte[] compressedData) throws Exception {  
     Inflater decompressor = new Inflater();  
     decompressor.setInput(compressedData);  
     ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);  
     byte[] buf = new byte[1024];  
     while (!decompressor.finished()) {  
       try {  
         int count = decompressor.inflate(buf);  
         bos.write(buf, 0, count);  
       } catch (DataFormatException e) {  
       }  
     }  
     try {  
       bos.close();  
     } catch (IOException e) {  
     }  
     byte[] decompressedData = bos.toByteArray();  
     return decompressedData;  
   }  
   public static void main(String args[]) {  
      try {  
      System.out.println("ZipUtil starting.");  
      FileInputStream fis = new FileInputStream("c:\\temp\\test.txt");  
      FileChannel fc = fis.getChannel();  
      byte[] data = new byte[(int) fc.size()];  
      ByteBuffer bb = ByteBuffer.wrap(data);  
      fc.read(bb);   
      System.out.println("Uncompressed Byte Array Size: "+data.length);  
      //Compress the byte array  
      byte [] compressedData = ZipUtil.compressByteArray(data);  
      System.out.println("Compressed Byte Array Size: "+compressedData.length);  
      //uncompress the byte array  
      byte [] uncompressedData = ZipUtil.uncompressByteArray(compressedData);  
      System.out.println("Uncompressed Data: " + new String(uncompressedData));  
     } catch (Exception e) {  
      System.err.println("An error occurred:"+e.toString());  
     }  
   }  
   public static void packZip(File output, List<File> sources) throws IOException  
   {  
     ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output));  
     zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);  
     for (File source : sources)  
     {  
       if (source.isDirectory())  
       {  
         zipDir(zipOut, "", source);  
       } else  
       {  
         zipFile(zipOut, "", source);  
       }  
     }  
     zipOut.flush();  
     zipOut.close();  
   }  
   private static String buildPath(String path, String file)  
   {  
     if (path == null || path.isEmpty())  
     {  
       return file;  
     } else  
     {  
       return path + "/" + file;  
     }  
   }  
   private static void zipDir(ZipOutputStream zos, String path, File dir) throws IOException  
   {  
     if (!dir.canRead())  
     {  
       return;  
     }  
     File[] files = dir.listFiles();  
     path = buildPath(path, dir.getName());  
     for (File source : files)  
     {  
       if (source.isDirectory())  
       {  
         zipDir(zos, path, source);  
       } else  
       {  
         zipFile(zos, path, source);  
       }  
     }  
   }  
   private static void zipFile(ZipOutputStream zos, String path, File file) throws IOException  
   {  
     if (!file.canRead())  
     {  
       return;  
     }  
     zos.putNextEntry(new ZipEntry(buildPath(path, file.getName())));  
     FileInputStream fis = new FileInputStream(file);  
     byte[] buffer = new byte[4092];  
     int byteCount = 0;  
     while ((byteCount = fis.read(buffer)) != -1)  
     {  
       zos.write(buffer, 0, byteCount);  
     }  
     fis.close();  
     zos.closeEntry();  
   }  
   public static void unzipFile(File arquivoZip,String pastaDestino) throws Exception{  
        FileInputStream fin = new FileInputStream(arquivoZip);  
     ZipInputStream zin = new ZipInputStream(fin);  
     ZipEntry ze = null;  
     while ((ze = zin.getNextEntry()) != null) {  
      System.out.println("Unzipping " + ze.getName());  
      FileOutputStream fout = new FileOutputStream(pastaDestino+ze.getName());  
      for (int c = zin.read(); c != -1; c = zin.read()) {  
       fout.write(c);  
      }  
      zin.closeEntry();  
      fout.close();  
     }  
     zin.close();  
   }  
 }  

Nenhum comentário:

Postar um comentário