Skip to content

Instantly share code, notes, and snippets.

@huljas
Created March 30, 2012 19:16
Show Gist options
  • Save huljas/2254197 to your computer and use it in GitHub Desktop.
Save huljas/2254197 to your computer and use it in GitHub Desktop.
Little unzip utility
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();
}
}
}
@pcholakov
Copy link

Thank you! Neat and simple :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment