Javatpoint Logo
Javatpoint Logo

Read and Print All Files From a Zip File in Java

A zip file is a type of compressed file that makes it possible to store several files inside of one file, making it the best option for storing big data. To read and print the contents of a zip file using Java, you can use the java.util.zip package, which provides classes for reading and writing zip files.

The ZipEntry class is used to mark the zip file and its contents, while the ZipFile class is used to read the contents of the zip file. In the code, we create a ZipFile object for the zip file we want to read and then get a list of entries in the zip file using the entries() method.

We then loop through each entry in the zip file and print its Name using the getName() method. To read the contents of each entry, we get an input stream for the entry using the getInputStream() method. We then create a buffer to hold the input stream data and read the data into the buffer using a loop. Finally, we convert the buffer data to a string and print it.

If the zip file is not found, the code will throw an IOException with the message "File not found" which can be handled using a try-catch block.

Approach: Reading and Printing All Files from a Zip File

Considering a system that stores a zip file named Reference.zip. It creates objects of these classes and initializes them to null. Then, it opens a file input stream to read the zip file and a buffered input stream to read its contents. The while loop is used to read and print the zip file names until the very last line.

Algorithm:

  1. Initialize FileInputStream, ZipInputStream, and ZipEntry object to null.
  2. Allow the user to provide the zip file's location while reading the input.
    Read and Print All Files From a Zip File in Java
  3. Open a FileInputStream and enter the zip file's path(C:\\Users\\maddu\\OneDrive\\Desktop\\Watcher\\Reference.zip).
    Read and Print All Files From a Zip File in Java
  4. Create a BufferedInputStream to read the zip file's contents.
  5. Construct a ZipInputStream so that you can read zip file entries and output their names while using a while loop.
  6. Handle exceptions during file handling and close the ZipInputStream and BufferedInputStream.

Implementation:

Filename: ReadFile.java

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






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA