Created
March 30, 2012 19:16
-
-
Save huljas/2254197 to your computer and use it in GitHub Desktop.
Little unzip utility
This file contains 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
import org.apache.commons.io.FileUtils; | |
import org.apache.commons.io.IOUtils; | |
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipFile; | |
/** | |
* Simple unzip utility. | |
* | |
* @author huljas | |
*/ | |
public class Unzip { | |
/** | |
* Unzips all files in the zipFile into the targetDir and returns the unzipped files. | |
*/ | |
public static List<File> unzip(File zipFile, File targetDir) throws IOException { | |
List<File> files = new ArrayList<File>(); | |
ZipFile zip = new ZipFile(zipFile); | |
try { | |
zip = new ZipFile(zipFile); | |
for (ZipEntry entry : Collections.list(zip.entries())) { | |
InputStream input = zip.getInputStream(entry); | |
try { | |
if (!targetDir.exists()) targetDir.mkdirs(); | |
File target = new File(targetDir, entry.getName()); | |
FileUtils.copyInputStreamToFile(input, target); | |
files.add(target); | |
} finally { | |
IOUtils.closeQuietly(input); | |
} | |
} | |
return files; | |
} finally { | |
zip.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! Neat and simple :-)