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 java.util.zip.ZipEntry 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
Illustration:
Considering a system that stores a zip file named
geekforgeeks.zip at D drive in windows operating system D:/geeks.zip in windows operating system Archive.zip in macOS operating system
Case 1: geekforgeeks.zip
Input : D:/geekforgeeks.zip
Output : The files in the Zip are as follows:
Java program to print a number.java
Java program to print your name.java
Java program to calculate.java
Java program to print a pattern.java
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:/geeks.zip
Input : D:/geeks.zip
Output : java.io.FileNotFoundException: D:/geeks.zip (The system cannot find the file specified)
Case 3: Archive.zip
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.
Approach
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.
Example
Java
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 {
public void printFileContent(String filePath)
{
FileInputStream fs = null;
ZipInputStream Zs = null;
ZipEntry ze = null;
try {
System.out.println(
"Files in the zip are as follows: ");
fs = new FileInputStream(filePath);
Zs = new ZipInputStream(
new BufferedInputStream(fs));
while ((ze = Zs.getNextEntry()) != null) {
System.out.println(ze.getName());
}
Zs.close();
}
catch (FileNotFoundException fe) {
fe.printStackTrace();
}
catch (IOException ie) {
ie.printStackTrace();
}
}
public static void main(String[] args)
{
GFG zf = new GFG();
Scanner sc = new Scanner(System.in);
System.out.println(
"Enter the location of the zip file: ");
String str = sc.nextLine();
zf.printFileContent(str);
}
}
Output:
Zip file chosen: Archive.zip
Zip file directory on local computer: /Users/mayanksolanki/Desktop/Job/Geekathon/Archive.zip
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:
Q1.Vaidik.Try2.png
__MACOSX/._Q1.Vaidik.Try2.png
Q2.Vaidik.Rry1.Output.png
__MACOSX/._Q2.Vaidik.Rry1.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
Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule. What We Offer:
Comprehensive Course
Expert Guidance for Efficient Learning
Hands-on Experience with Real-world Projects
Proven Track Record with 100,000+ Successful Geeks
Last Updated :
29 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...