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.