Skip to content

Instantly share code, notes, and snippets.

@johnkil
Last active December 9, 2015 23:38
Show Gist options
  • Save johnkil/4345139 to your computer and use it in GitHub Desktop.
Save johnkil/4345139 to your computer and use it in GitHub Desktop.
Implementation of two versions of the utilities to decompress zip archives (ZipInputStream & ZipFile).
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
*
* The class is responsible for unpacking ZIP file. The implementation is based on {@link ZipFile}.
*
* @author johnkil
*
*/
public class ZipFileUtils {
private static final String LOG_TAG = ZipFileUtils.class.getSimpleName();
private static final int BUFFER_SIZE = 8 * 1024;
/**
* Unpack ZIP archive to specified directory.
*
* @param srcFile - ZIP file
* @param outputDir - destination directory
* @return true in case of success, otherwise - false.
*/
public static boolean unzipFile(File srcFile, File outputDir) {
Log.d(LOG_TAG, "Unpacking %s to %s", srcFile, outputDir);
boolean result = false;
ZipFile zipFile = null;
try {
zipFile = new ZipFile(srcFile);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) entries.nextElement();
unzipEntry(zipEntry, zipFile, outputDir);
}
result = true;
} catch (IOException e) {
Log.e(LOG_TAG, e);
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException e) {
Log.e(LOG_TAG, "Unable to close zip-archive", e);
}
}
}
return result;
}
/**
* Unzip entry from ZIP file into outputDir.
*
* @param zipEntry Specify entry from ZIP file
* @param zipFile ZIP file
* @param outputDir Destination directory
* @throws IOException
*/
private static void unzipEntry(ZipEntry zipEntry, ZipFile zipFile, File outputDir) throws IOException {
File file = new File(outputDir, zipEntry.getName());
if (zipEntry.isDirectory()) {
// unzip directory
if (!file.exists()) {
if (file.mkdirs()) {
Log.d(LOG_TAG, "%s directory created", file);
} else {
throw new IOException(String.format("%s failure to create directory", file));
}
} else {
Log.w(LOG_TAG, "%s directory is already created", file);
}
} else {
// unzip file
BufferedOutputStream out = null;
BufferedInputStream in = null;
try {
out = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE);
in = new BufferedInputStream(zipFile.getInputStream(zipEntry), BUFFER_SIZE);
FileUtils.copyFile(in, out);
out.flush();
Log.d(LOG_TAG, "%s file created", file);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.e(LOG_TAG, e);
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
Log.e(LOG_TAG, e);
}
}
}
}
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* The class is responsible for unpacking ZIP file. The implementation is based on {@link ZipInputStream}.
*
* @author johnkil
*
*/
public class ZipUtils {
private static final String LOG_TAG = ZipUtils.class.getSimpleName();
private static final int BUFFER_SIZE = 8 * 1024;
/**
* Invisible constructor.
*/
private ZipUtils() {
}
/**
* UNZIP file into outputDir.
*
* @param zipFile The source ZIP file
* @param outputDir Destination directory
* @return true if successful, else false
*/
public static boolean unzip(File zipFile, File outputDir) {
Log.d(LOG_TAG, "Unpacking %s to %s", zipFile, outputDir);
try {
return unzip(new FileInputStream(zipFile), outputDir);
} catch (FileNotFoundException e) {
Log.e(LOG_TAG, "%s file does not exist", zipFile);
}
return false;
}
/**
* UNZIP InputStream into outputDir.
*
* @param in The source zipped {@link InputStream}
* @param outputDir Destination directory
* @return true if successful, else false
*/
public static boolean unzip(InputStream in, File outputDir) {
Log.v(LOG_TAG, "unzip() called: outputDir=[%s]", outputDir.getPath());
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(in, BUFFER_SIZE));
try {
if (outputDir.exists() && !outputDir.isDirectory()) {
Log.e(LOG_TAG, "outputDir [%s] is not the directory", outputDir.getPath());
return false;
} else if (!outputDir.exists() && !outputDir.mkdirs()) {
Log.e(LOG_TAG, "fail of creating outputDir [%s]", outputDir.getPath());
return false;
}
ZipEntry zipEntry = null;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
unzipEntry(zipEntry, zipInputStream, outputDir);
}
return true;
} catch (IOException e) {
Log.w(LOG_TAG, e);
} finally {
try {
zipInputStream.close();
} catch (IOException e) {
Log.w(LOG_TAG, e);
}
}
return false;
}
/**
* UNZIP entry from ZIP file into outputDir.
*
* @param zipEntry Specify entry from ZIP file
* @param zipInputStream The source {@link ZipInputStream}
* @param outputDir Destination directory
* @throws IOException
*/
private static void unzipEntry(ZipEntry zipEntry, ZipInputStream zipInputStream, File outputDir) throws IOException {
Log.v(LOG_TAG, "unzipEntry() called: zipEntry=[%s], outputDir=[%s]", zipEntry, outputDir);
File file = new File(outputDir, zipEntry.getName());
if (zipEntry.isDirectory()) {
// unzip directory
if (!file.exists()) {
if (file.mkdirs()) {
Log.d(LOG_TAG, "%s directory created", file);
} else {
throw new IOException(String.format("%s failure to create directory", file));
}
} else {
Log.w(LOG_TAG, "%s directory is already created", file);
}
} else {
// unzip file
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE);
FileUtils.copyFile(zipInputStream, out);
out.flush();
Log.d(LOG_TAG, "%s file created", file);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
Log.e(LOG_TAG, e);
}
}
try {
zipInputStream.closeEntry();
} catch (IOException e) {
Log.w(LOG_TAG, e);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment