Building a utility like Winzip is easy in Java, thanks to java.util.zip package.
Basically we need to open a ZipOutputStream which is created on top of FileOutputStream, then loop through all the files in the folder. While looping, get the handle each of the file using FileInputStream and create a ZipEntry for each file. Then read the byte chucks of that file and write it to ZipOutputStream opened earlier. Once done writing all the files, close the flush and finally close the output stream. In result a zipped file will be created will all the files from the folder to be zipped.
<!--break-->
package com.livrona.snippets.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* This snippet how to zip a folder and its contents to a output file.
*
* @author java4learners
*
*/
publicclass ZipFolder {
// buffer size
privatestaticfinalint BUFFER =1024;
/**
* Zips the folder contents into a file
*
* @param inFolder
* @param outFile
* @return
*/
public File zipFolder(File inFolder, File outFile) {
try {
// zipped file out stream
ZipOutputStream out =new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream(outFile)));
byte[] data =newbyte[BUFFER];
// get the list of files
String files[] = inFolder.list();
// loop through each
for (int i =0; i < files.length; i++) {
System.out.println("Adding file : "+ files[i]);
// get the input stream of the file
BufferedInputStream in =new BufferedInputStream(
new FileInputStream(inFolder.getPath() +"/"+ files[i]),
BUFFER);
// add the file header info (name, size) etc. to the zip stream
out.putNextEntry(new ZipEntry(files[i]));
int count;
// read the write input file to zip stream
while ((count = in.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
out.closeEntry(); // close entry
cleanUp(in); // close input stream
} // move to next file
cleanUp(out); // close zip stream
} catch (Exception e) {
e.printStackTrace();
}
// return te handle to the output file
returnnew File(outFile +".zip");
}
/**
* Close the Input File Stream
*
* @param in
* @throws Exception
*/
privatevoid cleanUp(InputStream in) throws Exception {
in.close();
}
/**
* Close the output file Stream
*
* @param out
* @throws Exception
*/
privatevoid cleanUp(OutputStream out) throws Exception {
out.flush();
out.close();
}
/**
* Test Method
*
* @param args
*/
publicstaticvoid main(String[] args) {
if (args.length !=2) {
System.out
.println("Usage: java com.livrona.snippets.io.ZipFolder <Path to your folder to zip> <Path to output zip file>");
System.exit(0);
}
// store args
String folder = args[0];
String zipfile = args[1];
System.out.println("Zip File : "+ zipfile);
System.out.println("Output File : "+ folder);
// invoke the folder zipper
try {
ZipFolder folderZipper =new ZipFolder();
folderZipper.zipFolder(new File(folder), new File(zipfile));
System.out.println("Folder zipped successfully");
} catch (Exception e) {
System.out.println("Failed to zip the folder : "+ e.getMessage());
e.printStackTrace();
}
}
}