Retrieving a file from a jar file

JAR file in Java is a compressed format and is used for packaging of the deliverables. At times, you may want to manipulate this file.
Below example indicates a scenario for the same.

import java.util.jar.*;
import java.io.*;

public class RetrievingJarEntry
{
   public static void main(String args[])
   {
      RetrievingJarEntry retrievingJarEntry = new RetrievingJarEntry();
      retrievingJarEntry.proceed();
   }
   
   private void proceed()
   {
      String sourceJarFile = “files/contacts.jar”;
      String sourceFile = “2.txt”;
      String destFile = “files/new2.txt”;
      try{
         
         JarFile jarFile = new JarFile(sourceJarFile);
         JarEntry jarEntry = jarFile.getJarEntry(sourceFile);
         System.out.println(“Found entry: ” + jarEntry);
         if ( jarEntry != null)
         {
            //Getting the jarEntry into the inputStream
            InputStream inputStream = jarFile.getInputStream(jarEntry); 
            //Creating a output stream to a new file of our choice
            FileOutputStream fileOutputStream = new java.io.FileOutputStream(destFile);
            System.out.println(“Attempting to create file: ” + destFile);
            while (inputStream.available()  0) 
            {  
               fileOutputStream.write(inputStream.read());
            }
            System.out.println(“Created file: ” + destFile);
            fileOutputStream.close();
            inputStream.close();
         }
      }catch(IOException ioe)
      {
         System.out.println(“Exception: ” + ioe);
      }
   }
}

/*

Expected output:

[[email protected]]# java RetrievingJarEntry
Found entry: 2.txt
Attempting to create file: files/new2.txt
Created file: files/new2.txt

*/

//Please note: You have to create a folder with name files and a jar file contacts.jar which has files 1.txt, 2.txt and 3.txt

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

How to Manage Your Finances after Buying a Home

How to Manage Your Finances after Buying a Home

Buying a home is a milestone in the journey of life – it’s one way to invest your money and create lasting memories. Now you know everything about a home purchase, home mortgage, and what is a conventional 97 loan but do you know what’s next after you sign the

Windows Logging is one of the vital aspects of any Windows system administration. However, it is mostly overlooked until the system develops a problem. This is because logs contain important information needed to troubleshoot and resolve system issues.

The Fundamentals of Windows Logging

Windows Logging is one of the vital aspects of any Windows system administration. However, it is mostly overlooked until the system develops a problem. This is because logs contain important information needed to troubleshoot and resolve system issues. Without it, tech experts might find it difficult to track a computer’s

Interstitial Ads: Best Practices for Successful Campaigns

Interstitial Ads: Best Practices for Successful Campaigns

Interstitial Ads: Best Practices for Successful Campaigns Interstitial ads are full-screen advertisements that appear to grasp the attention of on-site prospects, creating opportunities for brands seeking effective ways to communicate their proposition of value. With such an attention-grabbing format and high-impact visuals, it’s no wonder why interstitial advertising is proving

positive contribution tech

Technology’s Positive Contributions to Society

Technology has and continues to improve our lives. From the business world to the world of medicine, and our day-to-day lives, you can’t go a day without interacting with at least one form of technology. While some fear technology may be going too far, there are many ways in which

How to Choose From The Best Big Data Platforms in 2023

How to Choose From The Best Big Data Platforms in 2023

As big data continues to become increasingly popular in the business world, companies are always looking for better ways to process and analyze complex data. The process critically depends on the platform that manages and analyzes the data. In this article, we will provide a guide to help you choose