Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

adding new entry to existing jar files

Posted on 2001-08-08
5
Medium Priority
?
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

609 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