Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ZIP - how to unzip folder?

Tags:

Is there any sample code, how to particaly unzip folder from ZIP into my desired directory? I have read all files from folder "FOLDER" into byte array, how do I recreate from its file structure?

like image 922
Waypoint Avatar asked May 17 '12 10:05

Waypoint


People also ask

How can I read the content of a zip file without unzipping it in Java?

Methods. getComment(): String – returns the zip file comment, or null if none. getEntry(String name): ZipEntry – returns the zip file entry for the specified name, or null if not found. getInputStream(ZipEntry entry) : InputStream – Returns an input stream for reading the contents of the specified zip file entry.


1 Answers

I am not sure what do you mean by particaly? Do you mean do it yourself without of API help?

In the case you don't mind using some opensource library, there is a cool API for that out there called zip4J

It is easy to use and I think there is good feedback about it. See this example:

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

If the files you want to unzip have passwords, you can try this:

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(); } 

I hope this is useful.

like image 101
javing Avatar answered Oct 28 '22 11:10

javing