Java库来操作jar文件 [英] Java library to manipulate jar files

查看:60
本文介绍了Java库来操作jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以推荐使用Java库来操作 jar 文件吗?我发现 java.util.jar 提供的设施相当简洁(或者我可能没有一些示例代码可供查看)。我对以下内容感兴趣:

Can anyone recommend a Java library to manipulate jar files? I am finding the facilities available at java.util.jar rather terse (or maybe I don't have some example code to look at). I am interested in things like:


  1. 将文件添加到 jar

  2. jar 删除文件

  3. 从文件夹创建 jar 文件

  4. jar 文件缩小到文件夹

  5. 读取 jar条目的内容(可能在内存中,无需放气< <> li>
  6. 在内存中创建 jar条目,而无需从磁盘上的文件中读取。

  7. 询问 jar 文件条目是否为 Manifest 文件或其他特殊文件。

  1. adding files to a jar
  2. removing files from a jar
  3. create a jar file from a folder
  4. deflate a jar file to a folder
  5. reading the contents of a jar entry (perhaps in memory, without having to deflate the jar on disk)
  6. creating a jar entry in memory without having to read from a file on disk.
  7. asking whether a jar file entry is a Manifest file or some other special file or not.

如果有人可以使用 java.util.jar 或其他一些有用的库指向示例代码。特别是在查看 java.util.jar 的类和方法时,我不清楚如何读取特定Jar条目的内容,甚至不知道如何缩小 jar (不产生外部过程)。

If anyone can point to example code using either the java.util.jar or some other library that would help. In particular looking at the classes and methods of java.util.jar it is not clear to me how to read the contents of a particular Jar entry or even how to deflate the jar (without spawning an external process).

推荐答案

1& 2是简单的手动操作,将条目从Jar复制到另一个添加或留下您想要的条目,删除原始文件并重命名新文件以替换它。

1 & 2 are simple manual operations of copying the entries from Jar to another adding or leaving the entries you want, deleting he original file and renaming the new to replace it.

as jtahlbom已经指出,Java Jar API可以开箱即用。

As jtahlbom has pointed out, the Java Jar API handles the rest out of the box.

UPDATE with example

这些都是非常基本的例子。它们向您展示如何读取和编写Jar。基本上你可以从中得到你需要做的其他事情。

These are REALLY basic examples. They show you how to read and write a Jar. Basically from that you can derive just about everything else you need to do.

我已经设置了一个个人图书馆(不包括在内),它基本上允许我传入一个 InputStream 并将其写入 OutputStream ,反之亦然。这意味着你可以从任何地方读取并写入任何地方,这可以满足你们大部分的要求。

I've setup a personal library (not included) which basically allows me to pass in an InputStream and have it written to an OutputStream and visa versa. This means you could read from any where and write to anywhere, which could cover most of you requirements.

public void unjar(File jar, File outputPath) throws IOException {
    JarFile jarFile = null;
    try {
        if (outputPath.exits() || outputPathFile.mkdirs()) {
            jarFile = new JarFile(jar);
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                File path = new File(outputPath + File.separator + entry.getName());
                if (entry.isDirectory()) {
                    if (!path.exists() && !path.mkdirs()) {
                        throw new IOException("Failed to create output path " + path);
                    }
                } else {
                    System.out.println("Extracting " + path);

                    InputStream is = null;
                    OutputStream os = null;
                    try {
                        is = jarFile.getInputStream(entry);
                        os = new FileOutputStream(path);

                        byte[] byteBuffer = new byte[1024];
                        int bytesRead = -1;
                        while ((bytesRead = is.read(byteBuffer)) != -1) {
                            os.write(byteBuffer, 0, bytesRead);
                        }
                        os.flush();
                    } finally {
                        try {
                            os.close();
                        } catch (Exception e) {
                        }
                        try {
                            is.close();
                        } catch (Exception e) {
                        }
                    }
                }
            }
        } else {
            throw IOException("Output path does not exist/could not be created");
        }
    } finally {
        try {
            jarFile.close();
        } catch (Exception e) {
        }
    }
}

public void jar(File jar, File sourcePath) throws IOException {
    JarOutputStream jos = null;
    try {
        jos = new JarOutputStream(new FileOutputStream(jar));

        List<File> fileList = getFiles(sourcePath);
        System.out.println("Jaring " + fileList.size() + " files");

        List<String> lstPaths = new ArrayList<String>(25);
        for (File file : fileList) {
            String path = file.getParent().replace("\\", "/");
            String name = file.getName();

            path = path.substring(sourcePath.getPath().length());
            if (path.startsWith("/")) {
                path = path.substring(1);
            }

            if (path.length() > 0) {
                path += "/";
                if (!lstPaths.contains(path)) {
                    JarEntry entry = new JarEntry(path);
                    jos.putNextEntry(entry);
                    jos.closeEntry();
                    lstPaths.add(path);
                }
            }

            System.out.println("Adding " + path + name);

            JarEntry entry = new JarEntry(path + name);
            jos.putNextEntry(entry);

            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                byte[] byteBuffer = new byte[1024];
                int bytesRead = -1;
                while ((bytesRead = fis.read(byteBuffer)) != -1) {
                    jos.write(byteBuffer, 0, bytesRead);
                }
                jos.flush();
            } finally {
                try {
                    fis.close();
                } catch (Exception e) {
                }
            }
            jos.closeEntry();
        }
        jos.flush();
    } finally {
        try {
            jos.close();
        } catch (Exception e) {
        }
    }
}

如果你花点时间看一下,有很多关于如何在SO上添加/删除来自zip / jar文件的条目的例子

If you take the time look, there are a number of examples of how to add/remove entries from the from zip/jar files on SO

这篇关于Java库来操作jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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