How to Compress a Folder Recursively in Java in Zip Format

0 Flares 0 Flares ×

In one of my last tutorials I have explained about how to use “java.util.zip” package to compress a file in zip format in Java. Using the same concept we are going to compress a folder in Java recursively. That means our program will recursively compress all files and folders in a parent folder in zip format into a single zip file.

Recursive File Compression in Zip Format Example in Java

package com.lessonslab.util.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipRecursively
{
List<String> fileList;
private static final String TARGET_ZIP_FILE = "/home/hduser/test/myfolder.zip";
private static final String SOURCE_FOLDER = "/home/hduser/test/myfolder";

ZipRecursively()
{
fileList = new ArrayList<String>();
}

public static void main(String args[])
{
ZipRecursively myApp = new ZipRecursively();
myApp.formListOfFiles(new File(SOURCE_FOLDER));
myApp.zipMe(TARGET_ZIP_FILE);
}

public void zipMe(String targetFile){

byte[] buffer = new byte[1024];

try{

FileOutputStream fos = new FileOutputStream(targetFile);
ZipOutputStream zos = new ZipOutputStream(fos);

System.out.println("Processing........");

for(String file : this.fileList){

System.out.println("Adding : " + file);
ZipEntry ze= new ZipEntry(file);
zos.putNextEntry(ze);

FileInputStream in =
new FileInputStream(SOURCE_FOLDER + File.separator + file);
int len;
while ((len = in.read(buffer)) > 0)
{
zos.write(buffer, 0, len);
}
in.close();
}
//Close all resources
zos.closeEntry();
zos.close();
System.out.println("Generated:: " + targetFile);
System.out.println("Process Completed");
}
catch(IOException ex)
{
ex.printStackTrace();
}
}

private void formListOfFiles(File mainNode)
{
//Add only files from parent folder
if(mainNode.isFile())
{
fileList.add(formZipEntries(mainNode.getAbsoluteFile().toString()));
}

if(mainNode.isDirectory())
{
String[] subNode = mainNode.list();
//Add files recursively
for(String filename : subNode)
{
formListOfFiles(new File(mainNode, filename));
}
}
}

private String formZipEntries(String file)
{
return file.substring(SOURCE_FOLDER.length()+1, file.length());
}
}

Output

Processing........
Adding : folder-1/folder-1/TestDoc-2
Adding : folder-1/folder-1/TestDoc-4
Adding : folder-1/folder-1/TestDoc-3
Adding : folder-1/folder-1/TestDoc-5
Adding : folder-1/folder-1/TestDoc-1
Adding : folder-1/TestDoc-2
Adding : folder-1/TestDoc-4
Adding : folder-1/TestDoc-3
Adding : folder-1/folder-2/TestDoc-2
Adding : folder-1/folder-2/TestDoc-4
Adding : folder-1/folder-2/TestDoc-3
Adding : folder-1/folder-2/TestDoc-5
Adding : folder-1/folder-2/TestDoc-1
Adding : folder-1/TestDoc-5
Adding : folder-1/TestDoc-1
Adding : TestDoc-2
Adding : TestDoc-4
Adding : TestDoc-3
Adding : TestDoc-5
Adding : TestDoc-1
Generated:: /home/hduser/test/myfolder.zip
Process Completed

I hope you enjoyed this tutorial and learnt how to recursively compress a directory using Java.

Guru

+Ayodhyanath Guru holds a B.Tech degree in Electrical Engineering and has worked with various prestigious clients in the IT industry and presently working as a Software Engineer. He is a part time blogger and presently authors the Jafaloo.Com blog. Being a tech enthusiast Guru likes to surf the web and blogs about interesting technical topics like How-To guides, freewares, Tutorials, Software, Gadgets, web applications etc. Apart from blogging he likes coding in Java/J2EE and PHP.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 0 Flares ×