jar包解压

/**
     * 解压jar文件
     * 
     * @param jarFile
     *            要解压的jar文件路径
     * @param destination
     *            解压到哪里
     * @throws IOException
     */
    public static void unJar(File jarFile, File destination) {
        JarFile jar = null;
        try {
            if (destination.exists() == false) {
                destination.mkdirs();
            }
            jar = new JarFile(jarFile);
            Enumeration<JarEntry> en = jar.entries();
            JarEntry entry = null;
            InputStream input = null;
            BufferedOutputStream bos = null;
            File file = null;
            while (en.hasMoreElements()) {
                entry = en.nextElement();
                input = jar.getInputStream(entry);
                file = new File(destination, entry.getName());
                if (entry.isDirectory()) {
                    file.mkdirs();
                    continue;
                } else {
                    file.getParentFile().mkdirs();
                }
                bos = new BufferedOutputStream(new FileOutputStream(file));
                byte[] buffer = new byte[8192];
                int length = -1;
                while (true) {
                    length = input.read(buffer);
                    if (length == -1)
                        break;
                    bos.write(buffer, 0, length);
                }
                bos.close();
                input.close();
                // IOUtils.copy(input, bos);
            }

            Manifest mf = jar.getManifest();
            if (mf != null) {
                File f = new File(destination, "META-INF/MANIFEST.MF");
                File parent = f.getParentFile();
                if (parent.exists() == false) {
                    parent.mkdirs();
                }
                OutputStream out = new FileOutputStream(f);
                mf.write(out);
                out.flush();
                out.close();
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (jar != null) {
                try {
                    jar.close();
                } catch (Exception e) {
                }
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值