Skip to content

Instantly share code, notes, and snippets.

@gavingt
Created January 31, 2021 12:47
Show Gist options
  • Save gavingt/2d3fd4faaf4052a6953961c7b8afdeff to your computer and use it in GitHub Desktop.
Save gavingt/2d3fd4faaf4052a6953961c7b8afdeff to your computer and use it in GitHub Desktop.
static void unzip(InputStream uriInputStream, String unzipDirectory) throws Exception {
//create target location folder if not exist
createDirIfNotExist(unzipDirectory);
ZipInputStream zipInputStream = new ZipInputStream(uriInputStream);
ZipEntry ze;
while ((ze = zipInputStream.getNextEntry()) != null) {
//create dir if required while unzipping
if (ze.isDirectory()) {
createDirIfNotExist(ze.getName());
} else {
FileOutputStream fileOutputStream = new FileOutputStream(unzipDirectory + "/" + ze.getName());
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] buffer = new byte[1024];
int read = 0;
while ((read = zipInputStream.read(buffer)) != -1) {
bufferedOutputStream.write(buffer, 0, read);
}
zipInputStream.closeEntry();
bufferedOutputStream.close();
fileOutputStream.close();
}
}
zipInputStream.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment