unzip method...

A

Aerodyne

Hi all,

If I'm using an unzip method like this ... how do I get it to unzip a
file (about 6MB) uploaded to a location... there should only be one zip
file in this temp folder, once it's been unziped then uploaded to the
server everything in that folder will be deleted.

public void unpackZip() {
try {
final int BUFFER = 2048;
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(???); // is this
right
CheckedInputStream checksum = new CheckedInputStream(fis, new
Adler32()); // faster checksum than CRC32
ZipInputStream zis = new ZipInputStream(new
BufferedInputStream(checksum));
ZipEntry entry;
while((entry = zis.getNextEntry()) != null) {
// System.out.println("Extracting: " +entry);
int count;
byte data[] = new byte[BUFFER];
// write the files to the disk
FileOutputStream fos = new
FileOutputStream(entry.getName());
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
zis.close();
// System.out.println("Checksum:
"+checksum.getChecksum().getValue());
} catch(Exception e) {
e.printStackTrace();
}

}

I need help w/ the following please:
- unzip a file
- shows the checksum error if possible... jsp is the page + tomcat 5.
- delete folders contants... after upload
 
T

Thomas Weidenfeller

Aerodyne said:
public void unpackZip() {
try {
final int BUFFER = 2048;
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(???); // is this
right

ZipFile zf = new ZipFile("a-filename.zip");
Enumeration entries = zf.entries();
while(entries.hasMoreElements())) {
...
...
}

CheckedInputStream checksum = new CheckedInputStream(fis, new
Adler32()); // faster checksum than CRC32

What should this be good for? You will get a checksum over the whole zip
file, but you have nothing to compare it against.
ZipInputStream zis = new ZipInputStream(new
BufferedInputStream(checksum));
ZipEntry entry;
while((entry = zis.getNextEntry()) != null) {
// System.out.println("Extracting: " +entry);
int count;
byte data[] = new byte[BUFFER];
// write the files to the disk
FileOutputStream fos = new
FileOutputStream(entry.getName());

Bad in multiple ways. getName() returns the zip path name of the entry.
This is is not necessarily a file name:

- For security reasons you should check for absolute pathes and skip
such entries or prefix them with the current directory. If not, then
some clown can place a file with the same name as something important in
the archive and you might happily overwrite your own important file. Or
someone places some new configuration file somewhere.

- You should in general also check if the file already exists before
creating it.

- Zip always uses '/' as the path separator, which is not a valid path
separator on all operating systems. You must replace all '/' in the zip
path name with File.pathSeparator

- Pathes ending with a '/' (which is equal to isDirectory() == true)
don't indicate files at all, but directories. In such a case you better
create a directory, instead of a file.

- It is not uncommon that directory entries in a zip file are missing.
In such cases you will get file pathes which point to directories which
don't exist. Therefor, it is best to anticipatorily attempt to create
the directories for each and every zip entry.


I think you should rework your code from scratch, taking real-world
behavior of the zip package into account.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top