I have a method which performs zipping. I took the zipping code found on the net and modified it to make sure that folders can be zipped as well.

When using the code to zip a folder, it always produces an exception. Yet, the strange thing is that the code still manages to zip the folder correctly. I can view the contents of the zipped folder and unzip it.

There is no such problem if the zipping is done on a file.

The code is shown below:

Code:
public void PerformZip(File file,String destZipFile)
{
        try
	{
		int i;
		FileOutputStream fileOutStream = new FileOutputStream(destZipFile);
		ZipOutputStream zipOS = new ZipOutputStream(fileOutStream);
			
		if(file.isFile())  //if the entry is a file, zip it directly
		{
			long size; int len = 0;
			size = file.length();
			byte[] buf = new byte[(int)size];
				
			FileInputStream	fileInStream = new FileInputStream(file.getPath());
				
			zipOS.setLevel(Deflater.BEST_COMPRESSION);
			ZipEntry zipentry = new ZipEntry(file.getName());
			zipOS.putNextEntry(new ZipEntry(zipentry));
				
			while((len=fileInStream.read(buf)) > 0)
			{
				zipOS.write(buf,0,len);
			}
				
			zipOS.closeEntry();
			fileInStream.close(); 
			
		}
		else if(file.isDirectory()) //if the entry is a folder, do recursion
		{
			String name = file.getName();
			String folderPath = file.getPath();
				
			File path = new File(folderPath);
			File[] filesInFolder = path.listFiles();
				
			//Recurse to get at and zip the items in the folder
			for(int j = 0;j < filesInFolder.length;j++)
			{
				String destination = file.getParent() + "\\" + file.getName() + ".Z";
				SourcingPerformZip(filesInFolder[j],destination);
			}
		}
		
		zipOS.close();
		
	}catch(IOException e)
	{
	       System.out.println("Exception occured when zipping");
	}	
}
I guess it is the code within the elseif(file.isDirectory()) clause which cause the exception but I cant figure out which line is the culprit, especially when the folder still manages to be zipped correctly.