Wednesday, June 10, 2009

To zip a file or folder using java

This article tells you the simple code to zip a file or folder. We are going to use ZipEntry and ZipOutputStream to zip the file.

The codes are as follows:

To zip a file:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.util.zip.ZipEntry;
import java.io.File;
import java.util.StringTokenizer;
import java.text.SimpleDateFormat;
import java.io.InputStream;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.zip.Deflater;

public class ZipFileZip {

ZipFileZip(){

}

public static void main(String args[]){
ZipFileZip zipFileZip = new ZipFileZip();
zipFileZip.doZipFile("Sample.txt");
}


private void doZipFile(String fileSource){
File file = new File(fileSource);
if(!file.isDirectory()){
System.out.println("A File");
String extn = fileSource.substring(fileSource.lastIndexOf(".")+1);
String zipFileDest = new String();
zipFileDest = "./HouseKeep/"+fileSource.substring(fileSource.lastIndexOf("/")+1,fileSource.lastIndexOf("."));
File f = new File(fileSource);
byte[] buffer = new byte[18024];
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileDest+"_"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".zip"));
out.setLevel(Deflater.DEFAULT_COMPRESSION);
FileInputStream in = new FileInputStream(fileSource);
out.putNextEntry(new ZipEntry(zipFileDest+"."+extn));
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
in.close();
out.close();
boolean isFileDeleted = f.delete();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
} else {
System.out.println("A File");
}
}

}


TO zip a folder:


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.util.zip.ZipEntry;
import java.io.File;
import java.util.StringTokenizer;
import java.text.SimpleDateFormat;
import java.io.InputStream;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.zip.Deflater;

public class ZipFolder {

ZipFolder(){

}

public static void main(String args[]){
ZipFolder zipFolder = new ZipFolder();
zipFolder.doZipFolder("SampleFolder");
}

private void doZipFolder(String fileSource){
File file = new File(fileSource);
String lastToken = "";
StringTokenizer strToken = new StringTokenizer(fileSource,"/");

while(strToken.hasMoreTokens()){
lastToken = strToken.nextToken();
}
File outFile = new File("./HouseKeep/"+lastToken+"_"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".zip");
System.out.println("A directory");
try{
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
BufferedInputStream in =null;
byte[] data = new byte[2048];
String files[] = file.list();
for (int i=0; i < files.length; i++) {
in = new BufferedInputStream(new FileInputStream(file.getPath() + "/" + files[i]),2048);
out.putNextEntry(new ZipEntry(files[i]));
int count;
while((count = in.read(data,0,2048)) != -1){
out.write(data, 0, count);
}
out.closeEntry();
}
out.flush();
out.close();
out.close();
in.close();
}catch(Exception e){
e.printStackTrace();
}
}

}





Thanks all. Hope you all will find it useful.

No comments:

Total Pageviews