We will unzip a zip file with Java using a third party library. But yes, you can also unzip the zip using pure Java without any third party library.
Language Used:
Java
Git Repo:
https://cooltrickshome.blogspot.in/2016/11/unzip-files-using-java.html
Reference:
Without using any third party: http://www.codejava.net/java-se/file-io/programmatically-extract-a-zip-file-using-java
Using Third party library : http://stackoverflow.com/questions/10633595/java-zip-how-to-unzip-folder
POM Dependency:
Program:
Main method:
How it works:
1) We make a scanner object to take user input
2) We ask user the file to be zipped
3) We ask password from user if needed
4) We call unzipFile method which unzip the file. It take 3 parameters.
5) First param defines the file to be unzipped
6) Second param defines the path where file will be extracted
7) Third param defines the password to be used for extracting zip file
unzipFile method:
How it works:
1) We make an object of ZipFile pointing to the zip file to be unzipped
2) isEncrypted tell if the zip file require password for extraction
3) If password is needed we use setPassword to set the password for extraction
4) extractAll retreives the content of the zip file
Output:
Full Program:
Hope it helps :)
Language Used:
Java
Git Repo:
https://cooltrickshome.blogspot.in/2016/11/unzip-files-using-java.html
Reference:
Without using any third party: http://www.codejava.net/java-se/file-io/programmatically-extract-a-zip-file-using-java
Using Third party library : http://stackoverflow.com/questions/10633595/java-zip-how-to-unzip-folder
POM Dependency:
<!-- https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j -->
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.1</version>
</dependency>
Program:
Main method:
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
System.out.println("Please enter the zip file to be unzipped");
String zipFile=s.nextLine();
System.out.println("Please enter the password for zip file (type none if no password)");
String password=s.nextLine();
File f=new File(zipFile);
//Your password if any
unzipFile(zipFile, f.getParent(),password);
System.out.println("Extracted zip content at "+f.getParent());
s.close();
}
How it works:
1) We make a scanner object to take user input
2) We ask user the file to be zipped
3) We ask password from user if needed
4) We call unzipFile method which unzip the file. It take 3 parameters.
5) First param defines the file to be unzipped
6) Second param defines the path where file will be extracted
7) Third param defines the password to be used for extracting zip file
unzipFile method:
public static void unzipFile(String sourceZip, String destination, String password)
{
try {
ZipFile zipFile = new ZipFile(sourceZip);
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
zipFile.extractAll(destination);
} catch (ZipException e) {
e.printStackTrace();
}
}
How it works:
1) We make an object of ZipFile pointing to the zip file to be unzipped
2) isEncrypted tell if the zip file require password for extraction
3) If password is needed we use setPassword to set the password for extraction
4) extractAll retreives the content of the zip file
Output:
Please enter the zip file to be unzipped
C:\Users\anurag\Desktop\images.zip
Please enter the password for zip file (type none if no password)
Your password if any
Extracted zip content at C:\Users\anurag\Desktop
Full Program:
package com.cooltrickshome;
import java.io.File;
import java.util.Scanner;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
public class UnzipFile {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
System.out.println("Please enter the zip file to be unzipped");
String zipFile=s.nextLine();
System.out.println("Please enter the password for zip file (type none if no password)");
String password=s.nextLine();
File f=new File(zipFile);
//Your password if any
unzipFile(zipFile, f.getParent(),password);
System.out.println("Extracted zip content at "+f.getParent());
s.close();
}
public static void unzipFile(String sourceZip, String destination, String password)
{
try {
ZipFile zipFile = new ZipFile(sourceZip);
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
zipFile.extractAll(destination);
} catch (ZipException e) {
e.printStackTrace();
}
}
}
Hope it helps :)
No comments:
Post a Comment