I was wondering if anyone would tell me what I am doing wrong because I'm really stuck and getting really sad :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.

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.

...
ZipEntry readZipEntry = entries.nextElement();
	        	
ZipEntry newZipEntry = new ZipEntry(readZipEntry.getName());
zos.putNextEntry(newZipEntry);
...

Hope this helps someone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.