Creating a zip file of a folder with CRC32 checksum

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

The below code is similar to the previous post “Creating zip file of a folder” with minor change in main method.

The main method code snippet from the previous post as shown below


File folder = new File("E:\\Projects\\JavaProjects\\JavaConcepts\\Folder1");
ZipDemo2 zipDemo2 = new ZipDemo2(folder);
try(FileOutputStream fileOutputStream = new FileOutputStream("E:\\Projects\\JavaProjects\\JavaConcepts\\WorkFolder\\Folder1.zip");
ZipOutputStream zipFileOutputStream = new ZipOutputStream(fileOutputStream);) {
    zipDemo2.zip(folder, null, zipFileOutputStream);
}

The new code snippet will be as shown below


File folder = new File("E:\\Projects\\JavaProjects\\JavaConcepts\\Folder1");
ZipDemo5 zipDemo5 = new ZipDemo5(folder);
try(FileOutputStream fileOutputStream = new FileOutputStream("E:\\Projects\\JavaProjects\\JavaConcepts\\WorkFolder\\Folder1.zip");
    CheckedOutputStream checkedOutputStream = new CheckedOutputStream(fileOutputStream, new CRC32());
    ZipOutputStream zipFileOutputStream = new ZipOutputStream(checkedOutputStream);) {
    zipDemo5.zip(folder, null, zipFileOutputStream);
}

The difference being inclusion of new class CheckedOutputStream in between the ZipOutputStream and FileOutputStream. As a result any data written to ZipOutputStream will pass through CheckedOutputStream where a checksum is created and updated as the data is being sent to ZipOutputStream. When the ZipOutputStream flushes the data, the final checksum calculated is also flushed to FileOutputStream resulting in storage of checksum along with the compressed file.

Below is the complete code

Main code


package ZipDemo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipDemo5 {
    private File startingFolder;
    
    public ZipDemo5(File startingFolder) {
        this.startingFolder = startingFolder;
    }
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        File folder = new File("E:\\Projects\\JavaProjects\\JavaConcepts\\WorkFolder");
        ZipDemo5 zipDemo5 = new ZipDemo5(folder);
        try(FileOutputStream fileOutputStream = new FileOutputStream("E:\\Projects\\JavaProjects\\JavaConcepts\\WorkFolder.zip");
            CheckedOutputStream checkedOutputStream = new CheckedOutputStream(fileOutputStream, new CRC32());
            ZipOutputStream zipFileOutputStream = new ZipOutputStream(checkedOutputStream);) {
            zipDemo5.zip(folder, 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 + "/");
                zipFileOutputStream.putNextEntry(entry);
                zip(file, entry, zipFileOutputStream);
                zipFileOutputStream.closeEntry();
            } else {
                if(zipEntry != null) {
                    entry = new ZipEntry(zipEntry.getName() + file.getName());
                } else {
                    entry = new ZipEntry(file.getName());
                }
                zipFileOutputStream.putNextEntry(entry);
                try(FileInputStream fileInputStream = new FileInputStream(file)) {
                    byte[] data = new byte[1024];
                    int bytesRead = fileInputStream.read(data);
                    while(bytesRead != -1) {
                        zipFileOutputStream.write(data, 0, bytesRead);
                        bytesRead = fileInputStream.read(data);
                    }
                    zipFileOutputStream.flush();
                    zipFileOutputStream.closeEntry();
                    fileInputStream.close();
                }
            }
        }
    }
}

Leave a Reply