Java ZipFile getInputStream() function with examples


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 :



  • The function throws IllegalStateException if the zip file has been closed
  • The function throws ZipException if a ZIP format error has occurred
  • The function throws IOException if an I/O error has occurred
  • 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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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());
            }
        }
    }

    chevron_right

    
    

    Output:

    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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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());
            }
        }
    }

    chevron_right

    
    

    Output:

    null
    

    Error is thrown by the function.

    Reference: https://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipFile.html#getInputStream(java.util.zip.ZipEntry)



    My Personal Notes arrow_drop_up

    Second year Department of Information Technology Jadavpur University

    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.