ZipFile getEntry() function in Java with examples
The getEntry() function is a part of java.util.zip package. The function returns the ZipEntry of file present in the zip file specified by the string parameter.
Function Signature:
public ZipEntry getEntry(String name)
Syntax:
zip_file.getEntry();
Parameters: The function takes a string parameter, the name of the file.
Return value: The function returns the ZipEntry of the zip file specified by the string parameter.
Exceptions: The function throws IllegalStateException if the zip file has been closed.
Below programs illustrates the use of getEntry() function
Example 1: Create a file named zip_file and get the zip file entry using getEntry() function. “file.zip” is a zip file present in f: directory.
// Java program to demonstrate the // use of getEntry() function import java.util.zip.*; import java.util.Enumeration; public class solution { public static void main(String args[]) { try { // Create a Zip File ZipFile zip_file = new ZipFile("f:\\file.zip"); // get the Zip Entry using // the getEntry() function ZipEntry entry = zip_file.getEntry("file1.cpp"); // display the Zip Entry System.out.println("Name: " + entry.getName()); } catch (Exception e) { System.out.println(e.getMessage()); } } } |
Output:
Name: file1.cpp
Example 2: Create a file named zip_file and get the zip file entry using getEntry() function. “file.zip” is a zip file present in f: directory whereas “file4.cpp” is not present in the zip file.
// Java program to demonstrate the // use of getEntry() function import java.util.zip.*; import java.util.Enumeration; public class solution { public static void main(String args[]) { try { // Create a Zip File ZipFile zip_file = new ZipFile("f:\\file.zip"); // get the Zip Entry using // the getEntry() function ZipEntry entry = zip_file.getEntry("file4.cpp"); // display the Zip Entry System.out.println("Name: " + entry.getName()); } catch (Exception e) { System.out.println(e.getMessage()); } } } |
Output:
null
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipFile.html#getEntry(java.lang.String)
Recommended Posts:
- ZipFile size() function in Java with examples
- ZipFile getComment() function in Java with examples
- Java ZipFile getInputStream() function with examples
- ZipFile entries() function in Java with examples
- ZipFile getName() function in Java with examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Java SQL Timestamp before() function with examples
- Java SQL Timestamp after() function with examples
- Function Interface in Java with Examples
- Java SQL Timestamp getNanos() function with examples
- Deflater needsInput() function in Java with examples
- Java ZipEntry setLastModifiedTime() function with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



