tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Zip Utilities > UnZip Directory

UnZip Directory 

The following example shows how to unzip a ZIP file to user given directory. It checks for the existence of target directories and create them as needed.

File Name  :  
com/bethecoder/tutorials/utils/zip/UnZipDirectory.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.utils.zip;

import java.io.BufferedOutputStream;
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;

public class UnZipDirectory {

  /**
   @param args
   @throws IOException
   */
  public static void main(String[] argsthrows IOException {
    unzipDirectory("C:\\pqr.zip""C:\\VVVV");
    System.out.println("Done..");
  }

  public static void unzipDirectory(String srcZipFile, String tgtDir)
  throws IOException {
    unzipDirectory(new File(srcZipFile)new File(tgtDir));
  }
  
  public static void unzipDirectory(File srcZipFile, File tgtDir)
      throws IOException {
    /**
     * Make target directory if not available already
     */
    tgtDir.mkdirs();
    
    ZipFile zipFile = new ZipFile(srcZipFile);
    Enumeration<?> entries = zipFile.entries();
    ZipEntry entry = null;
    File tgtFile = null;
    
    while (entries.hasMoreElements()) {
      entry = (ZipEntryentries.nextElement();
      tgtFile = new File(tgtDir, entry.getName());
      System.out.println("Unzipping file : " + tgtFile);
      
      if (entry.isDirectory() && !tgtFile.exists()) {
        tgtFile.mkdirs();
        continue;
      }
      
      /**
       * Handle file and create parent directory
       * if it does'nt exist
       */
      if (!tgtFile.getParentFile().exists()) {
        tgtFile.getParentFile().mkdirs();
      }

      InputStream inStream = zipFile.getInputStream(entry);
      BufferedOutputStream outStream = 
        new BufferedOutputStream(
          new FileOutputStream(tgtFile));

      byte [] buffer = new byte[1024];
      int read;

      while ((read = inStream.read(buffer)) 0) {
        outStream.write(buffer, 0, read);
      }

      inStream.close();
      outStream.close();
    }
  }
}
   

It gives the following output,
Unzipping file : C:\VVVV\NEW2\AAA\abc.copy.txt
Unzipping file : C:\VVVV\NEW2\AAA\BIOSinfo.log
Unzipping file : C:\VVVV\NEW2\AAA\IPC.LOG
Unzipping file : C:\VVVV\NEW2\AAA\readonly_file.txt
Unzipping file : C:\VVVV\NEW2\AAA\software.tsv
Unzipping file : C:\VVVV\NEW2\abc.copy.txt
Unzipping file : C:\VVVV\NEW2\BIOSinfo.log
Unzipping file : C:\VVVV\NEW2\CCCC\AAA\abc.copy.txt
Unzipping file : C:\VVVV\NEW2\CCCC\AAA\BIOSinfo.log
Unzipping file : C:\VVVV\NEW2\CCCC\AAA\IPC.LOG
Unzipping file : C:\VVVV\NEW2\CCCC\AAA\readonly_file.txt
Unzipping file : C:\VVVV\NEW2\CCCC\AAA\software.tsv
Unzipping file : C:\VVVV\NEW2\CCCC\BIOSinfo.log
Unzipping file : C:\VVVV\NEW2\CCCC\IPC.LOG
Unzipping file : C:\VVVV\NEW2\CCCC\readonly_file.txt
Unzipping file : C:\VVVV\NEW2\file_ops.txt
Unzipping file : C:\VVVV\NEW2\IPC.LOG
Unzipping file : C:\VVVV\NEW2\NEW_SUB\BIOSinfo.log
Unzipping file : C:\VVVV\NEW2\NEW_SUB\IPC.LOG
Unzipping file : C:\VVVV\NEW2\NEW_SUB\readonly_file.txt
Unzipping file : C:\VVVV\NEW2\Oracle Identity Manager.txt
Unzipping file : C:\VVVV\NEW2\readonly_file.txt
Unzipping file : C:\VVVV\NEW2\software.tsv
Unzipping file : C:\VVVV\NEW2\test.txt
Done..



 
  


  
bl  br