Solved

adding new entry to existing jar files

Posted on 2001-08-08
5
237 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
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…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

813 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

17 Experts available now in Live!

Get 1:1 Help Now