java - 从代码更新 .JAR 的内容

标签 java jar

我正在制作一款需要更新的游戏。

我有两个 JAR 文件:Update.JarGame.Jar 基本上,我希望能够在不完全覆盖它的情况下修改 Game.Jar。

我想:

  1. 从代码中将 Jar 文件作为 Zip 文件打开
  2. 替换/添加一些资源
  3. 重新打包 Jar 文件。

有没有简单的方法或类可以做到这一点?如果不是,最干净的方法是什么?

最佳答案

Java JAR 文件是普通的 ZIP 文件。因此,您可以使用处理 ZIP 的代码打开和修改它。

这是一个有效的片段(由 David 提供):

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;


public class JarUpdater {
    public static void main(String[] args) {

        File[] contents = {new File("F:\\ResourceTest.txt"),
                           new File("F:\\ResourceTest2.bmp")};

        File jarFile = new File("F:\\RepackMe.jar");

        try {
            updateZipFile(jarFile, contents);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void updateZipFile(File zipFile,
             File[] files) throws IOException {
               // get a temp file
        File tempFile = File.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();
        // Compress the files
        for (int i = 0; i < files.length; i++) {
            InputStream in = new FileInputStream(files[i]);
            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(files[i].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();
    }
}

关于java - 从代码更新 .JAR 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7548900/

相关文章:

java - 使用二元运算符进行表达式解析

java - 没有方法在 RemoteViewsService 中触发 - Android

java - 访问包含带有 jar 的文件夹的存储库

java - 在 Netbeans 中有效,但在 "outside"中无效

android - 添加 volley.jar 库后没有主 list 属性错误

java - "switch"相当于异常处理

java - 用于 Spring 安全的 SpEL : Passing Values from XML to Java based SpEL configuration

java - 单击时不打开日期选择器对话框

java - 导出的Jar文件抛出异常(JDBC)

java - 如何在 Linux 终端中编译带有外部 jar 文件的 java 项目