Re: Package Question: java.util.zip
Re: Package Question: java.util.zip
- Subject: Re: Package Question: java.util.zip
- From: "Alberto de Arriba" <email@hidden>
- Date: Thu, 7 Jul 2005 17:08:09 +0200
This is the class I use to extract the contents of a zip file.
import com.webobjects.foundation.*;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;
public final class ZipManager {
// buffer size used for extraction
static final int BUFFER = 2048;
public static NSArray decompress(String fileName, String destinationPath)
throws IOException {
NSMutableArray listNames = new NSMutableArray();
try {
File file = new File(fileName);
File destinationDirectory = new File(destinationPath);
ZipFile zipFile = new ZipFile(file, ZipFile.OPEN_READ);
Enumeration enumZipEntries = zipFile.entries();
// extract each entry from the zip file
while (enumZipEntries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) enumZipEntries.nextElement();
String entryName = entry.getName();
File destFile = new File(destinationDirectory, entryName);
listNames.addObject(destFile.getAbsolutePath());
// create the parent directory structure if it is needed
destFile.getParentFile().mkdirs();
// entries can be files or directories: extract entry if not a directory
if (!entry.isDirectory()) {
BufferedInputStream input = new
BufferedInputStream(zipFile.getInputStream(entry));
int len;
byte data[] = new byte[BUFFER];
BufferedOutputStream destOutputStream = new BufferedOutputStream(new
FileOutputStream(destFile), BUFFER);
while ((len = input.read(data, 0, BUFFER)) != -1) {
destOutputStream.write(data, 0, len);
}
destOutputStream.flush();
destOutputStream.close();
input.close();
}
}
zipFile.close();
} catch (IOException ioe) {
throw ioe;
}
return listNames.immutableClone();
}
}
The method throws an exception if the file is not a "real" zip and returns
an NSArray with the path of every extracted file.
HTH,
Alberto.
----- Original Message -----
From: Rick Langschultz
To: 'WebObjects (Group)'
Sent: Wednesday, July 06, 2005 4:13 AM
Subject: Package Question: java.util.zip
Does anyone have a working example with source for either a WebObjects
application, JSP, servlet or stand alone binary to extract/encode a zip
file. This is one of the features I would love to use in one of my web
applications.
Thanks,
Rick
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden