Java 8 JarFile Stream

By Yashwant Chavan, Views 2267, Date 06-Nov-2016

In this tutorial you will learn about JarFile.stream() method. It return an ordered Stream over the ZIP file entries. Entries appear in the Stream in the order they appear in the central directory of the ZIP file.

tags java

Refer below steps.

  • Creates a new JarFile instance to read from the specified file name.
  • JarFile.stream() return an ordered Stream over the ZIP/Jar file entries.
  • Iterate and print each JarEntry details using forEach() loop.
  • In second example perform filter operation on Stream to find particular file name inside jar file.
  • Define Predicate for filter() operation as file -> file.getName().contains("BasicDynaBean.class"), Which will returns a stream consisting of the elements of this stream that match with given predicate condition.
  • Print resultant Optional<JarEntry> details.

Print all files inside jar using JarStream

package com.technicalkeeda.app;

import java.io.IOException;
import java.util.Optional;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Java8JarStream {
    public static void main(String[] args) {
        try {

            System.out.println("Print all files inside Jar");

            JarFile jarFile = new JarFile("C:\\demo\\commons-beanutils-1.8.3.jar");
            jarFile.stream().forEach(file -> System.out.println("Files :- " + file));

            Optional <JarEntry> searchResult = jarFile.stream()
                .filter(file -> file.getName().contains("BasicDynaBean.class")).findFirst();

            System.out.println("\nSearch for perticular file");
            System.out.println(searchResult.get());

            jarFile.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

output

Print All Files
Files :- META-INF/
Files :- META-INF/MANIFEST.MF
Files :- org/
Files :- org/apache/
Files :- org/apache/commons/
Files :- org/apache/commons/beanutils/
Files :- org/apache/commons/beanutils/converters/
Files :- org/apache/commons/beanutils/expression/
Files :- org/apache/commons/beanutils/locale/
Files :- org/apache/commons/beanutils/locale/converters/
Files :- org/apache/commons/collections/
Files :- META-INF/LICENSE.txt
Files :- META-INF/NOTICE.txt
Files :- org/apache/commons/beanutils/BasicDynaBean.class
Files :- org/apache/commons/beanutils/BasicDynaClass.class
Files :- org/apache/commons/beanutils/BeanAccessLanguageException.class
Files :- org/apache/commons/beanutils/BeanComparator.class
Files :- org/apache/commons/beanutils/BeanMap$1.class
Files :- org/apache/commons/beanutils/BeanMap$10.class
.....
.....
.....
.....
Files :- org/apache/commons/collections/FastHashMap$KeySet.class
Files :- org/apache/commons/collections/FastHashMap$Values.class
Files :- org/apache/commons/collections/FastHashMap.class
Files :- META-INF/maven/
Files :- META-INF/maven/commons-beanutils/
Files :- META-INF/maven/commons-beanutils/commons-beanutils/
Files :- META-INF/maven/commons-beanutils/commons-beanutils/pom.xml
Files :- META-INF/maven/commons-beanutils/commons-beanutils/pom.properties

Search Perticular File
org/apache/commons/beanutils/BasicDynaBean.class
Yashwant Chavan

Yashwant Chavan

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan@gmail.com