如何检查jar文件是否有效? [英] how to check if a jar file is valid?

查看:196
本文介绍了如何检查jar文件是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的webapp允许用户上传jar文件。但是,上传jar文件后,它已损坏。我已经通过比较md5校验和(winmd5free)来验证这一点。

my webapp allows a user to upload a jar file. however, after the jar file is uploaded, it is corrupted. i have verified this by comparing the md5 checksum (winmd5free).

上传的jar文件看起来正常和正确

the uploaded jar file looks "normal" and "right"


  • 文件大小与原始文件相比(在KB级别)

  • 我可以使用7z打开上传的jar文件并查看其内容(资源和类文件),一切都是一样的与原来的

相比,当我打开上传的jar文件(使用Notepad ++)时,我注意到二进制内容与原本的。另外,当我使用JarInputStream读取jar条目时,没有条目。

when i open up the uploaded jar file (using Notepad++), i did notice that the binary contents are different from the original. also, when i used JarInputStream to read the jar entries, there were no entries.

JarInputStream is = new JarInputStream(new FileInputStream(new File("uploaded.jar")));
JarEntry entry = null;
while(null != (entry = is.getNextJarEntry())) {
 System.out.println(entry.getName());
}

此外,当我双击jar(Windows)时,我得到了以下消息。

furthermore, when i double click on the jar (Windows), i get the following message.


错误:jarfile无效或损坏

Error: Invalid or corrupt jarfile

我的问题是


  • 有没有办法以编程方式检查jar文件是否有效?我本来希望JarInputStream立即检测到它,但它显示没有任何问题

  • 当我双击jar文件时,在windows中,是java.exe给了我无效或损坏的jarfile消息?

  • 当一个无效的jar文件作为类路径的一部分传入时,怎么没有抛出错误/异常?例如 java -cp uploaded.jar; libs * com.some.class.Test

  • is there a way to programmatically check if a jar file is valid? i would have expected JarInputStream to detect this right away, but it shows no problems at all
  • when i double click on the jar file, in windows, is it java.exe that is giving me the invalid or corrupt jarfile message?
  • how come when an invalid jar file is passed in as part of the classpath, no error/exception is thrown? e.g. java -cp uploaded.jar;libs* com.some.class.Test ?

请注意这与jar签名和/或检查jar的签名无关。它只是检查一个文件(上传与否)是否是一个有效的jar文件(不一定是jar的类文件是有效的,因为已经有关于这个问题的另一个SO帖子)。

please note that this has nothing to do with jar signing and/or checking the signing of a jar. it is simply checking if a file (uploaded or not) is a valid jar file (not necessarily if the jar's class files are valid, as there is another SO post on this issue already).

运行结果


jar -tvf uploaded.jar

jar -tvf uploaded.jar



java.util.zip.ZipException: error in opening zip file
        at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(ZipFile.java:127)
        at java.util.zip.ZipFile.<init>(ZipFile.java:88)
        at sun.tools.jar.Main.list(Main.java:977)
        at sun.tools.jar.Main.run(Main.java:222)
        at sun.tools.jar.Main.main(Main.java:1147)


推荐答案

a以编程方式检测无效jar文件的方法是使用java.util.ZipFile。

a way to programmatically detect an invalid jar file is to use java.util.ZipFile.

public static void main(String[] args) {
    if(args.length < 1) {
        System.err.println("need jar file");
        return;
    }

    String pathname = args[0];
    try {
        ZipFile file = new ZipFile(new File(pathname));
        Enumeration<? extends ZipEntry> e = file.entries();
        while(e.hasMoreElements()) {
            ZipEntry entry = e.nextElement();
            System.out.println(entry.getName());
        }
    } catch(Exception ex) {
        ex.printStackTrace();
    }
}

如果jar文件无效,ZipException将是在实例化ZipFile时抛出。

if the jar file is invalid, a ZipException will be thrown when instantiating the ZipFile.

这篇关于如何检查jar文件是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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