Skip to content

Instantly share code, notes, and snippets.

@ragklaat
Last active October 5, 2017 13:18
Show Gist options
  • Save ragklaat/41846e43695a162922cf to your computer and use it in GitHub Desktop.
Save ragklaat/41846e43695a162922cf to your computer and use it in GitHub Desktop.
Download and unzip files directly without saving zipped file
// warning: this uses only core Java tools and can unzip .zip files only
// warning: you should care about exceptions. Seriously.
private static boolean downloadAndUnzip(String downloadFileUrl,
String outputFile) throws MalformedURLException, IOException,
URISyntaxException {
byte[] buffer = new byte[1024];
ZipInputStream zis = new ZipInputStream(new URI(downloadFileUrl)
.toURL().openStream());
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
FileOutputStream fos = new FileOutputStream(outputFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment