Zip read exception

Hello,

I have a small problem regarding reading a zip. I have first looked over the other tutorials of how to do this but I am getting an exception when writing one of the files.

public ZipFile m_ZipFile;// Dont worry about null pointers, it has been instantiated, it is not closed when doing addFileToArchive

public String m_FilePath;// the path to the zipfile (m_ZipFile)

publicboolean addFileToArchive(File[] files)

{

try

{

File zipFile =new File(m_FilePath);

byte[] buf =newbyte[1024];

ZipOutputStream out =new ZipOutputStream(new FileOutputStream(zipFile));

for (Enumeration entries = m_ZipFile.entries(); entries.hasMoreElements();)

{

ZipEntry zipEntry = (ZipEntry)entries.nextElement();

System.out.println("Readding: " + zipEntry.getName());

InputStream zin = m_ZipFile.getInputStream(zipEntry);

out.putNextEntry(zipEntry);

int len;

while ((len = zin.read(buf)) > 0)// LINE 220

{

out.write(buf, 0, len);

}

out.closeEntry();

zin.close();

}

for (int i = 0; i < files.length; i++)

{

InputStream in =new FileInputStream(files[i]);

out.putNextEntry(new ZipEntry(files[i].getName()));

int len;

while ((len = in.read(buf)) > 0)

{

out.write(buf, 0, len);

}

// Complete the entry

out.closeEntry();

in.close();

}

out.close();

}

catch (java.io.FileNotFoundException fnfe)

{

System.out.println("Exception caught.");

fnfe.printStackTrace();

}

catch (java.io.IOException ioe)

{

System.out.println("Exception caught.");

ioe.printStackTrace();

}

returntrue;

}

Output and exception:

Readding: test/

Readding: test/dummy.txt

Exception caught.

java.util.zip.ZipException: ZIP_Read: error reading zip file

at java.util.zip.ZipFile.read(Native Method)

at java.util.zip.ZipFile.access$1200(ZipFile.java:29)

at java.util.zip.ZipFile$ZipFileInputStream.read(ZipFile.java:447)

at java.util.zip.ZipFile$1.fill(ZipFile.java:230)

at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:141)

at java.io.FilterInputStream.read(FilterInputStream.java:90)

at ZZipster.ZZipArchive.addFileToArchive(ZZipArchive.java:220)

Line 220 (First while): while ((len = zin.read(buf)) > 0)

As you can see, I'm trying to re-add the currently zipped files, and then add the additional files. What is wrong?

[4005 byte] By [Leviaa] at [2007-11-27 4:49:33]
# 1
You have a for-loop that enumerates the entries in the input ZIP file and reads each of them. After reading the first one you close the input ZIP file. Trying to read the second one causes the exception.(Not a very clear message, is it?)
DrClapa at 2007-7-12 10:02:42 > top of Java-index,Java Essentials,Java Programming...
# 2
Hmm, it is actually. But im just wondering, where am I closing the zip then? out.closeEntry() just ends the entry only..and zin.close is closing the entry's inputstream?
Leviaa at 2007-7-12 10:02:42 > top of Java-index,Java Essentials,Java Programming...