java.util.zip help (please)

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2004
Posts: 1
Reputation: queen is an unknown quantity at this point 
Solved Threads: 0
queen queen is offline Offline
Newbie Poster

java.util.zip help (please)

 
0
  #1
Jul 14th, 2004
I was wondering if anyone would tell me what I am doing wrong because I'm really stuck and getting really sad
goal: copy contents of oldZipFile.zip and create a newZip.zip with contents of old zip file.

Problem: If oldZipFile.zip just contains .txt files, it works. But if it contains larger files, such as .doc, then the program crashes- it expected x number of bytes but got x number of bytes instead.
I'm not sure what this error means or how to fix it.

This part of my code is broken down into 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();
}
}

If anyone can please please help me, I would really appreciate it. Anyone up for the challenge? Thanks.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: rg.crichton is an unknown quantity at this point 
Solved Threads: 0
rg.crichton rg.crichton is offline Offline
Newbie Poster
 
1
  #2
Oct 13th, 2009
I know this thread is old but I may as well post this for people that encounter this problem and also find no help.

To solve this problem I just created a new ZipEntry instead of reusing the one that came from the ZipFile being read. The ZipEntry read from the original zip file has all its properties set and I'm guessing the zip algorithm used doesn't always zip files to the exact same size. That's why it complains about the size of the ZipEntry. If you just create a new ZipEntry with the same name and add that instead it works perfectly. eg.

  1. ...
  2. ZipEntry readZipEntry = entries.nextElement();
  3.  
  4. ZipEntry newZipEntry = new ZipEntry(readZipEntry.getName());
  5. zos.putNextEntry(newZipEntry);
  6. ...

Hope this helps someone.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC