Open In App

Java.util.jar.JarInputStream class in Java

The JarInputStream class is used to read the contents of a JAR file from any input stream. It extends the class java.util.zip.ZipInputStream with support for reading an optional Manifest entry. The Manifest can be used to store meta-information about the JAR file and its entries.
Constructors

Methods:




//Java program demonstrating JarInputStream methods
  
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;
  
class JarInputStreamDemo extends JarInputStream
{
  
    public JarInputStreamDemo(InputStream in) throws IOException 
    {
        super(in);
    }
    public static void main(String[] args) throws IOException
    {
        FileInputStream is = new FileInputStream("codechecker.jar");
        JarInputStream jis = new JarInputStream(is);
        JarInputStreamDemo obj=new JarInputStreamDemo(jis);
  
        //illustrating createZipEntry() method
        ZipEntry ze1=obj.createZipEntry("ZipEntry");
        System.out.println(ze1.getName());
  
        //illustrating getNextEntry() method
        ZipEntry ze=jis.getNextEntry();
        System.out.println(ze.getName());
  
        //illustrating getManifest();
        System.out.println(jis.getManifest());
  
        // Reading from the current JAR file entry
        // into an array of 10 bytes
        byte b[] = new byte[10];
  
        //illustrating getNextJarEntry()
        //illustrating read(byte b[],int off,int length)
        while(jis.getNextJarEntry()!= null)
            jis.read(b);
        System.out.print(Arrays.toString(b));
  
        //closing the stream
        jis.close();
    }
}

Output :

Zipentry
Attention-64.png
java.util.jar.Manifest@513ee0c5
[-119, 80, 78, 71, 13, 10, 26, 10, 0, 0]

Article Tags :