How to Unzip Files Recursively in Java

Java: Extracting zip file with multiple subdirectories

Here is a class unzipping files from a zip file and recreating the directory tree as well.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ExtractZipContents {

public static void main(String[] args) {

try {
// Open the zip file
ZipFile zipFile = new ZipFile("Meow.zip");
Enumeration<?> enu = zipFile.entries();
while (enu.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) enu.nextElement();

String name = zipEntry.getName();
long size = zipEntry.getSize();
long compressedSize = zipEntry.getCompressedSize();
System.out.printf("name: %-20s | size: %6d | compressed size: %6d\n",
name, size, compressedSize);

// Do we need to create a directory ?
File file = new File(name);
if (name.endsWith("/")) {
file.mkdirs();
continue;
}

File parent = file.getParentFile();
if (parent != null) {
parent.mkdirs();
}

// Extract the file
InputStream is = zipFile.getInputStream(zipEntry);
FileOutputStream fos = new FileOutputStream(file);
byte[] bytes = new byte[1024];
int length;
while ((length = is.read(bytes)) >= 0) {
fos.write(bytes, 0, length);
}
is.close();
fos.close();

}
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

Source : http://www.avajava.com/tutorials/lessons/how-do-i-unzip-the-contents-of-a-zip-file.html

How to use ZipFile Class in Java to recursively open all files including those under Folders

Please take a look on here and here

public static void unzip(final ZipFile zipfile, final File directory)
throws IOException {

final Enumeration<? extends ZipEntry> entries = zipfile.entries();
while (entries.hasMoreElements()) {
final ZipEntry entry = entries.nextElement();
final File file = file(directory, entry);
if (entry.isDirectory()) {
continue;
}
final InputStream input = zipfile.getInputStream(entry);
try {
// copy bytes from input to file
} finally {
input.close();
}
}
}
protected static File file(final File root, final ZipEntry entry)
throws IOException {

final File file = new File(root, entry.getName());

File parent = file;
if (!entry.isDirectory()) {
final String name = entry.getName();
final int index = name.lastIndexOf('/');
if (index != -1) {
parent = new File(root, name.substring(0, index));
}
}
if (parent != null && !parent.isDirectory() && !parent.mkdirs()) {
throw new IOException(
"failed to create a directory: " + parent.getPath());
}

return file;
}

Uzip folders recursively -android

Ohh Yes! I have solved it.. :)

I have written following function that will create sub-folders recursively if required.

This is tried and tested function that will successfully unzips any file.

    /**
* Unzips the file (recursively creates sub-folder if exists.)
*
* @param tempFileName
* The zip file.
* @param destinationPath
* The destination path where unzipped file will be saved.
*/
public void unzipFile(String tempFileName, String destinationPath) {
try {

int index = destinationPath.lastIndexOf("\\");
String fileString = destinationPath.substring(index);

File extFile = new File("/mnt/sdcard/courses1", fileString);
if(!extFile.exists()) {
createDir(extFile);
}

byte[] buffer = new byte[1024];

FileInputStream fin = new FileInputStream(tempFileName);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry zipentry = null;
if (!(zin.available() == 0)) {
byte[] startBuffer = new byte[8];

while ((zipentry = zin.getNextEntry()) != null) {
String zipName = zipentry.getName();
if (zipName.startsWith("/")) {
zipName = zipentry.getName();
} else if (zipName.startsWith("\\")) {
zipName = zipentry.getName();
} else {
zipName = "/" + zipentry.getName();
}

String fileName = destinationPath + zipName;
fileName = fileName.replace("\\", "/");
fileName = fileName.replace("//", "/");

if (zipentry.isDirectory()) {
createDir(new File(fileName));
continue;
}

String name = zipentry.getName();
int start, end = 0;
while (true) {

start = name.indexOf('\\', end);
end = name.indexOf('\\', start + 1);
if (start > 0)
"check".toString();
if (end > start && end > -1 && start > -1) {
String dir = name.substring(1, end);

createDir(new File(destinationPath + '/' + dir));
// name = name.substring(end);
} else
break;
}

File file = new File(fileName);

FileOutputStream tempDexOut = new FileOutputStream(file);
int BytesRead = 0;

if (zipentry != null) {
if (zin != null) {
while ((BytesRead = zin.read(buffer)) != -1) {
tempDexOut.write(buffer, 0, BytesRead);
}
tempDexOut.close();
}
}
}
}

} catch (Exception e) {
Log.e("Exception", e.getMessage());
}
}

I hope it helps someone. :)

Thank you.

How do I recursively unzip nested ZIP files?

Thanks Cyrus! The master wizard Shawn J. Goff had the perfect script for this:

while [ "`find . -type f -name '*.zip' | wc -l`" -gt 0 ]; do find -type f -name "*.zip" -exec unzip -- '{}' \; -exec rm -- '{}' \;; done


Related Topics



Leave a reply



Submit