Creating a zip file of a folder

This post explains how to create a zip file of a folder (also containing sub folders)

The below image shows the hierarchy of the folder

package ZipDemo;

import java.nio.file.Path;
import java.nio.file.Paths;

public class ZipDemo2 {
    private File startingFolder;

    public ZipDemo2(File startingFolder) {
        this.startingFolder = startingFolder;

    public static void main(String[] args) throws FileNotFoundException, IOException {
        File folder = new File("E:\\Projects\\JavaProjects\\JavaConcepts\\Folder1");
        ZipDemo2 zipDemo2 = new ZipDemo2(folder);
        try(FileOutputStream fileOutputStream = new FileOutputStream("E:\\Projects\\JavaProjects\\JavaConcepts\\WorkFolder\\");
        ZipOutputStream zipFileOutputStream = new ZipOutputStream(fileOutputStream);) {
  , null, zipFileOutputStream);

    public void zip(File folder, ZipEntry zipEntry, ZipOutputStream zipFileOutputStream) throws FileNotFoundException, IOException {
        for(File file : folder.listFiles()) {
            ZipEntry entry = null;
            if(file.isDirectory()) {
                Path path1 = Paths.get(startingFolder.getAbsolutePath());
                Path path2 = Paths.get(file.getAbsolutePath());
                Path path = path1.relativize(path2);
                entry = new ZipEntry(path + "/");
                zip(file, entry, zipFileOutputStream);
            } else {
                if(zipEntry != null) {
                    entry = new ZipEntry(zipEntry.getName() + file.getName());
                } else {
                    entry = new ZipEntry(file.getName());
                try(FileInputStream fileInputStream = new FileInputStream(file)) {
                    byte[] data = new byte[1024];
                    int bytesRead =;
                    while(bytesRead != -1) {
                    zipFileOutputStream.write(data, 0, bytesRead);
                        bytesRead =;


In the above code if block adds information about a folder and else block zips the file in a folder. Whether it is folder or file the steps are as mentioned below
1) Create a ZipOutputStream
2) Create a instance of ZipEntry for each entry (whether file or folder) to be added in the zip file.
3) Call zipFileOutputStream.putNextEntry to add the entry in the zip file.
4) If it is file write to the zip file using the zipFileOutputStream and then close it by calling zipFileOutputStream.closeEntry
5) If it is folder just call zipFileOutputStream.closeEntry

In the above code zip method is called recursively. To indicate that a ZipEntry represents a directory we append “/” to the name used to create a zip entry as show below
entry = new ZipEntry(path + “/”);


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s