How To Read And Print All Files From A Zip File In Java

A compressed file, known as a zip file, allows multiple files to be stored within a single file, making it an efficient choice for managing large amounts of data. In Java, the java.util.zip package offers classes for handling zip files, enabling developers to extract and display the contents of a zip file.

The ZipEntry class is employed to indicate the zip file and its contents, whereas the ZipFile class is utilized to retrieve the contents of the zip file. Within the code, we instantiate a ZipFile object for the specific zip file we intend to access and subsequently retrieve a list of entries in the zip file by invoking the entries method.

Next, we iterate through every item in the compressed file and display its Name by utilizing the getName function. In order to access the data within each item, we acquire an input stream for that item with the getInputStream function. Subsequently, we establish a buffer to store the data from the input stream and read the data into the buffer through an iteration. Ultimately, we transform the data in the buffer into a string format and output it.

In the event that the zip archive is not located, the code will generate an IOException displaying the error message "File not found". This exception can be managed by implementing a try-catch block.

Approach: Reading and Printing All Files from a Zip File

Imagine a scenario where there is a system that holds a zip file under the name Reference.zip. In this situation, instances of these classes are generated and set to a null state initially. Subsequently, the system establishes a file input stream to access the zip file and a buffered input stream to peruse its data. By utilizing a while loop, the program is designed to extract and display the names of files within the zip until the termination condition is met.

Algorithm:

  • Initialize FileInputStream, ZipInputStream, and ZipEntry object to null.
  • Allow the user to provide the zip file's location while reading the input.
  • Open a FileInputStream and enter the zip file's path(C:\\Users\\maddu\\OneDrive\\Desktop\\Watcher\\Reference.zip).
  • Create a BufferedInputStream to read the zip file's contents.
  • Construct a ZipInputStream so that you can read zip file entries and output their names while using a while loop.
  • Handle exceptions during file handling and close the ZipInputStream and BufferedInputStream.
  • Implementation:

Filename: ReadFile.java

Example

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ReadFile {

  // The method reads and prints the content of the zip file
  public void printFileContent(String filePath) {
    FileInputStream fs = null;
    ZipInputStream Zs = null;
    ZipEntry ze = null;

    try {
      // Display a message when program compiles successfully
      System.out.println("Files in the zip are as follows: ");

      // Open a file input stream to read the zip file
      fs = new FileInputStream(filePath);
      // Create a buffered input stream to read the contents of the zip file
      Zs = new ZipInputStream(new BufferedInputStream(fs));

      // Loop to read and print the zip file name till the end
      while ((ze = Zs.getNextEntry()) != null) {
        System.out.println(ze.getName());
      }

      // Close the file connection
      Zs.close();
    } catch (FileNotFoundException fe) {
      // Catch block to handle if zip file is not found
      fe.printStackTrace();
    } catch (IOException ie) {
      // Catch block to handle any generic IO exception
      ie.printStackTrace();
    }
  }

  public static void main(String[] args) {
    // Enter the path of the zip file here
    String filePath = "C:\\Users\\maddu\\OneDrive\\Desktop\\Watcher\\Reference.zip";

    // Create an object of the ReadFile class
    ReadFile zf = new ReadFile();

    // Call the printFileContent method to read and print the zip file content
    zf.printFileContent(filePath);
  }
}

Output:

Output

Files in the zip are as follows: 
Bad Operand Types Error in Java.docx
Chained Exceptions in Java.docx
ConcurrentSkipListSet in Java.docx
Final static variable in Java.docx
Image1.jpg
Image2.jpg
Image3.jpg
Minimum number of subsets with distinct elements.docx
Separators In Java.docx

Input Required

This code uses input(). Please provide values below: