import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.zip.ZipEntry;import java.util.zip.ZipInputStream;/*** This utility decompresses a standard zip file to a destination directory.* @author Ha Minh Nam**/public class UnzipUtil {/*** A constants for buffer size used to read/write data*/private static final int BUFFER_SIZE = 4096;/**** @param zipFilePath Path of the zip file to be extracted* @param destDirectory Path of the destination directory* @throws IOException IOException if any I/O error occurred*/public void decompressFile(String zipFilePath, String destDirectory) throws IOException {File destDir = new File(destDirectory);if (!destDir.exists()) {destDir.mkdir();}ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));ZipEntry entry = zipIn.getNextEntry();while (entry != null) {String filePath = destDirectory + File.separator + entry.getName();if (!entry.isDirectory()) {extractFile(zipIn, filePath);} else {File dir = new File(filePath);dir.mkdir();}zipIn.closeEntry();entry = zipIn.getNextEntry();}zipIn.close();}/*** Extracts a single file* @param zipIn the ZipInputStream* @param filePath Path of the destination file* @throws IOException if any I/O error occurred*/private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));byte[] bytesIn = new byte[BUFFER_SIZE];int read = 0;while ((read = zipIn.read(bytesIn)) != -1) {bos.write(bytesIn, 0, read);}bos.close();}public static void main(String args[]) throws IOException{String zipFilePath = "C:\\Users\\Home\\Downloads\\zip\\BasicStrust2ProjectEclipse.zip";String destFilePath = "C:\\Users\\Home\\Downloads\\zip\\output";UnzipUtil unzipper = new UnzipUtil();unzipper.decompressFile(zipFilePath, destFilePath);}}
Thursday, November 8, 2012
Decompress the zip file in java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment