Open In App
Related Articles

Java Program to Read and Print All Files From a Zip File

Like Article
Save Article
Report issue

A zip file is a file where one or more files are compressed together, generally, zip files are ideal for storing large files.  Here the zip file will first read and at the same time printing the contents of a zip file using a java program using the class for marking the zip file and after reading it, the contents inside it would be printed. If in-case, the zip file is not found the code will throw an input-output exception that FileNotFoundException


Considering a system that stores a zip file named

  • at D drive in windows operating system
  • D:/ in windows operating system
  • in macOS operating system

Case 1:

Input  : D:/
Output : The files in the Zip are as follows:
         Java program to print a
         Java program to print your
         Java program to
         Java program to print a

Now if a system does not store the specific zip file inputted by the user then the code would throw an exception and print a message that the file has not been found.

Case 2: D:/

Input  :  D:/
Output : D:/ (The system cannot find the file specified)

Case 3:

It is the same as case 1 so reserving this for the implementation part just on the different operating system as shown in the example with greater depth.


  • Take the location of the zip file as input in the main method.
  • The location of the zip file is now sent to the method.
  • In the method, the file is read and its content is printed.
  • If in-case the file is not found, the code would throw an exception.



// Java program to read and print all files
// from a zip file
// Importing input output classes
// Importing zip classes and Scanner class
// from java.util package
import java.util.Scanner;
// Class to Read and print the Zip Files
public class GFG {
    // Function to read and print the file names.
    public void printFileContent(String filePath)
        // Creating objects for the classes and
        // initializing them to null
        FileInputStream fs = null;
        ZipInputStream Zs = null;
        ZipEntry ze = null;
        // Try block to handle if exception occurs
        try {
            // Display message when program compiles
            // successfully
                "Files in the zip are as follows: ");
            fs = new FileInputStream(filePath);
            Zs = new ZipInputStream(
                new BufferedInputStream(fs));
            // Loop to read and print the zip file name till
            // the end
            while ((ze = Zs.getNextEntry()) != null) {
            // Closing the file connection
        // Catch block to handle if any exception related
        // to file handling occurs
        catch (FileNotFoundException fe) {
            // Print the line line and exception
            // of the program where it occurred
        // Catch block to handle generic exceptions
        catch (IOException ie) {
            // Print the line line and exception
            // of the program where it occurred
    // Main driver method
    public static void main(String[] args)
        // Creating an object of the file
        GFG zf = new GFG();
        // Taking input of the zip file from local directory
        // Name of the zip file to be read should be entered
        Scanner sc = new Scanner(;
        // Display message asking user to enter
        // zip file local directory
            "Enter the location of the zip file: ");
        String str = sc.nextLine();
        // Print the zip files(compressed files)



Zip file chosen:

Zip file directory on local computer: /Users/mayanksolanki/Desktop/Job/Geekathon/

As seen above the following zip file prints out 5 files inside it. (All image of .png format) 

Files in the zip are as follows: 












Last Updated : 29 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads