Created
May 8, 2012 09:17
-
-
Save vainolo/2633812 to your computer and use it in GitHub Desktop.
ZipExtracter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* Copyright (c) 2012 Arieh 'Vainolo' Bibliowicz | |
* You can use this code for educational purposes. For any other uses | |
* please contact me: [email protected] | |
*******************************************************************************/ | |
package com.vainolo.examples.file; | |
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.Enumeration; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipFile; | |
/** | |
* Extract a zip file to a folder. No error checking or argument validation is done. | |
* | |
* @author vainolo | |
*/ | |
public class ZipExtracter { | |
public static void openZipFile(String zipFilename, String destinationDirname) throws IOException { | |
byte[] buffer = new byte[1024]; | |
int bytesRead = 0; | |
File zipFile = new File(zipFilename); | |
File destinationDir = new File(destinationDirname); | |
ZipFile zip = new ZipFile(zipFile); | |
Enumeration<? extends ZipEntry> zipEntries = zip.entries(); | |
while (zipEntries.hasMoreElements()) { | |
ZipEntry entry = zipEntries.nextElement(); | |
if (entry.isDirectory()) { | |
File newDir = new File(destinationDir, entry.getName()); | |
newDir.mkdirs(); | |
} else { | |
BufferedInputStream inputStream = new BufferedInputStream(zip.getInputStream(entry)); | |
File outputFile = new File(destinationDir, entry.getName()); | |
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); | |
while ((bytesRead = inputStream.read(buffer)) != -1) { | |
outputStream.write(buffer, 0, bytesRead); | |
} | |
inputStream.close(); | |
outputStream.close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment