Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更新jar文件的内容_Java_File_Jar - Fatal编程技术网

Java 更新jar文件的内容

Java 更新jar文件的内容,java,file,jar,Java,File,Jar,我正在尝试将文本文件添加到现有的jar中 这就是它的工作原理: JarFile A:运行此操作时,它将在JarFile B中添加一个文本文件 JarFile B:运行时,它将从JarFile A添加的textfiel中读取一些配置选项 以下是JarFileA为添加文件而执行的代码: public static void updateZipFile(File zipFile, String targetPackage, File[] files) throws IOException {

我正在尝试将文本文件添加到现有的jar中

这就是它的工作原理:

  • JarFile A:运行此操作时,它将在JarFile B中添加一个文本文件

  • JarFile B:运行时,它将从JarFile A添加的textfiel中读取一些配置选项

以下是JarFileA为添加文件而执行的代码:

public static void updateZipFile(File zipFile, String targetPackage, File[] files) throws IOException {
    // get a temp file
    File tempFile = createTempFile(zipFile.getName(), null);
    // delete it, otherwise you cannot rename your existing zip to it.
    tempFile.delete();

    boolean renameOk = zipFile.renameTo(tempFile);
    if (!renameOk) {
        throw new RuntimeException("Could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
    }
    byte[] buf = new byte[1024];

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (File f : files) {
            if (f.getName().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {
            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(name));
            // Transfer bytes from the ZIP file to the output file
            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }
    // Close the streams        
    zin.close();
    for (File file : files) {
        InputStream in = new FileInputStream(file);
        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(targetPackage + File.separator + file.getName()));
        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        // Complete the entry
        out.closeEntry();
        in.close();
    }
    // Complete the ZIP file
    out.close();
    tempFile.delete();
}
这一切在MacOSX上都可以正常工作,但在Windows上却不行。它会复制文件,但JarFileB无法读取新创建的文件。也许罐子坏了

非常感谢您的帮助


谢谢。

您是否在windows中手动打开jar b以确保添加了文件?是的,也是手动打开的。但是没有成功。