General discussion

Locked

Using the java.util.Zip package

By theobs ·
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

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Using the java.util.Zip package

by AlphaCentauri In reply to Using the java.util.Zip p ...

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();
}

Collapse -

Using the java.util.Zip package

by AlphaCentauri In reply to Using the java.util.Zip p ...

fi.read should be is.read

Collapse -

Using the java.util.Zip package

by theobs In reply to Using the java.util.Zip p ...

Poster rated this answer

Collapse -

Using the java.util.Zip package

by theobs In reply to Using the java.util.Zip p ...

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.

Collapse -

Using the java.util.Zip package

by theobs In reply to Using the java.util.Zip p ...

This question was closed by the author

Back to Web Development Forum
5 total posts (Page 1 of 1)  

Related Discussions

Related Forums