简体   繁体   English

org内的Java-copy file.txt,这是A.jar中的一个文件夹,而没有提取z.jar文件

[英]Java-Copy file.txt inside org which is a folder in A.jar without extracting z.jar file

I was trying to copy a file into a A.jar (without extracting it) but it didn't work. 我试图将文件复制到A.jar(不提取文件)中,但是没有用。 Suppose I have file "copy.txt" in "D:\\java\\copy.txt" and i want this file to be copied into my "A.jar/org/here" . 假设我在“ D:\\ java \\ copy.txt”中有文件“ copy.txt”,并且我希望将此文件复制到我的“ A.jar / org / here”中。 if the file is already exist then it should replace it. 如果文件已经存在,则应将其替换。

i tried modifying the below code but it didn't work. 我试图修改下面的代码,但是没有用。

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class deploy {
public static void main(String[] argv) {
    Path myFilePath = Paths.get("C:/Users/ma329300/Desktop/copy.txt");

    Path zipFilePath = Paths.get("D:/java/A.jar");
    try( FileSystem fs = FileSystems.newFileSystem(zipFilePath, null) ){
        Path fileInsideZipPath = fs.getPath("/org/copy.txt");
        Files.copy(myFilePath, fileInsideZipPath);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

First of all, check your paths. 首先,检查您的路径。 Java is really tricky with the paths and should be in full path form. Java对于路径确实很棘手,应该采用完整路径形式。 Try putting the backslashes like "D:\\java\\A.jar", as windows works differently than linux. 由于Windows与Linux的工作原理不同,请尝试使用反斜杠,例如“ D:\\ java \\ A.jar”。

Also, if you change your file and re-compile, it will generate a new .jar each time, with the updated file you wanted to change. 另外,如果您更改文件并重新编译,它将每次使用您要更改的更新文件生成一个新的.jar。 That would solve your problem without having to access your .jar externally. 这样可以解决您的问题,而无需从外部访问.jar。

I tried once to access libraries packed on a .jar and load them with the code inside the .jar too and didn't worked propperly, so be carefull. 我曾经尝试访问打包在.jar上的库,并且也将.jar内的代码加载到库中,但无法正常工作,因此请小心。

Another thing you should be aware of is that you cannot modify a .jar directly without decompressing, as it uses a special algorythm in order to get the indexation properly done for and by java. 您还应该注意的另一件事是,您不能直接修改.jar而不进行解压缩,因为它使用特殊的算法来使java和Java正确完成索引。 Changing one specific part of the .jar could corrupt the data into it and make it crash on the run. 更改.jar的一个特定部分可能会将数据损坏到其中,并使其在运行时崩溃。

Hope it helped. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM