从代码更新 .JAR 的内容 [英] Updating .JAR's contents from code

查看:12
本文介绍了从代码更新 .JAR 的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个需要更新的游戏.

I am making a game that needs to be updated.

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

I have two JAR files: Update.Jar and Game.Jar Basically, I want to be able to modify Game.Jar without completely overwriting it.

我想:

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

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

Is there an easy way or classes that can do this? If not, what would be the cleanest approach to doing this?

推荐答案

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

A Java JAR file is a normal ZIP file. You can therefore open and modify it with code dealing with ZIPs.

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

Here's a snippet which works (courtesy of 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();
    }
}

这篇关于从代码更新 .JAR 的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆