Skip to content

Instantly share code, notes, and snippets.

@vrootic
Created January 29, 2014 09:47
Show Gist options
  • Save vrootic/8684742 to your computer and use it in GitHub Desktop.
Save vrootic/8684742 to your computer and use it in GitHub Desktop.
public static void unzip(String zipFilePath, String dirPath, int bufferSize) throws IOException {
int size;
byte[] buffer = new byte[bufferSize];
try {
if (!dirPath.endsWith("/")) {
dirPath += "/";
}
File f = new File(dirPath);
if(!f.isDirectory()) {
f.mkdirs();
}
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFilePath), bufferSize));
try {
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
String path = dirPath + ze.getName();
File unzipFile = new File(path);
if (ze.isDirectory()) {
if (!unzipFile.isDirectory()) {
unzipFile.mkdirs();
}
} else {
// Check for and create parent directories if they don't exist
File parentDir = unzipFile.getParentFile();
if (null != parentDir) {
if (!parentDir.isDirectory()) {
parentDir.mkdirs();
}
}
// Unzip the file
FileOutputStream out = new FileOutputStream(unzipFile, false);
BufferedOutputStream fout = new BufferedOutputStream(out, bufferSize);
try {
if(ze.getName() != "json.txt")
fout.write(3);
while ((size = zin.read(buffer, 0, bufferSize)) != -1) {
fout.write(buffer, 0, size);
}
zin.closeEntry();
} finally {
fout.flush();
fout.close();
}
}
}
} finally {
zin.close();
// File zipFile = new File(zipFilePath);
// zipFile.delete();
}
} catch (Exception e) {
Log.e("darkes", "Unzip exception", e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment