Created
December 13, 2015 16:18
-
-
Save crgarridos/6611af5120801ebf7671 to your computer and use it in GitHub Desktop.
Best way to uncompress .zip file in android (http://stackoverflow.com/questions/4504291/how-to-speed-up-unzipping-time-in-java-android)
This file contains hidden or 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
public void unzip() { | |
try { | |
FileInputStream inputStream = new FileInputStream(filePath); | |
ZipInputStream zipStream = new ZipInputStream(inputStream); | |
ZipEntry zEntry = null; | |
while ((zEntry = zipStream.getNextEntry()) != null) { | |
Log.d("Unzip", "Unzipping " + zEntry.getName() + " at " | |
+ destination); | |
if (zEntry.isDirectory()) { | |
hanldeDirectory(zEntry.getName()); | |
} else { | |
FileOutputStream fout = new FileOutputStream( | |
this.destination + "/" + zEntry.getName()); | |
BufferedOutputStream bufout = new BufferedOutputStream(fout); | |
byte[] buffer = new byte[1024]; | |
int read = 0; | |
while ((read = zipStream.read(buffer)) != -1) { | |
bufout.write(buffer, 0, read); | |
} | |
zipStream.closeEntry(); | |
bufout.close(); | |
fout.close(); | |
} | |
} | |
zipStream.close(); | |
Log.d("Unzip", "Unzipping complete. path : " + destination); | |
} catch (Exception e) { | |
Log.d("Unzip", "Unzipping failed"); | |
e.printStackTrace(); | |
} | |
} | |
public void hanldeDirectory(String dir) { | |
File f = new File(this.destination + dir); | |
if (!f.isDirectory()) { | |
f.mkdirs(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment