Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

adding new entry to existing jar files

Posted on 2001-08-08
5
Medium Priority
?
245 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 28

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 80 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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month20 days, 18 hours left to enroll

810 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