- Given an input directory containing files and sub-directories.
- We would like to compress or zip all files present in a input directory.
- We will traverse input directory recursively to compress all files.
- Java framework has provided couple of classes to zip a file(s).
- ZipOutputStream: ZipOutputStream class implements an output stream filter for writing files in ZIP file format.
- ZipEntry: ZipEntry class represents an entry of ZIP file.
1. Program flow: Zip input directory (sub-directories)
- Suppose, we would like to zip input directory containing files and sub-directories.
- We will traverse the input directory recursively and we will create a list of all files.
- We will create output zip files using ZipOutputStream.
- We will traverse list of files and compress each file.
- Each compress file will be added to ZipOutputStream.
- At the end of traversal, we will get zipped output file.
2. Code: compress input directory recursively – ZipOutputStream (java)
package org.learn;
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 ZipFilesInDirectoryRecursively {
public static void main(String[] args) throws IOException {
File inputDirectory = new File("C:\\InputDirectory");
File outputZip = new File("C:\\output\\zipDirectory.zip");
outputZip.getParentFile().mkdirs();
List listFiles = new ArrayList();
//Populate all files...iterate through directories/subdirectories...
//recursively
System.out.printf("1. Input directory %s has following files:\n",
inputDirectory.getCanonicalPath());
listFiles(listFiles, inputDirectory);
//Create zip output stream to zip files
ZipOutputStream zipOutputStream = new ZipOutputStream(
new FileOutputStream(outputZip));
//Create zip files by recursively iterating through directories
//and sub directories....
System.out.println("\n2. Output Zipped file at location:"+outputZip.getCanonicalPath());
createZipFile(listFiles, inputDirectory, zipOutputStream);
}
private static void createZipFile(List listFiles, File inputDirectory,
ZipOutputStream zipOutputStream) throws IOException {
for (File file : listFiles) {
String filePath = file.getCanonicalPath();
int lengthDirectoryPath = inputDirectory.getCanonicalPath().length();
int lengthFilePath = file.getCanonicalPath().length();
//Get path of files relative to input directory.
String zipFilePath = filePath.substring(lengthDirectoryPath + 1, lengthFilePath);
ZipEntry zipEntry = new ZipEntry(zipFilePath);
zipOutputStream.putNextEntry(zipEntry);
FileInputStream inputStream = new FileInputStream(file);
byte[] bytes = new byte[1024];
int length;
while ((length = inputStream.read(bytes)) >= 0) {
zipOutputStream.write(bytes, 0, length);
}
zipOutputStream.closeEntry();
System.out.println("Zipped file:"+filePath);
}
zipOutputStream.close();
}
//Get list of all files recursively by iterating through sub directories
private static List listFiles(List listFiles, File inputDirectory)
throws IOException {
File[] allFiles = inputDirectory.listFiles();
for (File file : allFiles) {
if (file.isDirectory()) {
listFiles(listFiles, file);
} else {
System.out.println(file.getCanonicalPath());
listFiles.add(file);
}
}
return listFiles;
}
}
3. Output – zipped input directory – ZipOutputStream in java
4. Output – zipped input directory – ZipOutputStream in java
1. Input directory C:\InputDirectory has following files:
C:\InputDirectory\Dir1\Dir11\Dir11_F1.txt
C:\InputDirectory\Dir1\Dir11\Dir11_F2.txt
C:\InputDirectory\Dir1\Dir1_F1.txt
C:\InputDirectory\Dir1\Dir1_F2.txt
C:\InputDirectory\Dir2\Dir21\Dir21_F1.txt
C:\InputDirectory\Dir2\Dir21\Dir21_F2.txt
C:\InputDirectory\Dir2\Dir2_F1.txt
C:\InputDirectory\Dir2\Dir2_F2.txt
C:\InputDirectory\FirstFile.txt
C:\InputDirectory\SecondFile.txt
C:\InputDirectory\ThirdFile.txt
2. Output Zipped file at location:C:\output\zipDirectory.zip
Zipped file:C:\InputDirectory\Dir1\Dir11\Dir11_F1.txt
Zipped file:C:\InputDirectory\Dir1\Dir11\Dir11_F2.txt
Zipped file:C:\InputDirectory\Dir1\Dir1_F1.txt
Zipped file:C:\InputDirectory\Dir1\Dir1_F2.txt
Zipped file:C:\InputDirectory\Dir2\Dir21\Dir21_F1.txt
Zipped file:C:\InputDirectory\Dir2\Dir21\Dir21_F2.txt
Zipped file:C:\InputDirectory\Dir2\Dir2_F1.txt
Zipped file:C:\InputDirectory\Dir2\Dir2_F2.txt
Zipped file:C:\InputDirectory\FirstFile.txt
Zipped file:C:\InputDirectory\SecondFile.txt
Zipped file:C:\InputDirectory\ThirdFile.txt