Last active
October 5, 2017 13:18
-
-
Save ragklaat/41846e43695a162922cf to your computer and use it in GitHub Desktop.
Download and unzip files directly without saving zipped file
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
// 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