Mega Code Archive

 
Categories / Java Tutorial / File
 

Unpack a segment from a zip

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Utils {   /**    * unpack a segment from a zip    *     * @param addsi    * @param packetStream    * @param version    */   public static void unpack(InputStream source, File destination) throws IOException {     ZipInputStream zin = new ZipInputStream(source);     ZipEntry zipEntry = null;     FileOutputStream fout = null;     byte[] buffer = new byte[4096];     while ((zipEntry = zin.getNextEntry()) != null) {       long ts = zipEntry.getTime();       // the zip entry needs to be a full path from the       // searchIndexDirectory... hence this is correct       File f = new File(destination, zipEntry.getName());       f.getParentFile().mkdirs();       fout = new FileOutputStream(f);       int len;       while ((len = zin.read(buffer)) > 0) {         fout.write(buffer, 0, len);       }       zin.closeEntry();       fout.close();       f.setLastModified(ts);     }     fout.close();   } }