Home  >  Core Java

Example of JarInputStream in Java

By Arvind Rai, January 17, 2014
java.util.jar.JarInputStream reads the jar from an input stream. If we read a jar normally as input stream, then we cannot know the jar details effectively in our application. Java provides the JarInputStream, that will convert input stream to jar input stream. Now with this instance we can fetch java.util.jar.JarEntry that will provide jar entry details. We can get the details of each entry of jar with its fully qualified name. JarInputStream also returns manifest details.

JarInputStream.getManifest()

It returns the java.util.jar.Manifest instance. Manifest will give the details of Manifest file which lies in jar.

JarInputStream.getNextJarEntry()

It returns the jar entry starting from beginning. When we call it iteratively, it returns each jar entry and when finished, it returns null.

JarEntry in Java

java.util.jar.JarEntry represents an entry of jar. A jar is the set of entries of classes and other files. While fetching details of entry, JarInputStream returns JarEntry to get details of any entry of a jar.
JarInputStreamDemo.java
package com.concretepage.util.jar;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
public class JarInputStreamDemo {
        public static void main(String[] args) {
           try {
               File file = new File("D:/cp/jar/testjar.jar");
               FileInputStream fis = new FileInputStream(file);
               JarInputStream jis = new JarInputStream(fis);
               System.out.println(jis.markSupported());
               JarEntry je;
               while((je=jis.getNextJarEntry())!=null){
                     //gives fully qualified name of classes inside jar
                     System.out.println(je.getName());
                     // gives the size of data per entry
                     System.out.println(je.getCompressedSize());
                    
                     //gives modification time
                     System.out.println(je.getTime());
                    
                     //gives comment per entry
                     System.out.println(je.getComment());
               }
               jis.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
         }
}
 
In the example, we are reading a jar from a given location. First it is read in file input stream, we pass this input stream to JarInputStream. Now to know the information of jar, we iterate it, and we get JarEntry. Using JarEntry, we can fetch the details of each jar entry. The information can be fetched like name, compressed size, modification time, comments etc per entry of a jar.
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us