Read the contents of Zip file without Unzip in Java

Hey guys in this post, we will discuss reading the contents of a zip file using ZipFile package in Java. ZipFile is a, it provides some really useful methods and constructor for reading the zip file. let’s look at some of the important methods and constructor.


  • ZipFile(String filePath): This constructor takes the string as parameter. We will pass the zip file path.
    Ex: ZipFile(“D:\\”)
  • ZipFile(File file): This constructor takes the file object as parameter. We will pass the object of the file.
    Ex: ZipFile(new File(“D:\\”))


  • 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.
  • getName(): String – returns the path name of the ZIP file.
  • entries(): Enumeration<? extends ZipEntry> – returns an enumeration of the ZIP file entries.

Let’s look at an example, i have a zip file it contains few files like, excel, pdf, image etc and i stored it in my D:// drive.

import java.util.Enumeration;

public class ReadZipFile {
	public static void main(String[] args) throws IOException {
		ZipFile zip = null;
		try {
			zip = new ZipFile("D:\\");

			for (Enumeration<?> e = zip.entries(); e.hasMoreElements();) {
				ZipEntry entry = (ZipEntry) e.nextElement();
				if (!entry.isDirectory()) {
		} catch (Exception e) {
		} finally {


Here, the important methods to keep in mind is that,

  • isDirectory() This will return boolean telling that whether the zip file contains any folder or not. Here i am ignoring the directory.
  • hasMoreElements() This will return boolean letting us know whether the elements are present or not

This will produce the following output –

fr_leading traling vth spec chars_CSV (1).csv
Pro Angular 6, 3rd Edition.pdf

That’s it for this post, i will see you in the next post.

Reference links:

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply