Showing posts with label ZIP. Show all posts
Showing posts with label ZIP. Show all posts

Create ZIP File using Java Code

 


Create ZIP File using Java Code



We seen how to unzip a ZIP file using Java code in our earlier tutorial, now we will see how to create a ZIP file using Java code in this tutorial. Below sample code will read list of files in a folder and zip file name as input parameters. 



import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CreateZIPFile {

 public static void main(String argv[]) {
  // List of files in a folder to zip
  String filesPath = "D:\\filesfolder";
  // ZIP file name and path to create
  String zipFileName = "D:\\myZipFile.zip";
  
  createZIPFile(filesPath, zipFileName);
 }
 
 public static void createZIPFile(String filesPath, String zipFileName){

  try {
  
   FileOutputStream dest = new FileOutputStream(zipFileName);
   ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));

   byte buffer[] = new byte[1024];
   File file = new File(filesPath);
   String files[] = file.list();
   System.out.println("No. of files to ZIP : "+files.length);
   BufferedInputStream bis = null;
   for (int i = 0; i < files.length; i++) {
    FileInputStream fi = new FileInputStream(filesPath+ "\\"+ files[i]);
    bis = new BufferedInputStream(fi, 1024);
    ZipEntry entry = new ZipEntry(files[i]);
    out.putNextEntry(entry);
    int count;
    while ((count = bis.read(buffer, 0, 1024)) != -1) {
     out.write(buffer, 0, count);
    }
    bis.close();
   }
   out.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}









Unzip a ZIP File using Java Code

 


Unzip a ZIP File using Java Code


In this tutorial we will see about unzipping a Zip file using Java code. Below example will gives you the code to unzip a zip file by taking zip file and output folder path as input parameters. 



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

public class UnZip {

 public static final void main(String[] args) {

  // Zip file path
  String zipFile = "D:\\myZipFile.zip";

  // Output folder to save the unzip files
  String outputFolder = "D:\\unzipfolder\\";
  
  unzipFile(zipFile, outputFolder);

 }
 
 public static void unzipFile(String zipFile, String outputFolder){
  
  ZipFile zFile = null;

  try {
   zFile = new ZipFile(zipFile);
   Enumeration zEntries = zFile.entries();

   while (zEntries.hasMoreElements()) {
    ZipEntry zipEntry = (ZipEntry) zEntries.nextElement();

    // Check for Directory or not
    if (zipEntry.isDirectory()) {
     // Creating directory
     (new File(zipEntry.getName())).mkdir();
     continue;
    }

    // Unzipping files
    createUnzipFile(zFile.getInputStream(zipEntry),
      new BufferedOutputStream(new FileOutputStream(
        outputFolder + zipEntry.getName())));
   }
   zFile.close();
  } catch (IOException e) {
   e.printStackTrace();
  
  } 
 }

 public static final void createUnzipFile(InputStream iStream,
   OutputStream oStream) {
  try {
   byte[] buffer = new byte[1024];
   int len;
   while ((len = iStream.read(buffer)) >= 0)
    oStream.write(buffer, 0, len);

  iStream.close();
  oStream.close();
  } catch (Exception e) {
   e.printStackTrace();
 
  } 
 }
}