Saturday, February 9, 2019

Read ZipEntry form ZipInputStream as InputStream

To read ZipEntry from ZipInputStream

public class ZipEntityDemo {
public InputStream extract(ZipInputStream zis, String entityName) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ZipEntry ze = null;
do {
ze = zis.getNextEntry();
if (ze != null && ze.getName().equalsIgnoreCase(entityName)) {
byte[] buffer = new byte[1];
while (zis.available() == 1) {
zis.read(buffer);
out.write(buffer, 0, 1);
}
out.close();
return new ByteArrayInputStream(out.toByteArray());
}
} while (ze != null);
return null;
}
}
Here zis.getNextEntry() will return us the beginning of an entry. If the name of the entry match to our desired name we can start reading. Here we are reading byte by byte in a loop till last byte. We could have improved by using ze.getSize(), and we could read multiple bytes till the size. But the getSize() methods some time returns  -1 for some zipfiles. So it wouldn't be reliable. With out knowing the entry size only way is to read byte by byte till last.

Sunday, January 13, 2019

How to Unzip in Java

Here is a quick java program to unzip or explode a zip file. We will read the zip file with the help of java.util.ZipFile, then we will process each entries inside this. We will read the entries as stream and write to a buffer and eventually to a file output stream.  So at the end we will extract all the elements in the zip, and maintain the same hierarchical structure of directories .Please refer the below example.

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 UnZipDemo {
public static void main(String args[]) throws IOException
{
String zipFileName = "D:\\demo.zip";
ZipFile zipFile = new ZipFile(zipFileName);
String extractedPath = "D:\\demo";
byte[] buffer = new byte[1024];
Enumeration enm = zipFile.entries();
ZipEntry ze = null;
InputStream fis = null;
while (enm.hasMoreElements()) {
ze = (ZipEntry) enm.nextElement();
System.out.println("processing = "+ze.getName());
File newFile = new File(extractedPath + File.separator + ze.getName());
// create all non exists folders
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
fis = zipFile.getInputStream(ze);
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
fis.close();
}
}
}

Thursday, January 3, 2019

Zip files in Java

To create or read zip files in Java, we can use java.util.ZipFile. This allow to read an zip file and read the entries inside it. We can create a zip file by using ZipOutputStream.

package demo.zipfile;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class ZipFileDemo {
public static void main(String args[]) throws IOException {
// create a zip file
String zipFileName = "D:\\demo.zip";
ZipOutputStream zipos = new ZipOutputStream(new FileOutputStream(zipFileName));
// In Zip Output Stream we can create file.
ZipEntry ze1 = new ZipEntry("file1.txt");
zipos.putNextEntry(ze1);
zipos.write("Hello World".getBytes());
zipos.closeEntry();
// Now lets create another file in a directory
zipos.putNextEntry(new ZipEntry("etc\\file2.txt"));
zipos.write("Hello Again".getBytes());
zipos.closeEntry();
zipos.close();
// To Read Zip file
ZipFile zipFile = new ZipFile(zipFileName);
// for specific entry
ZipEntry ze2 = zipFile.getEntry("file1.txt");
InputStream is = zipFile.getInputStream(ze2);
Scanner scan = new Scanner(is);
while (scan.hasNextLine()){
System.out.println(scan.nextLine());
}
scan.close();
Enumeration enm = zipFile.entries();
while(enm.hasMoreElements()) {
ZipEntry ze = (ZipEntry) enm.nextElement();
System.out.println(ze.getName());
}
}
}