Creating a file archiver in zip format

This post explains how to create file archiver in WinZip format. WinZip is a file archiver and compression tool. Whenever a zip file is created by default the file is compressed and archived together in a single file.

We can change the default behaviour (i.e., compression) by setting the level property in ZipOutputStream. The below gives you an example of it

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.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipDemo11 {
    private File startingFolder;
    
    public ZipDemo11(File startingFolder) {
        this.startingFolder = startingFolder;
    }
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        File folder = new File("E:\\Projects\\JavaProjects\\JavaConcepts\\WorkFolder");
        ZipDemo2 zipDemo2 = new ZipDemo2(folder);
        try(FileOutputStream fileOutputStream = new FileOutputStream("E:\\Projects\\JavaProjects\\JavaConcepts\\WorkFolder1.zip");
            ZipOutputStream zipFileOutputStream = new ZipOutputStream(fileOutputStream);) {
            zipFileOutputStream.setLevel(ZipOutputStream.STORED);
            zipDemo2.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.closeEntry();
                    fileInputStream.close();
                }
            }
        }
    }
}

In the above code I changed the default behavior by using the below code snippet


zipFileOutputStream.setLevel(ZipOutputStream.STORED);

By default level value will be "DEFLATE".

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s