unzip File Into Directory : Zip Unzip « File « Java Tutorial






import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * @author csterling
 *
 */
public class ZipFileUtil {

  /**
   * @param zipFile
   * @param jiniHomeParentDir
   */
  public static void unzipFileIntoDirectory(ZipFile zipFile, File jiniHomeParentDir) {
    Enumeration files = zipFile.entries();
    File f = null;
    FileOutputStream fos = null;
    
    while (files.hasMoreElements()) {
      try {
        ZipEntry entry = (ZipEntry) files.nextElement();
        InputStream eis = zipFile.getInputStream(entry);
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
  
        f = new File(jiniHomeParentDir.getAbsolutePath() + File.separator + entry.getName());
        
        if (entry.isDirectory()) {
          f.mkdirs();
          continue;
        } else {
          f.getParentFile().mkdirs();
          f.createNewFile();
        }
        
        fos = new FileOutputStream(f);
  
        while ((bytesRead = eis.read(buffer)) != -1) {
          fos.write(buffer, 0, bytesRead);
        }
      } catch (IOException e) {
        e.printStackTrace();
        continue;
      } finally {
        if (fos != null) {
          try {
            fos.close();
          } catch (IOException e) {
            // ignore
          }
        }
      }
    }
  }

}








11.59.Zip Unzip
11.59.1.Uses Zip compression to compress any number of files given on the command line
11.59.2.Decompress a zip file using ZipFile
11.59.3.Unzip
11.59.4.gzip
11.59.5.Compress a Byte Array
11.59.6.Decompress a Byte Array
11.59.7.Compress Java objects
11.59.8.Decompress Java objects
11.59.9.Compress string(byte array) by Deflater
11.59.10.Compare two zip files
11.59.11.Put file To Zip File
11.59.12.Unpack a segment from a zip
11.59.13.Unpack an archive from a URL
11.59.14.unzip File Into Directory
11.59.15.Zip a list of file into one zip file.
11.59.16.Zip up a directory
11.59.17.Zip files