I am currently working on this patch, however i found out that my new patch
does not work properly in that it increases the size of the jar file
considerably. For example , if i add a new file of size 300 kb to an
existing jar file of size 1 mb, then it adds the new file to the jar, but
increases the jar file to 1.9 mb. I tried to use jar command and add the
file , which increased the jar files size to 1.25 mb.

I then tried to use the Jar class from ANT and it works perfectly (same as
the jar command) and is much simpler to use. Do you think we could use the
ant classes for openejb project.? if yes, then below is the code which i
have written which adds a file to the jar file. i still needs to handle
exceptions e.g  file not found , if renaming files doesnt work etc, but just
as a POC, could you please look into it and let me know if it is ok and if i
can submit a patch  using ANT libraries

import java.io.File;
import java.io.IOException;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Jar;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.ZipFileSet;

public class JarUtilAnt {

   /**
    * @param args
    * @throws IOException
    */
   public static void main(String[] args) throws IOException {
       addFileToJar("c:/jars/ant.jar", "c:/test.xml");


   }
   public static void addFileToJar(String jarFile, String file) throws
IOException{
       Jar jar = new Jar();
       File f = new File(jarFile);
       File parentFile = f.getParentFile();
      File renamedFile = new File(parentFile,"temp.jar");
      f.renameTo(renamedFile);
       FileSet newFile = new FileSet();
       newFile.setFile(new File(file));
       ZipFileSet fileSet = new ZipFileSet();
       fileSet.setSrc(renamedFile);
       jar.addFileset(fileSet);
       jar.addFileset(newFile);
       File destFile = new File(jarFile);
       jar.setDestFile(destFile);
       Project p = new Project();
       jar.setProject(p);
       jar.executeMain();
       renamedFile.delete();

   }

}
On 1/11/07, David Blevins <[EMAIL PROTECTED]> wrote:


On Jan 8, 2007, at 6:09 PM, Karan Malhi wrote:

> Thanks Dain,
>
> I will make the suggested changes and submit another patch.

I just have to point out.... Most people tend to say "sorry" when
they get feedback, but you said "thanks."  Definitely the right
attitude to have!  Big thumbs up!

Thanks Karan and Dain.

-David

>
> On 1/8/07, Dain Sundstrom <[EMAIL PROTECTED]> wrote:
>>
>> This is a good start but I see some bugs :)
>>
>> comments in line...
>>
>> -dain
>>
>> On Jan 7, 2007, at 12:59 PM, [EMAIL PROTECTED] wrote:
>>
>> > +            JarInputStream jis = new JarInputStream(new
>> > FileInputStream(jarFile));
>> > +            File tempJar = File.createTempFile("temp", "jar");
>>
>> The temp file should be created in the same directory as the final
>> original file because on some platforms the temp directory is on a
>> separate drive and rename only works when the source and target file
>> are on the same drive.
>>
>> > +            JarOutputStream jos = new JarOutputStream(new
>> > FileOutputStream(tempJar));
>> > +            JarEntry nextJarEntry = null;
>> > +            while ((nextJarEntry = jis.getNextJarEntry()) !=
>> null) {
>> > +                jos.putNextEntry(nextJarEntry);
>> > +            }
>>
>> You need to copy the contents from jis to jos after putNextEntry.
>>
>> > +            jis.close();
>> > +            jos.putNextEntry(new JarEntry(file));
>> > +            FileInputStream fis = new FileInputStream(file);
>> > +            for (int c = fis.read(); c != -1; c = fis.read()) {
>> > +                jos.write(c);
>> > +            }
>>
>> Byte at a time is quite slow.  Instead I suggest using 4k blocks.
>>
>> > +            fis.close();
>> > +            jos.close();
>>
>> Close should be in a finally block.
>>
>> > +            File oldJar = new File(jarFile);
>> > +            oldJar.delete();
>> > +            tempJar.renameTo(oldJar);
>>
>> It is safer to move the old file aside, rename the temp jar, then
>> delete the old file.  That way if the rename fails, you can back
>> it out.
>>
>> > +        } catch (FileNotFoundException e) {
>> > +            throw new OpenEJBException(messages.format("file.
>> > 0003", file, jarFile, e.getMessage()));
>> > +        } catch (IOException e) {
>> > +            throw new OpenEJBException(messages.format("file.
>> > 0003", file, jarFile, e.getMessage()));
>> >          }
>> >
>> >      }
>>
>>
>
>
> --
> Karan Malhi




--
Karan Malhi

Reply via email to