A ZIP file can contain multiple files and directories compressed into a single archive. Java provides classes from the java.util.zip package to read ZIP archives and access the entries stored inside them.
- ZipInputStream is used to read entries from a ZIP file sequentially.
- ZipEntry represents an individual file or directory stored inside the ZIP archive.
Steps to read and print files from a ZIP file
- Take the ZIP file path as input.
- Create a ZipInputStream for the specified ZIP file.
- Read each ZipEntry using getNextEntry().
- Print the name of every entry until all entries are processed.
Approach
- Read the location of the ZIP file from the user.
- Pass the file path to a method that processes the ZIP archive.
- Use ZipInputStream to access each entry in the archive.
- Use getNextEntry() to move through the files one by one.
- Print the name of each file or directory present in the ZIP file.
- Handle FileNotFoundException if the specified ZIP file does not exist and IOException for other file-related errors.
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
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
System.out.println(
"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) {
System.out.println(ze.getName());
}
// Closing the file connection
Zs.close();
}
// Catch block to handle if exception related
// to file handling occurs
catch (FileNotFoundException fe) {
// Print the line line and exception
// of the program where it occurred
fe.printStackTrace();
}
// Catch block to handle generic exceptions
catch (IOException ie) {
// Print the line line and exception
// of the program where it occurred
ie.printStackTrace();
}
}
// 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(System.in);
// Display message asking user to enter
// zip file local directory
System.out.println(
"Enter the location of the zip file: ");
String str = sc.nextLine();
// Print the zip files(compressed files)
zf.printFileContent(str);
}
}
Output
For example, if the ZIP file is located at:
/Users/mayanksolanki/Desktop/Job/Geekathon/Archive.zipand contains several files, the output will be:
Enter the location of the zip file:
/Users/mayanksolanki/Desktop/Job/Geekathon/Archive.zip
Files in the zip are as follows:
Q1.Vaidik.Try2.png
__MACOSX/_Q1.Vaidik.Try2.png
Q2.Vaidik.Ry1.Output.png
__MACOSX/_Q2.Vaidik.Ry1.Output.png
Q2.Vaidik.Try2.png
__MACOSX/_Q2.Vaidik.Try2.png
Q4.Vaidik.OSI.png
__MACOSX/_Q4.Vaidik.OSI.png
Q12Vaidik.Try1.png
__MACOSX/_Q12Vaidik.Try1.png
Explanation
- FileInputStream opens the ZIP file from the specified location.
- BufferedInputStream provides buffered access to the file data.
- ZipInputStream reads the ZIP archive and provides access to its entries.
- getNextEntry() returns the next ZipEntry in the archive. It returns null when there are no more entries.
- getName() returns the name and path of the current entry.
- close() closes the ZIP input stream after all entries have been processed.
- If the specified file does not exist, FileNotFoundException is handled by the catch block.
Note: This program prints the names of the files and directories inside the ZIP archive. It does not extract or print the actual contents of those files.
Advantages
- Simple: The program can list all entries in a ZIP file using only a few Java classes.
- No extraction required: Files can be listed without first extracting the ZIP archive.
- Sequential processing: getNextEntry() allows the entries to be processed one by one.
- Built-in support: ZIP handling is available through Java's standard library.
Limitations
- Does not extract files: The program only displays entry names.
- No file content is displayed: It does not read or print the actual contents of the files inside the ZIP.
- Exception handling is required: Missing files or other I/O problems can cause exceptions.
- ZIP-specific: ZipInputStream is designed for ZIP archives and cannot directly process other archive formats.