Update on java.zip util- please please help

Q

queen

I am nearly at my wits end with this program and need real help.
Ok- If you remember, I needed to copy a zip file the user specifies
and create a copy of that zip file.
So i cut out the middle step of extracting the files to a dir and then
zipping them back up- rather, I created a reference to the files in
the old zip (using enumeration type) and casting each nextElement as a
ZipEntry, sent the files in the old zip directly to the new zip- so no
need to delete the files anymore, b/c they are never extracted to a
dir:

Enumeration zipFileEntries=zipFile.entries(); //zipFile is a ZipFile

But still have problem of empty folder-It was interesting to read what
you guys had to say on the matter though. But for now, I'll assume
that there are no non empty subfolders in the zip. I can't deal with
that now. I have a new problem.

my code to add the files from the old zip to the new zip:

while(zipFileEntries.hasMoreElements())
{
//get/create a zip file entry
ZipEntry entry=(ZipEntry)zipFileEntries.nextElement();
/*without this, a file of unrecognizeable type is creatred bearing the
subfolder's name*/
if(entry.isDirectory())
continue;
out.putNextEntry(entry);
copy(zipFile.getInputStream(entry), out);
}
out.close();

public static void copy(InputStream in, OutputStream out) throws
IOException
{
//Transfer bytes from in to out
byte[] buf=new byte[1024];
int len;
//write the src file to dest file
while ((len=in.read(buf)) > 0)
out.write(buf, 0, len);
in.close();
}

It works fine for .txt files. But when the old zip has other file
types, ie: .doc, or .xls, then it crashes when it gets to the point of
adding that file to the new zip-
error I get:
Exception in thread "main" java.util.zip.ZipException: invalid entry
compressed size (expected 9059 but got 8996 bytes)...

What does this mean? How can I make it work for all types of files?

Someone? Anyone? Please please help.
Thanks so much guys. Youre the best! :)
 
C

Chris Smith

queen said:
It works fine for .txt files. But when the old zip has other file
types, ie: .doc, or .xls, then it crashes when it gets to the point of
adding that file to the new zip-
error I get:
Exception in thread "main" java.util.zip.ZipException: invalid entry
compressed size (expected 9059 but got 8996 bytes)...

There are all sorts of missing information here. For example:

1. What's in the "..." part of the error message? That's the part
that tells you where the error is, and I'm not sure why you
deleted it, apparently on purpose.

2. What's the implementation of your copy method?

There should be no reason that text files are any different from any
other file, unless your copy method is wrong.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Q

queen

Chris Smith said:
There are all sorts of missing information here. For example:

1. What's in the "..." part of the error message? That's the part
that tells you where the error is, and I'm not sure why you
deleted it, apparently on purpose.

2. What's the implementation of your copy method?

There should be no reason that text files are any different from any
other file, unless your copy method is wrong.

Sorry- :)
1) Exception in thread "main" java.util.zip.ZipException: invalid
entry
compressed size (expected 9059 but got 8996 bytes)
at java.util.zip.ZipOutputStream.closeEntry(Unknown Source)
at java.util.zip.ZipOutputStream.putNextEntry(Unknown Source)
at ZipProgram.addToZip(ZipProgram.java:109)
at ZipProgram.main(EachFile.java:178)

2) Here is a more complete form of the code. This part is broken down
into 3 functions: a) to get the zip entries from oldZipFile.zip b) add
each file to ZipOutputStream (newZipFile.zip) and this calls, for each
file 3) copy- to copy the file.
And you are right :) about no difference btwn .txt or other type
file- when I ran it again it crashed as it reached a .txt.
However, if there were only .txt in the zip it would run fine- only
crashes when I add other files types.
-The 3 functions:
1) getZipEntries(String zf) //gets the contents of oldZipFile.zip
{
File z=new File(zf);
ZipFile zipFile=new ZipFile(z, ZipFile.OPEN_READ);
Enumeration zipFileEntries=zipFile.entries(); //get entries of
oldZipFile.zip
addToZip();
}

2) addToZip()
{
while(zipFileEntries.hasMoreElements())
{
ZipEntry ze=(ZipEntry)zipFileEntries.nextElement();
zos.putNextEntry(ze); //zos is type ZipOutputStream
copy(zipFile.getInputStream(ze),zos);
}
zos.close();
}

3) copy(InputStream in, OutputStream out)
{
byte []buf=new byte[1024];
int len;
while((len=in.read(buf))>0)
out.write(buf,0,len);
in.close();
}
}

Please can you help?
 

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
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top