Home » Java programs » Java file handling programs

Java program to read and print all files from a zip file

In this java program, we are going to learn how to read a zip file and access all files from it? Here, we will have a zip file, read files from it and print file names.
Submitted by IncludeHelp, on November 03, 2017

Given a zip file, and we have to print all files names from it using java program.

To implement this java program, we are going to use main classes FileInputStream to manage normal files by creating its object, and ZipInputStream to manage zip files by creating its object.

There are two other methods of ZipInputStream class, which are:

  1. getNextEntry() - this will check whether next file (while calling it in the loop) is available in the zip file or not.
  2. getName() - to get file’s name from the zip file.

Program to extract the names of files from zip file in java

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class FindFileInZipFile
	public void printFileList(String filePath)
		// initializing the objects.
		FileInputStream fis = null;
		ZipInputStream Zis = null;
		ZipEntry zEntry = null;
			fis = new FileInputStream(filePath);
			Zis = new ZipInputStream(new BufferedInputStream(fis));

			// this will search the files while end of the zip.
			while((zEntry = Zis.getNextEntry()) != null)
		catch (FileNotFoundException e) 
		catch (IOException e) 
	//main function
	public static void main(String a[])
		// creating object of the file.
		FindFileInZipFile fff = new  FindFileInZipFile();
		System.out.println("Files in the Zip are : ");

		// enter the path of the zip file with name.


Files in the Zip are : 

Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com (2015-2018), Some rights reserved.