« Nice information regarding QuickTime and Streaming | Main | Gotta get those cookies »

August 16, 2004

Compress and Decompress with Java (using zip)

Compressing and Decompressing Data using Java

Posted by vanevery at 03:58 PM

Trackback Pings

TrackBack URL for this entry:
http://www.walking-productions.com/scgi-bin/mt/mt-tb.cgi/294

Comments

I've just been down the 'decompress with Java' road just recently also. Thought perhaps your readers might find the code I hacked useful in some situations. One is for simple ZIP decompression, the other for GZIP:

import java.io.*;
import java.util.*;
import java.util.zip.*;

/* UnZIP a file archive. */
/* Parameter 1: the directory containing the ZIP file. This will also */
/* be the output directory. */
/* Parameter 2: the ZIP file name. */
/* */

public class UnZIPFile {

public static void main(String[] args) {

String dir = args[0];
String infile = dir + "/" + args[1];
String outfile;

ZipInputStream zin;
FileOutputStream fout;
ZipEntry ze;
int length;
byte [] outBuf = new byte[512];

try {
zin = new ZipInputStream(
new BufferedInputStream(new FileInputStream(infile)));
System.out.println("Processing ZIP archive: " + infile);
while((ze = zin.getNextEntry()) != null) {
System.out.println(" Processing entry: " + ze);
outfile = dir + "/" + ze;
fout = new FileOutputStream(outfile);
while((length = zin.read(outBuf)) != -1) {
fout.write(outBuf,0,length);
}
fout.close();
System.out.println(" Entry extracted OK to: " + outfile);
}
zin.close();
System.exit(0); // (remove for servlets!)
}

catch (Exception e) {
System.err.println("UnZIPFile failed.");
System.err.println(e);
System.exit(-1);
}
}
}

import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.util.StringTokenizer;

/* UnGZIP a file archive. */
/* Parameter 1: the directory containing the GZIP file. This will also */
/* be the output directory. */
/* Parameter 2: the GZIP file name. */
/* Parameter 3: (optional) output file name */
/* */

public class UnGZIPFile {

public static void main(String[] args) {

String dir = args[0];
String fileName = args[1];
String infile = dir + "/" + fileName;
String outfile;

GZIPInputStream gzin;
FileOutputStream fout;
int length;
byte [] outBuf = new byte[512];

if (args.length == 3) { // output file name specified as a parameter
outfile = dir + "/" + args[2];
} else { // output file is input file name w/o extension
StringTokenizer st = new StringTokenizer(fileName, ".");
int tokenCount = st.countTokens();
if (tokenCount == 1) { // no extension, use 'txt'...
outfile = dir + "/" + fileName + ".txt";
} else {
outfile = dir + "/";
for (int i = 1; i

Posted by: Bill Middleton at September 23, 2004 02:30 PM

Your code doesn't take in account ZIP files that have directory entries after the file entries.

What happens, for example, if I get a file entry "images/color.jpg" and the directory "images" doesn't exist yet ?

I think a better option is to get the entry Enumeration from the ZipFile (with entries method) and then take different actions depending whether the entry is a directory or not (verify that with the isDirectory method).

If the entry is a directory, then we simply create it. If the entry is a file, first we get the directory part from its name and create the directory. Then we get the InputStream from the ZipEntry and write to the file.

Posted by: Ionut Margelatu at April 19, 2005 05:23 AM