2 Replies - 2005 Views - Last Post: 29 June 2012 - 09:50 AM

#1 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Unzip File or Folder Contained within .Zip file

Posted 01 June 2007 - 05:44 AM

Description: I suggest, using a global variable for BUFFER of: private static final int BUFFER = 2048; This code is modified from the example supplied by java.sun.com The code supplied works, but it is missing some buffer flushing, and proper methodology.Decompresses a .zip file with the built in algorithm.
     public File unzip(File inFile, File outFolder)
     {
          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);
                    }
                    cleanUp(out);
               }
               cleanUp(in);
               return outFolder;
          }
          catch(Exception e)
          {
               e.printStackTrace();
               return inFile;
          }
     }
     
     private void cleanUp(InputStream in) throws Exception
     {
          in.close();
     }
     
     private void cleanUp(OutputStream out) throws Exception
     {
          out.flush();
          out.close();
     }


Is This A Good Question/Topic? 0
  • +

Replies To: Unzip File or Folder Contained within .Zip file

#2 Taiko3615   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 14-June 11

Re: Unzip File or Folder Contained within .Zip file

Posted 14 June 2011 - 09:57 PM

This code bug if you have a zip folder with subfolders, here is an improved version that should run in all circumstances. (Should run direclty after a copy/paste) public static File unzip(File inFile, File outFolder) { final int BUFFER = 2048; 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]; //We will try to reconstruct the entry directories File entrySupposedPath = new File(outFolder.getAbsolutePath()+File.separator+entry.getName()); //Does the parent folder exist? if (!entrySupposedPath.getParentFile().exists()){ entrySupposedPath.getParentFile().mkdirs(); } // 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; } }
Was This Post Helpful? 0
  • +
  • -

#3 Greg86   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-June 12

Re: Unzip File or Folder Contained within .Zip file

Posted 29 June 2012 - 09:50 AM

Have a look here for a fully working example of how to zip and unzip files and folders using the Apache Commons library. Definitely makes life a lot easier: http://developer-tip...ommons-Compress
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1