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?

