This is a simple Java example to search for a file entry in a zip file.The java class uses java util zip package utilities for the same.
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZipFileSearcher {
public static void main(String args[]) {
try
{
// Read the zip file from location mentioned
//If File not found throws IO Exception
ZipFile sourceZipFile = new ZipFile("c:/demo.zip");
// Input file to search
String filetoSearch = "getfile1.txt";
Enumeration e = sourceZipFile.entries();
boolean found = false;
System.out.println("Searching " + filetoSearch + " in " + sourceZipFile.getName());
// loop all the entries in the zip file
while(e.hasMoreElements())
{
ZipEntry zipFile = (ZipEntry)e.nextElement();
//validate zip file entry against the file to search
if(zipFile.getName().toLowerCase().indexOf(filetoSearch) != -1)
{
found = true;
// entry found condition
System.out.println("File Found " + zipFile.getName());
break;
}
}
// entry not found condition
if(found == false)
{
System.out.println("File :" + filetoSearch + " Not Found in Zip File: " + sourceZipFile.getName());
}
// close the source file
sourceZipFile.close();
}
// handle IO exception
catch(IOException ioe)
{ System.out.println("Error opening zip file" + ioe);
}
}
}