| | |
java.util.zip help (please)
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2004
Posts: 1
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
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.
Hope this helps someone.
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.
Java Syntax (Toggle Plain Text)
... ZipEntry readZipEntry = entries.nextElement(); ZipEntry newZipEntry = new ZipEntry(readZipEntry.getName()); zos.putNextEntry(newZipEntry); ...
Hope this helps someone.
![]() |
Similar Threads
- C++ Unzip Message String which is zipped using java.until.zip (C++)
- array required, but java.lang.String found and java.util.Vector found? (Community Introductions)
- problem with java.util.Collections (Java)
Other Threads in the Java Forum
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu update windows





