Results 1 to 1 of 1
  1. #1
    jimmy is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default Recreate directory structure when extracting from zip file

    Using java.util.zip, I am unable to extract files in the exact directory structure as inside a zip file, I keep getting IOException: Parent of file is not a directory, below is the code, please advice.

    Java Code:
    public static void extractZip(String zippath, String parentpath, String zipfilename) {
    
    try  {  
    
          
    		
         ZipFile archive = new ZipFile(zippath); 
    		      
         byte[] buffer = new byte[2048];  
    		    
         for ( Enumeration e = archive.entries(); e.hasMoreElements();) {  
    		          
    	ZipEntry entry = (ZipEntry) e.nextElement();  
    		    
    	String filename = entry.getName();  
    	File destFile = new File(parentpath+"/"+zipname+"/"+filename);  
    		    
    	if (destFile.exists()) {
      
                System.out.println("Overwrite not supported");  
    	}  
           else {  
    	
                String parent = destFile.getParent();  
    	
                if (parent!=null) {  
    		              
                     File parentFile = new File(parent);  
    	    
                     if (!parentFile.exists()) {  
    		                  
    		              parentFile.mkdirs();  
    	         }  
    	    } 
    		            
    	    InputStream in = archive.getInputStream(entry);  
    		            
    	    destFile.createNewFile();
    	  
    	    OutputStream out = new FileOutputStream(destFile);  
    		    
    	    int count;  
    	
                while((count=in.read(buffer)) != -1)  
                      out.write(buffer, 0, count);  
    		    
                in.close();  
                out.close();
           }  
       }  
    
    } catch(Exception e) {e.printStackTrace();}
    
    }
    Last edited by jimmy; 03-18-2009 at 01:38 PM.

Similar Threads

  1. [SOLVED] File chooser selecting file from directory...?
    By prabhurangan in forum AWT / Swing
    Replies: 12
    Last Post: 06-18-2008, 04:08 AM
  2. Extracting JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:17 AM
  3. Extracting data from an XML file...
    By techno_brains in forum New To Java
    Replies: 1
    Last Post: 07-15-2007, 05:46 PM

Posting Permissions

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