Created
January 31, 2021 12:47
-
-
Save gavingt/2d3fd4faaf4052a6953961c7b8afdeff to your computer and use it in GitHub Desktop.
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
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