Example of java code to uncompress and extract files from a compressed zip file using java.util.zip
package.
The example opens a zip file and starts traversing the files in a similar manner used in walking a directory tree. If we find a directory entry, we create a new directory. If we find a file entry, we write the decompressed file.
1. Java Unzip File Example
The Example uses ZipInputStream to read a ZipFile and then read all the ZipEntry one by one. Then uses FileOutputStream to write all the files into file system.
import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Main { public static void main(String[] args) { //Open the file try(ZipFile file = new ZipFile("files.zip")) { FileSystem fileSystem = FileSystems.getDefault(); //Get file entries Enumeration<? extends ZipEntry> entries = file.entries(); //We will unzip files in this folder String uncompressedDirectory = "uncompressed/"; Files.createDirectory(fileSystem.getPath(uncompressedDirectory)); //Iterate over entries while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); //If directory then create a new directory in uncompressed folder if (entry.isDirectory()) { System.out.println("Creating Directory:" + uncompressedDirectory + entry.getName()); Files.createDirectories(fileSystem.getPath(uncompressedDirectory + entry.getName())); } //Else create the file else { InputStream is = file.getInputStream(entry); BufferedInputStream bis = new BufferedInputStream(is); String uncompressedFileName = uncompressedDirectory + entry.getName(); Path uncompressedFilePath = fileSystem.getPath(uncompressedFileName); Files.createFile(uncompressedFilePath); FileOutputStream fileOutput = new FileOutputStream(uncompressedFileName); while (bis.available() > 0) { fileOutput.write(bis.read()); } fileOutput.close(); System.out.println("Written :" + entry.getName()); } } } catch(IOException e) { e.printStackTrace(); } } }
2. How Unzipping Works?
Below listed things are happening in sequence in the above example.
- ZipFile object represents the
.zip
file and is used to access its information. - ZipEntry class represents an entry in the zip file – either file or directory.
- Each
ZipEntry
instance has the compressed and uncompressed size information, the name, and the input stream of the uncompressed bytes. - Using InputStream and BufferedInputStream, we read the uncompressed bytes into a byte buffer to then use FileOutputStream to write it to a file.
- Keep doing it until whole file is processed.
Happy Learning !!
This code is insecure and vulnerable to the Zip Slip vulnerability.
https://snyk.io/research/zip-slip-vulnerability
This code works absolutely perfect.
But with a huge file, this takes a lot of lots of time. Is there any way to increase the speed.
Hi,
we have a code that decompress the f zip files.
is there anyway to catch the filenames.
example:
i have 1.zip
2.zip
3.zip
1.pdf
4.zip
1.txt
can we catch the path as \1.zip\2.zip\3.zip\1.pdf
\1.zip\2.zip\4.zip\1.txt
for above compressions
for knowing which file belongs to which compression
Excellent Article…
this article saved my ton of time.
Hi, I am running into an issue where I have multiple levels of directories within a zip file. It seems that in some cases the code doesn’t work correctly because I am trying to unzip a file whose directory has not been created yet. The above code seems to have an expectation that the entry that represents the directory will come before any entries for files within that directory, but that does not seem to be the case. Any ideas?
Hi, if i have files in side directory, how can i unzip it, can i take another while loop inside of directory or does it works with your code?
Excelente aporte, gracias por el articulo.
Excellent contribution. Thanks for the article.