CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2005
    Posts
    200

    Unhappy Cant figure out why this zipping code produces an exception

    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.

  2. #2
    Join Date
    Mar 2008
    Posts
    14

    Re: Cant figure out why this zipping code produces an exception

    Do you have the stack trace?

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Cant figure out why this zipping code produces an exception

    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.
    Given that it zips correctly, it's more likely that as your recusion unwinds you are closing the stream multiple times and calling close() on a closed stream will throw an exception.

    This problem does show that its important to do something sensible when exceptions are caught and not just print a pointless message such as "Exception occured when zipping" (although it has to be said this is better than catching an exception and doing absolutely nothing). If you are not able to programtically handle the problem then you should output a useful message such as printing the stack trace so you can figure out where things have gone wrong ie use e.printStackTrace();

  4. #4
    Join Date
    Feb 2008
    Posts
    966

    Re: Cant figure out why this zipping code produces an exception

    Also to note: you are only catching IOException, however

    FileOutputStream fileOutStream = new FileOutputStream(destZipFile);

    Will throw FileNotFoundException if the file is not in the working directory (or just can't be found). You should be catching all of the possible exceptions so that you can properly analyze what is going on. If nothing else, make your last catch block Exception e so that you have covered all bases if you don't know what exceptions are being thrown.

  5. #5
    Join Date
    Apr 2005
    Posts
    200

    Re: Cant figure out why this zipping code produces an exception

    i've printed out the exception message and it says that the zip file must have at least 1 entry.

    Code:
    java.util.zip.ZipException: ZIP file must have at least one entry
            at java.util.zip.ZipOutputStream.finish(ZipOutputStream.java:292)
            at java.util.zip.DeflaterOutputStream.close(DeflaterOutputStream.java:14
    6)
            at java.util.zip.ZipOutputStream.close(ZipOutputStream.java:311)
            at zipAndSign.myUtils.Zip.FileZip.SourcingPerformZip(FileZip.java:407)
            at zipAndSign.Start.Run(Start.java:186)
            at Main.main(Main.java:15)
    ZIP file must have at least one entry
    
    Press any key to continue...
    However, the folder which was zipped has 4 files inside (no inner folders). What could be causing this exception?

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Cant figure out why this zipping code produces an exception

    What could be causing this exception?
    If you read the exception stack strace you will see the problem occurs in your code in method SourcingPerformZip which is in file FileZip.java at line 407. This line appears to be calling the output streams close() method which is ultimately failing because the zip file is empty.

    You haven't shown us the SourcingPerformZip() method (method names shouldn't start with a capital letter BTW, so this should be called sourcingPerformZip()) so it's hard to say exactly what is wrong. I have noticed in your code that you have a comment saying you are using recursion to handle directory entries but you are not recursively calling the PerformZip() method. Having said that, if you were, it wouldn't work because this method creates a new output stream each time it is called and so would overwrite any files previously added to the zip.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured