Read and Print all files from a zip file in Java
A zip file is a compressed archive file format used to store multiple files and folders into a single file. It is commonly used to reduce file size for easier storage and transmission. Here in this zip file first we have to read the contents of the file using a Java program that uses java.util.zip.ZipEntry class for reading the contents of the zip file and then the contents inside the zip file will be printed. If the zip file is not found then it will throw NoSuchFileException.
Example 1:
Input
Path: D:/ javawork.zip

Output:
File: Javawork/Java Lambda Expression with Collections.docx File: Javawork/Java Program to add Two Binary Strings.docx File: Javawork/largest palindrome by changing at most K-Digits in Java.docx File: Javawork/Marker Interface in Java.docx
Explanation: In This example, javawork.zip contains the 4 files that areJavawork/Java Lambda Expression with Collections.docx, Javawork/Java Program to add Two Binary Strings.docx, Javawork/largest palindrome by changing at most K-Digits in Java.docx, Javawork/Marker Interface in Java.docx. so this will be the output.
Example 2:
Input
Path: D:/ javaw.zip

Output:
java.nio.file.NoSuchFileException: D:\javaw.zip
Explanation: Here in this case javaw.zip file is not present in the operating system so the program is unable to find that file path that’s why it’s throwing java.nio.file.NoSuchFileException.
Approach: Enumeration-based
The approach shown in the code example is using the java.util.zip package to read and print files from a zip file. It utilizes the ZipFile class to open and access the contents of the zip file, and the ZipEntry class to represent individual entries (files and folders) within the zip file. In this code, the approach is based on the standard Java libraries for working with zip files and is suitable for basic zip file operations in Java.
FileName: ZipFileReader.java
import java.io.IOException; import java.nio.file.*; import java.util.Enumeration; import java.util.zip.*; public class ZipFileReader { public static void main(String[] args) { // Path to the zip file String zipFilePath = "D:/javawork.zip"; try (ZipFile zipFile = new ZipFile(zipFilePath)) { //Here we get an enumeration of all entries inside the zip file Enumeration<? extends ZipEntry> entries = zipFile.entries(); // Iterate over each entry while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); // Check if the current entry is a file (not a directory) if (!entry.isDirectory()) { // Print the name of the file System.out.println("File: " + entry.getName()); // Read the content of the file here if needed } } } catch (IOException e) { e.printStackTrace(); } } }
Output
Mentioned files are present in the folder Path: D:/ javawork.zip
File: Javawork/Java Lambda Expression with Collections.docx File: Javawork/Java Program to add Two Binary Strings.docx File: Javawork/largest palindrome by changing at most K-Digits in Java.docx File: Javawork/Marker Interface in Java.docx
The following program shown is the same as the previous one. The only difference in this example is that the file path given in the example is not present in the operating system so it will be going to throw an exception named NoSuchFileException.
FileName: ZipFileReader.java
import java.io.IOException; import java.nio.file.*; import java.util.Enumeration; import java.util.zip.*; public class ZipFileReader { public static void main(String[] args) { // Path to the zip file String zipFilePath = "D:/javaw.zip"; try (ZipFile zipFile = new ZipFile(zipFilePath)) { // Get an enumeration of all entries in the zip file Enumeration<? extends ZipEntry> entries = zipFile.entries(); // Iterate over each entry while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); // Check if the current entry is a file (not a directory) if (!entry.isDirectory()) { // Print the name of the file System.out.println("File: " + entry.getName()); // Read the content of the file here if needed } } } catch (IOException e) { e.printStackTrace(); } } }
Output:
The given folder doesn’t contain any file Path: D:\javaw.zip
java.nio.file.NoSuchFileException: D:\javaw.zip
Approach: Using a ZipInputStream
In this approach, we are using a ZipInputStream to read the zip file's contents. Then we create a ZipEntry object to represent each entry (file or directory) within the zip file. The getNextEntry() method is used to iterate over each entry in the zip file.
FileName: ZipFileReader1.java
import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class ZipFileReader1 { public static void main(String[] args) { // Path to the zip file String zipFilePath = "D:/javawork.zip"; try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath))) { ZipEntry entry; // Iterate over each entry in the zip file while ((entry = zipInputStream.getNextEntry()) != null) { // Check if the current entry is a file (not a directory) if (!entry.isDirectory()) { // Print the name of the file System.out.println("File: " + entry.getName()); // Read the content of the file here if needed } zipInputStream.closeEntry(); } } catch (IOException e) { e.printStackTrace(); } } }
Output
Mentioned files are present in the folder Path: D:/ javawork.zip
File: Javawork/Java Lambda Expression with Collections.docx File: Javawork/Java Program to add Two Binary Strings.docx File: Javawork/largest palindrome by changing at most K-Digits in Java.docx File: Javawork/Marker Interface in Java.docx