Java 如何从JNLP文件下载库

Java 如何从JNLP文件下载库,java,java-web-start,gzipinputstream,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/

我试图从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);
    }
提前谢谢

编辑: 感谢@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();

        }
    }
publicstaticvoidloadjnlp(stringfile,stringoutdir)抛出IOException、ParserConfigurationException、SAXException{
byte[]encoded=Files.readAllBytes(path.get(file));
Document Document=stringToXML(新字符串(编码,Charset.defaultCharset()),false);
System.out.println(document.getElementsByTagName(“title”).item(0));
String baseURL=document.getElementsByTagName(“jnlp”).item(0.getAttributes().getNamedItem(“codebase”).getTextContent();
int i;
整数长度;
字节[]缓冲区=新字节[1024];
NodeList NodeList=document.getElementsByTagName(“jar”);
Unpacker Unpacker=Pack200.newUnpacker();
对于(i=0;i0){
UncompressedPackeOut.write(缓冲区,0,长度);
}
}
解压缩packedout.close();
File uncompressedPackedIn=新文件(outDir+'/'+fileName+.pack”);
FileOutputStream uncompressedUnpacked=新的FileOutputStream(outDir+'/'+文件名);
try(JarOutputStream JarOutputStream=新的JarOutputStream(未压缩打包)){
解包(uncompressedPackedIn,jarOutputStream);
}最后{
uncompressedUnpacked.close();
}
compressedPackedIn.delete();
解压缩packedin.delete();
}
}

你解压缩了pack.gz文件吗?谢谢,这就是我需要的。你解压缩了pack.gz文件吗?谢谢,这就是我需要的
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();

        }
    }