Solved

adding new entry to existing jar files

Posted on 2001-08-08
5
238 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
ID: 6365234
what I am doing keeps creating a new file with a single entry only.  
0
 
LVL 27

Expert Comment

by:rrz
ID: 6365550
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
ID: 6366346
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
ID: 6378966
tks vm . CHEERS
0
 
LVL 92

Expert Comment

by:objects
ID: 6379097
thanks for the points :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
javap bin 2 40
how to add new optional parameter to JSP 1 49
HashTable highest marks enumeration alternative 9 42
Desingning Refactoring existing code 2 26
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

809 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