Open In App
Related Articles

Java ZipFile getInputStream() function with examples

Improve Article
Improve
Save Article
Save
Like Article
Like


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.




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

    
    

    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.




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

    
    

    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)


    Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

    Last Updated : 06 Mar, 2019
    Like Article
    Save Article
    Previous
    Next
    Similar Reads
    Complete Tutorials