Saturday, October 25, 2025
HomeLanguagesJavaZipFile entries() function in Java with examples

ZipFile entries() function in Java with examples

The entries() function is a part of java.util.zip package. The function returns the enumeration of zip file entries of the zip file.

Function Signature:

public Enumeration entries()

Syntax:

zip_file.entries();

Parameters: The function does not require any parameters

Return value: The function returns the enumeration of zip file entries of the zip file, the enumeration contains the ZipEntry of all the files in the zip file.

Exceptions: The function throws IllegalStateException if the zip file has been closed.

Below programs illustrates the use of entries() function

Example 1: Create a file named zip_file and get the zip file entries using entries() function. “file.zip” is a zip file present in f: directory.




// Java program to demonstrate the
// use of entries() function
  
import java.util.zip.*;
import java.util.Enumeration;
  
public class solution {
    public static void main(String args[])
    {
  
        try {
  
            // Create a Zip File
            ZipFile zip_file
                = new ZipFile("f:\\file.zip");
  
            // get the Zip Entries using
            // the entries() function
            Enumeration<? extends ZipEntry> entries
                = zip_file.entries();
  
            System.out.println("Entries:");
  
            // iterate through all the entries
            while (entries.hasMoreElements()) {
                // get the zip entry
                ZipEntry entry = entries.nextElement();
  
                // display the entry
                System.out.println(entry.getName());
            }
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}


Output:

Entries:
file3.cpp
file1.cpp
file2.cpp

Example 2: Create a file named zip_file and get the zip file entries using entries() function. This function throws exception if we close the file and then call the function entries().




// Java program to demonstrate the
// use of entries() function
  
import java.util.zip.*;
import java.util.Enumeration;
  
public class solution {
    public static void main(String args[])
    {
  
        try {
  
            // Create a Zip File
            ZipFile zip_file
                = new ZipFile("f:\\file.zip");
  
            // close the zip file
            zip_file.close();
  
            // get the Zip Entries using
            // the entries() function
            Enumeration<? extends ZipEntry> entries
                = zip_file.entries();
  
            System.out.println("Entries:");
  
            // iterate through all the entries
            while (entries.hasMoreElements()) {
  
                // get the zip entry
                ZipEntry entry = entries.nextElement();
  
                // display the entry
                System.out.println(entry.getName());
            }
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}


Output:

zip file closed

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipFile.html#entries()

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS