ZipException : error in opening zip file
Hello,
I need to create a zip file using Java. I have to zip content from a directory (sub-folders included). So I'm using a recursive method.
In fact my zip file is well created, when I open it with winrar, winzip, everything is ok, same thing with a bash on linux (unzip).
My problem is that I have to open the created zip file in Java. When I do it, i get an exception:
java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:203)
at java.util.zip.ZipFile.<init>(ZipFile.java:234)
...
This is my method:
publicvoid zipContent()
{
ZipOutputStream zipout;
try
{
zipout =new ZipOutputStream("destination zip file");
zipDir("main source directory", zipout,"");
zipout.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
privatevoid zipDir(String source, ZipOutputStream zipout, String relativeDir)throws IOException
{
File zipDir =new File(source);
String[] dirList = zipDir.list();
byte[] readBuffer =newbyte[2156];
int bytesIn = 0;
// Loop through dirList, and zip the files.
for (int i = 0; i < dirList.length; i++)
{
File f =new File(zipDir, dirList[i]);
if (f.isDirectory())
{
// If the File object is a directory, call this
// function again to add its content recursively.
String filePath = f.getPath();
String name = f.getName();
if (!name.endsWith("/")) name +="/";
zipEntries.put(name, name);
if (relativeDir.equals(""))
{
ZipEntry anEntry;
anEntry =new ZipEntry(name);
zipout.putNextEntry(anEntry);
zipout.closeEntry();
zipDir(filePath, zipout, name);
}
else
{
ZipEntry anEntry;
if (!relativeDir.endsWith("/"))
relativeDir +="/";
anEntry =new ZipEntry(relativeDir + name);
zipout.putNextEntry(anEntry);
zipDir(filePath, zipout, relativeDir + name);
}
// Loop again.s
continue;
}
// Create a FileInputStream on top of f.
FileInputStream fis =new FileInputStream(f);
ZipEntry anEntry;
if ((!relativeDir.endsWith("/")) && (!relativeDir.equals("")))
relativeDir +="/";
if (!relativeDir.equals(""))
{
anEntry =new ZipEntry(relativeDir + f.getName());
}
else
{
anEntry =new ZipEntry(f.getName());
}
// Place the zip entry in the ZipOutputStream object.
zipout.putNextEntry(anEntry);
zipEntries.put(anEntry.getName(), anEntry.getName());
// Now write the content of the file to the ZipOutputStream.
while ((bytesIn = fis.read(readBuffer)) != -1)
{
zipout.write(readBuffer, 0, bytesIn);
}
// Close the Stream.
zipout.closeEntry();
fis.close();
}
}
This the code I use to unzip:
try
{
ZipFile zip =new ZipFile(zipFile);
Enumeration zipEnum = zip.entries();
zipEnum = zip.entries();
while (zipEnum.hasMoreElements())
{
ZipEntry item = (ZipEntry) zipEnum.nextElement();
// Directory case.
if (item.isDirectory())
{
File newdir =new File(unzippedFilesDir +"/" + item.getName());
newdir.mkdir();
}
// File case.
else
{
String newfile = unzippedFilesDir +"/" + item.getName();
InputStream is = zip.getInputStream(item);
FileOutputStream fos =new FileOutputStream(newfile);
int ch;
while ((ch = is.read()) != -1)
{
fos.write(ch);
}
is.close();
fos.close();
}
}
zip.close();
}
catch (Exception e)
{
System.out.println(e);
e.printStackTrace();
}
The exception is thrown on the line:
ZipFile zip =new ZipFile(zipFile);
Do you see something weird in my code..?
I'm working on this problem for several days, and I'm losing hope...
Thanks,
Phil

