如何从JNLP文件下载库

时间:2016-04-30 18:18:51

标签: java java-web-start gzipinputstream

我正在尝试从Java程序加载特定的JNLP文件并获取其.jar文件。我已经下载了.pack.gz文件(因为它使用了压缩),但现在我无法解压缩它们。我正在尝试使用类java.util.jar.Pack200.Unpacker,但它会抛出一个包含以下信息的异常:

SEVERE: null
java.io.IOException: Corrupted pack file: magic/ver = 1F8B0800/0.0 should be CAFED00D/150.7 OR CAFED00D/160.1 OR CAFED00D/170.1 OR CAFED00D/171.0

    at com.sun.java.util.jar.pack.NativeUnpack.start(Native Method)
    at com.sun.java.util.jar.pack.NativeUnpack.run(NativeUnpack.java:198)
    at com.sun.java.util.jar.pack.NativeUnpack.run(NativeUnpack.java:247)
    at com.sun.java.util.jar.pack.UnpackerImpl.unpack(UnpackerImpl.java:138)
    at com.sun.java.util.jar.pack.UnpackerImpl.unpack(UnpackerImpl.java:174)
    at rmiattack.Util.loadJNLP(Util.java:186)
    at rmiattack.RmiAttack.debugFunction(RmiAttack.java:50)
    at rmiattack.RmiAttack.main(RmiAttack.java:74)

我已经找了那个错误,然后我尝试使用命令“unpack200”解压缩文件并且它有效。然后我试图在openjdk项目中找到unpack200源代码,但我没有找到它们。那么,有谁能告诉我在哪里可以找到这些来源知道如何使用Unpacker类? 我附上了我正在使用的代码:

public static void loadJNLP(String file, String outDir) throws IOException, ParserConfigurationException, SAXException {
        byte[] encoded = Files.readAllBytes(Paths.get(file));
        Document document = stringToXML(new String(encoded, Charset.defaultCharset()), false);
        System.out.println(document.getElementsByTagName("title").item(0));
        String baseURL = document.getElementsByTagName("jnlp").item(0).getAttributes().getNamedItem("codebase").getTextContent();
        int i;
        NodeList nodeList = document.getElementsByTagName("jar");
        Unpacker unpacker = Pack200.newUnpacker();
        SortedMap<String,String> properties = unpacker.properties();
        properties.entrySet().stream().forEach((entry) -> {
            System.out.println(entry.getKey() + " - " + entry.getValue());
        });
        for (i = 0; i < nodeList.getLength(); i++) {
            //This can be threaded
            NamedNodeMap attributes = nodeList.item(i).getAttributes();
            String fileName = attributes.getNamedItem("href").getTextContent().replace(".jar", "") + "__V" + attributes.getNamedItem("version").getTextContent() + ".jar.pack.gz";
            File packedJar = new File(outDir + '/' + fileName); //File path to download packed jar
            FileUtils.copyURLToFile(new URL(baseURL + '/' + fileName), packedJar); //Download packed jar
            FileOutputStream fileOutputStream = new FileOutputStream(outDir+'/'+attributes.getNamedItem("href").getTextContent()); //Save jar without version
            try (JarOutputStream jarOutputStream = new JarOutputStream(fileOutputStream)) {
                unpacker.unpack(packedJar, jarOutputStream);
            } finally {
                fileOutputStream.close();
            }
            packedJar.delete();
        }
        System.out.println("Save to " + outDir);
    }

异常之前的输出是:

[title: null]
com.sun.java.util.jar.pack.class.format.error - pass
com.sun.java.util.jar.pack.default.timezone - false
com.sun.java.util.jar.pack.disable.native - false
com.sun.java.util.jar.pack.verbose - 0
pack.class.attribute.CompilationID - RUH
pack.class.attribute.SourceID - RUH
pack.code.attribute.CharacterRangeTable - NH[PHPOHIIH]
pack.code.attribute.CoverageTable - NH[PHHII]
pack.deflate.hint - keep
pack.effort - 5
pack.keep.file.order - true
pack.modification.time - keep
pack.segment.limit - -1
pack.unknown.attribute - pass

提前致谢。

编辑: 感谢@Thorbjoern的评论我已经解决了。我只需要解压缩文件然后解压缩它们。文件代码如下所示:

public static void loadJNLP(String file, String outDir) throws IOException, ParserConfigurationException, SAXException {
        byte[] encoded = Files.readAllBytes(Paths.get(file));
        Document document = stringToXML(new String(encoded, Charset.defaultCharset()), false);
        System.out.println(document.getElementsByTagName("title").item(0));
        String baseURL = document.getElementsByTagName("jnlp").item(0).getAttributes().getNamedItem("codebase").getTextContent();
        int i;
        int length;
        byte[] buffer = new byte[1024];
        NodeList nodeList = document.getElementsByTagName("jar");
        Unpacker unpacker = Pack200.newUnpacker();
        for (i = 0; i < nodeList.getLength(); i++) {
            //This can be threaded
            NamedNodeMap attributes = nodeList.item(i).getAttributes();
            String fileName = attributes.getNamedItem("href").getTextContent().replace(".jar", "") + "__V" + attributes.getNamedItem("version").getTextContent() + ".jar";

            File compressedPackedIn = new File(outDir + '/' + fileName + ".pack.gz"); //File path to download compressed packed jar
            FileUtils.copyURLToFile(new URL(baseURL + '/' + fileName + ".pack.gz"), compressedPackedIn); //Download packed jar
            FileOutputStream uncompressedPackedOut;
            try (GZIPInputStream compressed = new GZIPInputStream(new FileInputStream(outDir + '/' + fileName + ".pack.gz"))) {
                uncompressedPackedOut = new FileOutputStream(outDir + '/' + fileName + ".pack");
                //http://www.mkyong.com/java/how-to-decompress-file-from-gzip-file/
                while ((length = compressed.read(buffer))> 0) {
                    uncompressedPackedOut.write(buffer, 0, length);
                }
            }
            uncompressedPackedOut.close();
            File uncompressedPackedIn = new File(outDir + '/' + fileName + ".pack");

            FileOutputStream uncompressedUnpacked = new FileOutputStream(outDir + '/' + fileName);
            try (JarOutputStream jarOutputStream = new JarOutputStream(uncompressedUnpacked)) {
                unpacker.unpack(uncompressedPackedIn, jarOutputStream);
            } finally {
                uncompressedUnpacked.close();
            }
            compressedPackedIn.delete();
            uncompressedPackedIn.delete();

        }
    }

0 个答案:

没有答案