The getInputStream() function is a part of java.util.zip package. The function returns the InputStream of a specific ZipEntry passed as parameter.Closing the Zip File will also close all the InputStream generated by this function.
Function Signature :
public InputStream getInputStream(ZipEntry e)
Syntax :
zip_file.getInputStream(entry);
Parameters : The function takes a ZipEntry object as parameter.
Return value : The function returns a InputStream Object to read the contents of the ZipFile Entry.
Exceptions :
Below programs illustrates the use of getInputStream() function
Example 1: We will create a file named zip_file and get the zip file entry using getEntry() function and then get the Input Stream object to read the contents of the file.”file.zip” is a zip file present in f: directory.
// Java program to demonstrate the // use of getInputStream() function import java.util.zip.*; import java.util.Enumeration; import java.util.*; import java.io.*; 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" ); // get the Input Stream // using the getInputStream() // function InputStream input = zip_file.getInputStream(entry); // Create a scanner object Scanner sc = new Scanner(input); System.out.println( "Contents:" ); // Display the contents Zip Entry while (sc.hasNext()) { System.out.println(sc.nextLine()); } // Close the scanner sc.close(); } catch (Exception e) { System.out.println(e.getMessage()); } } } |
Contents: This is a file in ZIP file.
Example 2: We will create a file named zip_file and get the zip file entry using getEntry() function and then get the Input Stream object to read the contents of the file.”file4.cpp” is not present in the zip file.
// Java program to demonstrate the // use of getInputStream() function import java.util.zip.*; import java.util.Enumeration; import java.util.*; import java.io.*; 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" ); // Get the Input Stream // using the getInputStream() // function InputStream input = zip_file.getInputStream(entry); // Create a scanner object Scanner sc = new Scanner(input); System.out.println( "Contents:" ); // Display the contents Zip Entry while (sc.hasNext()) { System.out.println(sc.nextLine()); } // Close the scanner sc.close(); } catch (Exception e) { System.out.println(e.getMessage()); } } } |
null
Error is thrown by the function.