Q
queen
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!
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!