Monday, July 6, 2009

how to unzip file in java; Java Unzip file,

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class MakeUnZip {
public static void main(String argv[]) {
String zipFileName = "src\\myZip.zip";
unZip(zipFileName);
}

public static void unZip(String zipFileName) {
int BUFFER = 2048;
try {
BufferedOutputStream dest = null;
FileInputStream fileInputStream = new FileInputStream(zipFileName);
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
ZipEntry zipEntry;
int count=0;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
System.out.println("Extracting File Name :: " + zipEntry);
count++; int length;
byte data[] = new byte[BUFFER];
FileOutputStream fileOutputStream = new FileOutputStream(zipEntry.getName());
dest = new BufferedOutputStream(fileOutputStream, BUFFER);
while ((length = zipInputStream.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, length);
}

dest.flush();
dest.close();
}

zipInputStream.close();
System.out.println("Total "+count+ " Files Unziped Successfully ");
} catch (Exception e) { e.printStackTrace(); }
}
}

No comments:

Post a Comment

You can put your comments here (Either feedback or your Question related to blog)