Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Unzip files and folders

  1. #1
    Asolpdrag is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Question Unzip files and folders

    I have serach though the whole web and I have only got errors.
    My zip file has some folders in it for example "folder1" and "folder2". And with all the codes I have tried, I get this error:
    Cant find path to /xxxxxxxx/folder1
    Or something like that.
    In fact none of the codes I have tried have extracted anything at all.
    This is the last one I tried, but the other ones are very similar:

    Java Code:
    BUFFER = 2048;
    
    File inFile = new File(ZipName);
    		File outFolder = new File("/" + ZipName);
    				
    		try
            {
                 BufferedOutputStream out = null;
                 ZipInputStream  in = new ZipInputStream(
                                               new BufferedInputStream(
                                                    new FileInputStream(inFile)));
                 ZipEntry entry;
                 while((entry = in.getNextEntry()) != null)
                 {
                      //System.out.println("Extracting: " + entry);
                      int count;
                      byte data[] = new byte[BUFFER];
                     
                      // write the files to the disk
                      out = new BufferedOutputStream(
                                new FileOutputStream(outFolder.getPath() + "/" + entry.getName()),BUFFER);
                     
                      while ((count = in.read(data,0,BUFFER)) != -1)
                      {
                           out.write(data,0,count);
                      }
                      out.flush();
                      out.close();
                 }
                 in.close();
                 //return outFolder;
            }
            catch(Exception e)
            {
                 e.printStackTrace();
                 //return inFile;
            }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Unzip files and folders

    Or something like that.
    Can you post the compete text of the error message?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Unzip files and folders

    Quote Originally Posted by Norm View Post
    Can you post the compete text of the error message?
    ... along with a SSCCE (Short, Self Contained, Compilable and Executable) example that clearly demonstrates the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Asolpdrag is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Unzip files and folders

    Java Code:
    java.io.FileNotFoundException: \last.zip\Prueba1.txt (El sistema no puede encontrar la ruta especificada)
    	at java.io.FileOutputStream.open(Native Method)
    	at java.io.FileOutputStream.<init>(Unknown Source)
    	at java.io.FileOutputStream.<init>(Unknown Source)
    	at voxel.updater.extract(updater.java:237)
    	at voxel.updater.download(updater.java:195)
    	at voxel.updater.updater(updater.java:281)
    	at voxel.Window.main(Window.java:10)
    I am new in programming, so I dont understand what a SSCCE means, so I have made what I could:

    Java Code:
    package com.unzip; //Your Package
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    public class Unzip { //Your Class Name
    	
    	private static String ZipName = "last.zip"; //Your Zip File To Be Extracted
    	private static final int BUFFER = 2048;
    	
    	public static void main(String[] args) {
    
    		File inFile = new File(ZipName);
    		File outFolder = new File("/" + ZipName);
    				
    		try
            {
    			
                 BufferedOutputStream out = null;
                 ZipInputStream  in = new ZipInputStream(
                                               new BufferedInputStream(
                                                    new FileInputStream(inFile)));
                 
                 ZipEntry entry;
                 while((entry = in.getNextEntry()) != null) {
    
                      int count;
                      byte data[] = new byte[BUFFER];
                     
    
                      out = new BufferedOutputStream(
                                new FileOutputStream(outFolder.getPath() + "/" + entry.getName()),BUFFER);
                     
                      while ((count = in.read(data,0,BUFFER)) != -1) {
                    	  
                           out.write(data,0,count);
                           
                      }
                      
                      out.flush();
                      out.close();
                 }
                 
                 in.close();
    
            }
            catch(Exception e)
            {
            	
                 e.printStackTrace();
                 
            }
    		
       }
    	
    }
    and here is the zip file I am using: last.zip

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Unzip files and folders

    Quote Originally Posted by Asolpdrag View Post
    I am new in programming, so I dont understand what a SSCCE means, ...
    The link in Darryl's reply pretty much explains the SSCCE concept fully.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Unzip files and folders

    java.io.FileNotFoundException: \last.zip\Prueba1.txt
    Does the folder in the error message exist: \last.zip\

    Where are the packages shown in the error message: voxel.updater and voxel.Window ?
    Your posted code is NOT the same code as generated the error message.
    You need to have the error message and the code agree.
    Last edited by Norm; 02-04-2012 at 06:44 PM.

  7. #7
    Asolpdrag is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Unzip files and folders

    I am not english, thats why I didn't undertand SSCCE article.
    I think my error was that I was saying that I wanted the zip file to be extracted to a folder called "last.zip" but what I really want is to extract the zip file CALLED "last.zip" to the root folder of my program.

    So my question now is:

    How can I set the folder to the root folder?

    The reason package dont match is that I have my class updater which is in a package voxel (the name of my program) cannot run by itself, so I made another class exactly the same but which can run by itself, so you could copy paste and run it easily (thats the only thing I think I have understood from SSCCEE Article). Finally my class Window has nothing to do with the unzipping, so I have just ommited it as I have read in SSCCE Article.

    Sorry about my mistakes.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Unzip files and folders

    I'm no expert in this, but perhaps you need to make directories when one is needed or encountered. For example,

    Java Code:
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    public class Unzip2 {
       private static final String IN_PATH = "C:/Users/Pete/Documents/Fubar/";
       private static final String ZIP_NAME = "last.zip"; 
       private static final String OUT_PATH = "out/";
    
       public static void main(String[] args) {
          extract(IN_PATH + ZIP_NAME, IN_PATH + OUT_PATH);
       }
    
       private static void extract(String zipPath, String outDirPath) {
          File outDir = new File(outDirPath);
          if (!outDir.exists()) { // if output directory doesn't exist
             outDir.mkdir();  // make it
          }
          BufferedInputStream bis = null;
          BufferedOutputStream bos = null;
          byte[] buffer = new byte[2^16];
          
          try {
             FileInputStream fis = new FileInputStream(zipPath);
             ZipInputStream zis = new ZipInputStream(fis);
             bis = new BufferedInputStream(zis);
             ZipEntry ze = null;
             while ((ze = zis.getNextEntry()) != null) {
                if (ze.isDirectory()) {
                   System.out.println("directory: " + ze.getName());
                   File dir = new File(outDirPath + ze.getName());
                   if (!dir.exists()) { // if directory doesn't exist
                      dir.mkdir();   // make it!
                   }
                } else {
                   System.out.println("file: " + ze.getName());
                   
                   try {
                      File file = new File(outDirPath + ze.getName());
                      FileOutputStream fos = new FileOutputStream(file);
                      bos = new BufferedOutputStream(fos);
                      
                      while (bis.read(buffer) > 0) {
                         bos.write(buffer);
                      }
                   } finally {
                      if (bos != null) {
                          bos.close();
                      }
                   }
                }
                zis.closeEntry();
             }
             
          } catch (FileNotFoundException e) {
             e.printStackTrace();
          } catch (IOException e) {
             e.printStackTrace();
          } finally {
             if (bis != null) {
                try {
                   bis.close();
                } catch (IOException e) {
                   e.printStackTrace();
                }
             }
          }
       }
    
    }

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Unzip files and folders

    If you are having problems getting your program to write to the desired directory, make a small test program that writes one line to a file. Then test different paths with that program until it writes to the directory you want.
    When you find the correct path, copy the path into the unzip program.

  10. #10
    Asolpdrag is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Unzip files and folders

    Ok that was ONE of my problems, not the problem is to extract folders, I get errors saying folders dont exixst, it should create thim if that happens?

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Unzip files and folders

    I get errors saying folders dont exist
    The File class has a method for creating folders.

  12. #12
    Asolpdrag is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Unzip files and folders

    and how can I indicate the program to extract the files in the same folder it is?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Unzip files and folders

    Quote Originally Posted by Asolpdrag View Post
    and how can I indicate the program to extract the files in the same folder it is?
    By simply telling it to do so. Have you looked at my sample code above?
    Norm likes this.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Unzip files and folders

    Create the FileOutputStream with the path where you want the file to be written.

  15. #15
    Asolpdrag is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Unzip files and folders

    ok now I have this code:

    Java Code:
    	    try {
    	    	
    	        byte[] buffer = new byte[1024];
    	        
    	        ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream(ZipName));
    	        ZipEntry zipentry = zipinputstream.getNextEntry();
    	        
    	        System.out.println ("Start");
    	        System.out.println (zipentry);
    	        
    	        while (zipentry != null) {
    	        	
    	            String entryName = "" + zipentry.getName();
    	            
    	            File newFile = new File(entryName);
    	            
    	            System.out.println ("Entered while loop 1");
    	            
    	            if (zipentry.isDirectory()) {
    	            	
    	            	System.out.println ("Entered if 1");
    	            	
    	                if (!newFile.mkdirs()) {
    	                	
    	                	System.out.println ("Entered if 2");
    	                    break;
    	                    
    	                }
    	                
    	                zipentry = zipinputstream.getNextEntry();
    	                
    	                System.out.println ("Just Ending if 1");
    	                continue;
    	                
    	            }
    
    	            System.out.println ("Ended if 1");
    	            
    	            int n;
    	            
    	            FileOutputStream fileoutputstream = new FileOutputStream(entryName);
    
    	            while ((n = zipinputstream.read(buffer)) != -1) {
    	            //while ((n = zipinputstream.read(buffer, 0, 1024)) > -1) {
    	            	
    	            	System.out.println ("Entered while loop 2");
    	            	
    	                fileoutputstream.write(buffer, 0, n);
    	                
    	            }
    
    	            fileoutputstream.close();
    	            zipinputstream.closeEntry();
    	            zipentry = zipinputstream.getNextEntry();
    	            
    	            System.out.println (zipentry);
    	            System.out.println ("Ended loop 1");
    
    	        }
    
    	        zipinputstream.close();
    	        
    	        System.out.println("EXTRACTED");
    	        
    	    } catch (Exception e) {
    	    	
    	        e.printStackTrace();
    	        System.out.println("Coldn't extract game files");
    	        
    	    }
    But with some zip files it enters the while loop but with others "zipentry" has null value.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Unzip files and folders

    with some zip files it enters the while loop but with others "zipentry" has null value.
    What is the difference between the two types of zip files?

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Help please, Playing with folders and files.
    By paulio2 in forum New To Java
    Replies: 38
    Last Post: 11-17-2011, 03:16 PM
  2. Error in unzip for large files
    By magesh joghee in forum Advanced Java
    Replies: 1
    Last Post: 08-03-2011, 05:35 PM
  3. Access files in folders and sub-folders
    By kakinyim in forum CLDC and MIDP
    Replies: 0
    Last Post: 05-18-2011, 10:29 PM
  4. Folders to group source files
    By Skiller in forum New To Java
    Replies: 6
    Last Post: 03-31-2011, 09:20 PM
  5. Searching directories for folders and .txt files
    By XDrew574X in forum New To Java
    Replies: 1
    Last Post: 03-29-2011, 10:41 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
  •