Open In App

How to Zip and Unzip Files in Java?

In Java Programming we have a lot of features available for solving real-time problems. Here I will explain how to Zip files and How to Unzip files by using Java Programming. Mostly zip and unzip files are used for zip the files for save storage, easy to share, and other purposes. In this article, we will learn how to zip and unzip files using the Java Programming Language.

Prerequisites:

Zip and Unzip files in Java

For Zip files in Java programming, Java programming provides classes and methods for handling files. So Here we have the FileOutputStream class is used for Handling files and ZipOutputStream is used for Zip the Files by using putNextEntry() and ZipEntry(). These two methods are used to Move normal files into Zip files and the next method is used to convert into a single Zip file.

ZipInputStream and FileInputStream these two classes are used to Unzip the Zipped files. Once extract the files from the Zip file then those files are saved into the root folder individually. And we have methods getNextEntry() and mkdirs() for handling the compression of the files and creating folders. Below I provide the example for zip and unzip files in Java with related output.

Example:

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

public class ZipExample {

    // Method to zip files
    public static void zipFiles(String[] srcFiles, String zipFile) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(zipFile);
             ZipOutputStream zos = new ZipOutputStream(fos)) {

            byte[] buffer = new byte[1024];
            
            for (String srcFile : srcFiles) {
                File fileToZip = new File(srcFile);
                try (FileInputStream fis = new FileInputStream(fileToZip)) {
                    zos.putNextEntry(new ZipEntry(fileToZip.getName()));
                    int length;
                    while ((length = fis.read(buffer)) > 0) {
                        zos.write(buffer, 0, length);
                    }
                    zos.closeEntry();
                }
            }
        }
    }

    // Method to unzip files
    public static void unzip(String zipFile, String destFolder) throws IOException {
        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {
            ZipEntry entry;
            byte[] buffer = new byte[1024];
            while ((entry = zis.getNextEntry()) != null) {
                File newFile = new File(destFolder + File.separator + entry.getName());
                if (entry.isDirectory()) {
                    newFile.mkdirs();
                } else {
                    new File(newFile.getParent()).mkdirs();
                    try (FileOutputStream fos = new FileOutputStream(newFile)) {
                        int length;
                        while ((length = zis.read(buffer)) > 0) {
                            fos.write(buffer, 0, length);
                        }
                    }
                }
            }
        }
    }

      // Main Function
    public static void main(String[] args) {
        try {
            // Zip files
            String[] filesToZip = {"file1.txt", "file2.txt"};
            String zipFileName = "compressed.zip";
            zipFiles(filesToZip, zipFileName);
            System.out.println("Files zipped successfully.");

            // Unzip files
            String destinationFolder = "unzipped";
            unzip(zipFileName, destinationFolder);
            System.out.println("Files unzipped successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output:

Output of Zip UnZip Program

Output


Final Result Unzipped File:

Output Folders

Output Folders

Explanation of the above Program:

Article Tags :