Update a file inside a JAR in a specific directory with jar command

前端 未结 6 649
礼貌的吻别
礼貌的吻别 2020-12-13 02:35

So I\'ve been looking high and low for an answer to this and obviously haven\'t found a satisfactory answer.

The problem is that I want to update a JAR (or any file

相关标签:
6条回答
  • 2020-12-13 02:48

    This previous discussion shows some Java source code for updating a zip (or jar) file.

    It's not entirely clear to me whether this will actually be any faster than using jar -u, but at least you should be able to adapt that code so that you don't have to recreate the file structure on disk.

    Edit: Here is the documentation on the 'jar' utility - you can see for yourself that the particular behavior that you're looking for, while it would be useful, isn't there.

    I think that the reason is that the command-line interface is basically a clone of the Unix/Linux 'tar' command. Of course, it produces zip format, rather than tar.

    Edit #2 - It occurred to me that to add a file to a zip, you'd just have to strip off the central directory from the end of the file, append the new entry, and then re-write the central directory. An update would be the same thing, except instead of adding the entry to the directory, you'd update the existing directory entry to point to the new zip entry.

    While exploring this possibility, I found that Java 7 includes a zip file system provider which looks like it does this, and could form the basis of a solution for what you want to do.

    0 讨论(0)
  • 2020-12-13 02:50

    It works , but its not the extact solution you are looking at.

    Instead of manually creating folder structure , i am asking to mention the whole path ,so mkdir will create folder and sub-directories too.hope this helps....

    set /p Folder=where you want to move inside jar 
    set /p File=Mention path of file to be moved 
    set /p Jarfile=Where is your jar file
    mkdir %Folder%
    copy %File% %Folder%
    jar -uvf %Jarfile% %File%
    rd /s /q %Folder%
    rmdir %Folder%
    
    0 讨论(0)
  • 2020-12-13 02:54

    As the accepted answer already pointed out, you can't use neither the zip nor the jar command to insert a specific file on the file system to a specific directory inside the compressed file.

    But there is this workaround, which in a nutshell extracts a specific file inside the compressed file first, you can update it and then put it back into the compressed file:

    1. Extracting a single file :
    Consider a jar with the following contents

    $ jar tvf foo.jar 
     0 Thu Jan 10 00:05:06 IST 2013 META-INF/
    68 Thu Jan 10 00:05:06 IST 2013 META-INF/MANIFEST.MF
     0 Thu Jan 10 00:04:30 IST 2013 x/
     0 Thu Jan 10 00:07:36 IST 2013 x/b/
     9 Thu Jan 10 00:07:36 IST 2013 x/b/hello.txt
     0 Thu Jan 10 00:04:30 IST 2013 x/a/
    

    To extract only hello.txt, you have to give fully qualified path of the file-to-extract. Jar creates appropriate folders too.

    Example :

    $ jar xvf foo.jar x/b/hello.txt
    inflated: x/b/hello.txt
    
    $ tree x/
    x
    └── b
        └── hello.txt  
    1 directory, 1 file
    

    2. Updating a Single File

    1. Modify the extracted file
    2. update the jar using the 'u' flag. Provide the exploded directory root and jar shall update all the path elements under it.

    Example:

    $ jar vfu foo.jar x/
    adding: x/(in = 0) (out= 0)(stored 0%)
    adding: x/b/(in = 0) (out= 0)(stored 0%)
    adding: x/b/hello.txt(in = 23) (out= 11)(deflated 52%)
    

    3. Which version of jar I tried this out with ?

    I used jar bundled with JDK 7 on Fedora 17.


    I have verified all the steps mentioned here. Hope this helps.

    0 讨论(0)
  • 2020-12-13 02:56

    I too was trying to update a jar without reproducing the jar's file structure external to the jar, and for some reason the jar uf jarFileName -C ... command never let me copy the file to a child directory in the jar. I then tried cutting and pasting the file into the jar using ubuntu 14.04's built in archive manager (i.e. right click on jar file and chose "open with archive manager" then paste the file anywhere in the jar's file structure), and it worked. It's not a cli solution but it works. Hope this saves someone else some time.

    0 讨论(0)
  • 2020-12-13 02:57

    4129445 : An API to incrementally update ZIP files in the Sun/Oracle bug database asks for this feature to be implemented in the java api.

    From the Evaluation:

    "It is easy to sympathize with those who want this bug fixed. It is perhaps easier to sympathize with those who have, inadvertently, overwritten JAR files that are already in-use by a running JVM."

    and

    "It would have been nice if the jar & zip APIs had allowed for mutable zip files from day one. But at this point adding mutability to jar & zip files from Java(or rather, increasing the ease of mutating them) seems likely to introduce more hard-to-debug problems."

    0 讨论(0)
  • 2020-12-13 03:08

    I think this is due to how the classes in the java.util.zip package work. They sit on top of streams.

    The ZipFile class is built on top of a ZipFileInputStream. Writing zipfiles is done by feeding the ZipEntries one by one to a ZipOutputStream.

    I assume the jar executable is built on top of these classes.

    0 讨论(0)
提交回复
热议问题