how to check if a jar file is valid?

前端 未结 1 1546
北恋
北恋 2020-12-30 06:03

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).

相关标签:
1条回答
  • 2020-12-30 06:28

    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();
        }
    }
    

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

    0 讨论(0)
提交回复
热议问题