Discussion Locked

Using the java.util.Zip package

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

How do I use the classes in the Zip package to:

1) zip a file. For example, I want to zip the file c:\temp\test.txt into c:\temp\testzip.zip

2) unzip a file. For example, I want to unzip all the contents of c:\temp\testzip.zip to c:\temp

5 replies

by alphacentauri

Here's a snippet that might help for unzipping...

byte b[] = new byte[1024];
ZipFile zf = new ZipFile(zipfile);
for (Enumeration e = zf.entries();
e.hasMoreElements();) {
ZipEntry ze = (ZipEntry)e.nextElement();
String filename =ze.getName();
InputStream is = zf.getInputStream(ze);

FileOutputStream fo =
new FileOutputStream(filename);
while (fi.read(b) != -1) {
fo.write(b);
}
fo.close();
}

by theobs

i've tried your proposed solution already. the problem is the size of the file that was unzipped is incorrect. for example, consider a text file in the zip file that has only 100 bytes. by initializing the byte array with 1024 elements, the resulting unzipped file's size will be 1024 whereas it was just 100 originally.

This discussion is closed to new replies.