Thursday, September 27, 2012

Extract Zip File from source to Destination using Java

import java.io.*;
import java.util.*;
import java.util.zip.*;

public class ExtractzipDemo{
 
  public static void extract(String file, String destination) throws IOException {
    ZipInputStream in = null;
    OutputStream out = null;
    try {
      // Open the ZIP file
      in = new ZipInputStream(new FileInputStream(file));
    // Get the first entry
      ZipEntry entry = null;
      while ((entry = in.getNextEntry()) != null) {
        String outFilename = entry.getName();
        // Open the output file
        if (entry.isDirectory()) {
          new File(destination, outFilename).mkdirs();
        } else {
          out = new FileOutputStream(new File(destination,outFilename));
          // Transfer bytes from the ZIP file to the output file
          byte[] buf = new byte[1024];
          int len;
          while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
          }
        // Close the stream
          out.close();
        }
      }
    } finally {
      // Close the stream
      if (in != null) {
        in.close();
      }
      if (out != null) {
        out.close();
      }
    }
  }
 
  public static void main(String[] args){
 
   String Src= "D:\Test\Src.zip";
   String Dest="D:\ZipOutput";
   extract(Src,Dest);
   System.out.println("Done....");
 
  }
  }
}

No comments:

Post a Comment