You must have zipped and unzipped files using utilities such as Win zip or 7-zip, probably a million times, but what if you needed to do the zipping (or unzipping) from inside your Java application. For one of my projects I had do precisely that. It is no big deal but there are so many libraries to choose from, so it can get a bit confusing. In particular, I was looking for a tool which was
- Able to compress and decompress easily (duh...)
- Allows different levels of compression
- Allows folders to be compressed
- Allows multiple files to be compressed
- Allows encryption
// DEFLATE_LEVEL_ULTRA = maximum compression
// DEFLATE_LEVEL_MAXIMUM
// DEFLATE_LEVEL_NORMAL = normal compression
// DEFLATE_LEVEL_FAST
// DEFLATE_LEVEL_FASTEST = fastest compression
It support AES encryption with the key strengths of 128, 192 and 256 (and of course you could set a password !). Just to show how easy it is to use, I am attaching a example code which compresses a single file, outputs the compression ratio and again decompresses it. (Please note that you may have change the file names/ paths)
The compressing function
private static void compress(String inputFile,String compressedFile) {
try {
ZipFile zipFile = new ZipFile(compressedFile);
File inputFileH = new File(inputFile);
ZipParameters parameters = new ZipParameters();
// COMP_DEFLATE is for compression
// COMp_STORE no compression
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
// DEFLATE_LEVEL_ULTRA = maximum compression
// DEFLATE_LEVEL_MAXIMUM
// DEFLATE_LEVEL_NORMAL = normal compression
// DEFLATE_LEVEL_FAST
// DEFLATE_LEVEL_FASTEST = fastest compression
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_ULTRA);
// file compressed
zipFile.addFile(inputFileH, parameters);
long uncompressedSize = inputFileH.length();
File outputFileH = new File(compressedFile);
long comrpessedSize = outputFileH.length();
//System.out.println("Size "+uncompressedSize+" vs "+comrpessedSize);
double ratio = (double)comrpessedSize/(double)uncompressedSize;
System.out.println("File compressed with compression ratio : "+ ratio);
} catch (Exception e) {
e.printStackTrace();
}
}
The decompressing function
public static void decompress(String compressedFile,String destination) {
try {
ZipFile zipFile = new ZipFile(compressedFile);
if (zipFile.isEncrypted()) {
//zipFile.setPassword(password);
}
zipFile.extractAll(destination);
} catch (ZipException e) {
e.printStackTrace();
}
System.out.println("File Decompressed");
}
And the main function
public static void main(String[] args) {
String filePath = ADD THE FILE PATH
String fileName = ADD THE FILENAME
String inputFile = filePath + fileName;
Calendar calendar = Calendar.getInstance();
long millisecs = calendar.getTime().getTime();
// compressed file name
String compressedFile = inputFile + "_" + millisecs + ".zip";
//invoke file compression
compress(inputFile,compressedFile);
millisecs = calendar.getTime().getTime();
String destination = filePath + "decompress";
decompress(compressedFile,destination);
}
The decompressing function
public static void decompress(String compressedFile,String destination) {
try {
ZipFile zipFile = new ZipFile(compressedFile);
if (zipFile.isEncrypted()) {
//zipFile.setPassword(password);
}
zipFile.extractAll(destination);
} catch (ZipException e) {
e.printStackTrace();
}
System.out.println("File Decompressed");
}
And the main function
public static void main(String[] args) {
String filePath = ADD THE FILE PATH
String fileName = ADD THE FILENAME
String inputFile = filePath + fileName;
Calendar calendar = Calendar.getInstance();
long millisecs = calendar.getTime().getTime();
// compressed file name
String compressedFile = inputFile + "_" + millisecs + ".zip";
//invoke file compression
compress(inputFile,compressedFile);
millisecs = calendar.getTime().getTime();
String destination = filePath + "decompress";
decompress(compressedFile,destination);
}
Also you would need to add the following libraries to the top and add the zip4j jar to your build path.
import java.io.File;
import java.util.Calendar;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
Happy (Un)zipping ;-
import java.io.File;
import java.util.Calendar;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
Happy (Un)zipping ;-