Saturday, April 13, 2013

How to unzip a .zip file with Zip4j

3 days ago I discovered a  cool easy to use java tool for working with zip files.
It is called Zip4j.

Have a look at this super simple example that will allow you to unzip a file into a given path in your computer:

 String source = "folder/source.zip";  
 String destination = "folder/source/";    
   try {  
     ZipFile zipFile = new ZipFile(source);  
     zipFile.extractAll(destination);  
   } catch (ZipException e) {  
     e.printStackTrace();  
   }  

Use this if the unzip file has a password:

 String source = "folder/source.zip";  
 String destination = "folder/source/";  
 String password = "password";  
 try {  
   ZipFile zipFile = new ZipFile(source);  
   if (zipFile.isEncrypted()) {  
     zipFile.setPassword(password);  
   }  
   zipFile.extractAll(destination);  
 } catch (ZipException e) {  
   e.printStackTrace();  
 }  

That was easy and cool.
Are you now thinking the same as I am thinking?...
Let's go brute force zip passwords!!! :P

Hahaha.... I am just joking don't do that, this is not a hacking blog( yet! :p )  I hope you enjoyed this post. Use Zip4j with care.

3 comments:

  1. when i try to call these functions zipFile.getName(), zipFile.getEntry(fileName) i am getting this error
    The method getEntry(String).getName() is undefined for the type ZipFile

    ReplyDelete
    Replies
    1. You are probably using a different an old version of the library... Which version of the library are you using? I found a related answer in stackoverflow, maybe you find it useful:
      http://stackoverflow.com/questions/18974389/extracting-files-with-zip4j-temporarily-extracted-for-reading-but-not-visible

      Delete
  2. This comment has been removed by the author.

    ReplyDelete

Share with your friends