[Java] unzip

Now that we know how to zip in Java, let's see how to unzip using the same Apache Commons Compress library:

 import java.util.*;  
 import java.io.*;  
 import org.apache.commons.compress.archivers.zip.*;  
 import org.apache.commons.compress.utils.IOUtils;  
 public void unzip(String ziplocation, String zipname, String unziplocation){  
      //method variables  
      private ZipFile zipFile;  
      private Enumeration<ZipArchiveEntry> entries;  
      private ZipArchiveEntry entry;  
      private File entryDestination;  
      private InputStream in;  
      private OutputStream out;  
      zipFile = new ZipFile(ziplocation+File.separator+zipname);  
      //get zip file content references  
      entries = zipFile.getEntries();  
      if(entries != null){  
           //for each reference, unzip to unziplocation, creating directories if needed  
           while (entries.hasMoreElements()) {  
                entry = entries.nextElement();  
                entryDestination = new File(unziplocation, entry.getName());  
                if (entry.isDirectory()){  
                else {  
                     in = zipFile.getInputStream(entry);  
                     out = new FileOutputStream(entryDestination);  
                     IOUtils.copy(in, out);  
                     //close streams ignoring exceptions  

No comments:

Post a Comment

With great power comes great responsibility.

Da grandi poteri derivano grandi responsabilità.