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
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
- Archieve.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: Archieve.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.
- 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.
Zip file chosen: Archieve.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:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.