Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Update on java.zip util- please please help

Reply
Thread Tools

Update on java.zip util- please please help

 
 
queen
Guest
Posts: n/a
 
      07-14-2004
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!
 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      07-15-2004
queen wrote:
> 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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-15-2004
On 14 Jul 2004 16:09:18 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (queen) wrote or
quoted :

>out.putNextEntry(entry);
>copy(zipFile.getInputStream(entry), out);


you need out.closeEntry();
after your copy.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
queen
Guest
Posts: n/a
 
      07-15-2004
Chris Smith <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)> ...
> 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:17

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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Update On The Windows Phone 7 Update Update Lawrence D'Oliveiro NZ Computing 2 02-25-2011 08:03 AM
Help Help Help Pentax S5i Help needed (Please) The Martian Digital Photography 14 06-20-2008 07:56 AM
Please please please help this guy with his open source java app casioculture@gmail.com Java 4 05-05-2005 08:24 AM
HELP! HELP! PLEASE, PLEASE, PLEASE tpg comcntr Computer Support 11 02-15-2004 06:22 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments