zip

Search file in a zip file

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: June 14th, 2013
0 277 1 minute read

This is an example of how to search aFile in a zip file, using theZipFile class. Searching a File in a zip file implies that you should:

  • Create a newZipFile and open it for reading.
  • Get the enumeration of the ZipFile entries, withentries() API method of ZipFile and iterate through each one of them.
  • For each one of the entries, get its name, withgetName() API method ofZipEntry.
  • If the name is equal to the name of the file we are searching, then return true, else false.
  • Close the ZipFile, withclose() API method of ZipFile.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;import java.io.IOException;import java.util.Enumeration;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;public class SearchFileInAZipFile {public static void main(String[] args) {String searchFile = "seek.txt";ZipFile zipFile = null;boolean fileFound = false;try {// open a zip file for readingzipFile = new ZipFile("c:/archive.zip");// get an enumeration of the ZIP file entriesEnumeration<? extends ZipEntry> e = zipFile.entries();while (e.hasMoreElements()) {ZipEntry entry = e.nextElement();// get the name of the entryString entryName = entry.getName();if (entryName.equalsIgnoreCase(searchFile)) {fileFound = true;break;}}}catch (IOException ioe) {System.out.println("Error opening zip file" + ioe);} finally { try { if (zipFile!=null) { zipFile.close(); } } catch (IOException ioe) {System.out.println("Error while closing zip file" + ioe); } }System.out.println("File found: " + fileFound);}}

 
This was an example of how to search a File in a zip file in Java.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: June 14th, 2013
0 277 1 minute read
Photo of Byron Kiourtzoglou

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor atJava Code Geeks.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.