Tuesday, August 23, 2022

Unzip File in Java

To unzip files in Java you can use classes provided by java.util.zip package for data compression and decompression.

Steps to unzip file in Java

To unzip a zipped file you need to read data from an input stream. For that you can use ZipInputStream class residing in the java.util.zip package.

Once ZIP input stream is opened, you can read the zip entries using the getNextEntry() method which returns a ZipEntry object. If the end-of-file is reached, getNextEntry returns null.

While going through the zip entries you can check whether that entry is for a directory or for a file, if it is a directory just create the folder in destination. If it is a file then you need to read the data to the output file by opening an OutputStream.

Java program for unzipping file

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.ZipInputStream;

public class Unzip {
  static final int BUFFER = 2048;
  // Output folder
  private static final String DEST_FOLDER = "G://Output";
  public static void main (String argv[]) {
    try {
      File folder = new File(DEST_FOLDER);
      if(!folder.exists()){
        folder.mkdir();
      }
      BufferedOutputStream dest = null;
      // zipped input
      FileInputStream fis = new FileInputStream("G://files.zip");
      ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
      ZipEntry entry;
      while((entry = zis.getNextEntry()) != null) {
        System.out.println("Extracting: " +entry);
        int count;
        byte data[] = new byte[BUFFER];
        String fileName = entry.getName();
        File newFile = new File(folder + File.separator + fileName);
        // If directory then just create the directory (and parents if required)
        if(entry.isDirectory()){
          if(!newFile.exists()){
            newFile.mkdirs();
          }
        }else{                
          // write the files to the disk
          FileOutputStream fos = new FileOutputStream(newFile);
          dest = new BufferedOutputStream(fos, BUFFER);
          while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
          }
          dest.flush();
          dest.close();
        }
        zis.closeEntry();                
      }
      zis.close();
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

Note that though here output folder is taken as a separate folder with different name, but you can also derive that name using the input folder name.

That's all for this topic Unzip File in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. How to Untar a File in Java
  2. How to Find Last Modified Date of a File in Java
  3. How to Read File From The Last Line in Java
  4. Reading File in Java Using BufferedReader
  5. Reading File in Java Using Scanner

You may also like-

  1. How to Sort ArrayList of Custom Objects in Java
  2. Print Odd-Even Numbers Using Threads And wait-notify Java Program
  3. Deadlock in Java Multi-Threading
  4. final Vs finally Vs finalize in Java
  5. Creating Custom Exception Class in Java
  6. Java Abstract Class and Abstract Method
  7. Callable and Future in Java With Examples
  8. Executor And ExecutorService in Java With Examples