Solved

adding new entry to existing jar files

Posted on 2001-08-08
5
234 Views
Last Modified: 2008-03-03
Hi all

How can I addanother file to an existing jar file using programming.  Of course can use runtime and java -jar ...

But java programing , how to do it.  I am using

String file="C:\\thisfile.txt";

try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream("C:\\myjarfile.jar"));

BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));

ZipEntry entry = new ZipEntry("newfile.txt");
out.putNextEntry(entry);
byte[] buf = new byte[1024]; //read 1K at a time
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf,0,len);
}
in.close();

out.close();

0
Comment
Question by:TLTEO
  • 2
  • 2
5 Comments
 

Author Comment

by:TLTEO
Comment Utility
what I am doing keeps creating a new file with a single entry only.  
0
 
LVL 27

Expert Comment

by:rrz
Comment Utility
Have you seen this other question?
http://itworld.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20164786
The name of the question is
"ZipOutputstream Question". It was submitted by "jkb2" on 8/07/2001.
It might be helpful.

0
 
LVL 92

Accepted Solution

by:
objects earned 20 total points
Comment Utility
From my comment in the question mentioned above by rrz:

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

public class JarUpdate {
  /**
   * main()
   */
  public static void main(String[] args) throws IOException {
     // Get the jar name and entry name from the command-line.

     String jarName = args[0];
     String fileName = args[1];

     // Create file descriptors for the jar and a temp jar.
     
     File jarFile = new File(jarName);
     File tempJarFile = new File(jarName + ".tmp");

     // Open the jar file.

     JarFile jar = new JarFile(jarFile);
     System.out.println(jarName + " opened.");

     // Initialize a flag that will indicate that the jar was updated.

     boolean jarUpdated = false;

     try {
        // Create a temp jar file with no manifest. (The manifest will
        // be copied when the entries are copied.)

        Manifest jarManifest = jar.getManifest();
        JarOutputStream tempJar =
           new JarOutputStream(new FileOutputStream(tempJarFile));

        // Allocate a buffer for reading entry data.

        byte[] buffer = new byte[1024];
        int bytesRead;

        try {
           // Open the given file.
           
           FileInputStream file = new FileInputStream(fileName);

           try {
              // Create a jar entry and add it to the temp jar.
           
              JarEntry entry = new JarEntry(fileName);
              tempJar.putNextEntry(entry);

              // Read the file and write it to the jar.

              while ((bytesRead = file.read(buffer)) != -1) {
                 tempJar.write(buffer, 0, bytesRead);
              }

              System.out.println(entry.getName() + " added.");
           }
           finally {
              file.close();
           }

           // Loop through the jar entries and add them to the temp jar,
           // skipping the entry that was added to the temp jar already.

           for (Enumeration entries = jar.entries(); entries.hasMoreElements(); ) {
              // Get the next entry.

              JarEntry entry = (JarEntry) entries.nextElement();

              // If the entry has not been added already, add it.

              if (! entry.getName().equals(fileName)) {
                 // Get an input stream for the entry.
             
                 InputStream entryStream = jar.getInputStream(entry);

                 // Read the entry and write it to the temp jar.

                 tempJar.putNextEntry(entry);

                 while ((bytesRead = entryStream.read(buffer)) != -1) {
                    tempJar.write(buffer, 0, bytesRead);
                 }
              }
           }

           jarUpdated = true;
        }
        catch (Exception ex) {
           System.out.println(ex);
           
           // Add a stub entry here, so that the jar will close without an
           // exception.
           
           tempJar.putNextEntry(new JarEntry("stub"));
        }
        finally {
           tempJar.close();
        }
     }
     finally {
        jar.close();
        System.out.println(jarName + " closed.");

        // If the jar was not updated, delete the temp jar file.

        if (! jarUpdated) {
           tempJarFile.delete();
        }
     }

     // If the jar was updated, delete the original jar file and rename the
     // temp jar file to the original name.

     if (jarUpdated) {
        jarFile.delete();
        tempJarFile.renameTo(jarFile);
        System.out.println(jarName + " updated.");
     }
  }
}
0
 

Author Comment

by:TLTEO
Comment Utility
tks vm . CHEERS
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
thanks for the points :)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
commonTwo  challenge 63 98
Basics of Drool Rule Engine with few samples 2 39
topping2 challenge 13 55
bitbucket vs gitbucket 3 29
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now