溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何解壓jar

發(fā)布時(shí)間:2021-10-20 10:11:11 來(lái)源:億速云 閱讀:138 作者:柒染 欄目:大數(shù)據(jù)

這篇文章給大家介紹如何解壓jar,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

場(chǎng)景

  • 頁(yè)面上傳jar包

  • 后臺(tái)解壓jar包

  • 頁(yè)面展示所有package

  • 選擇一個(gè)package

  • 頁(yè)面顯示class和子package

    • 選擇class,進(jìn)入class解析頁(yè)面

    • 選擇package,顯示class和子package

解壓jar

package com.wuxiongwei.java.jar2;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
 * 非常好的工具類(lèi) <br>
 * 解壓jar.
 * @author
 * @version 1.0.0
 */
public class JarDecompression {

    protected static Log log = LogFactory.getLog(JarDecompression.class);

    @SuppressWarnings("resource")
    public static void uncompress(File jarFile, File tarDir) throws IOException {
        JarFile jfInst = new JarFile(jarFile);
        Enumeration enumEntry = jfInst.entries();
        while (enumEntry.hasMoreElements()) {
            JarEntry jarEntry = (JarEntry) enumEntry.nextElement();
            File tarFile = new File(tarDir, jarEntry.getName());
            if(jarEntry.getName().contains("META-INF")){
                File miFile = new File(tarDir, "META-INF");
                if(!miFile.exists()){
                    miFile.mkdirs();
                }

            }
            makeFile(jarEntry, tarFile);
            if (jarEntry.isDirectory()) {
                continue;
            }
            FileChannel fileChannel = new FileOutputStream(tarFile).getChannel();
            InputStream ins = jfInst.getInputStream(jarEntry);
            transferStream(ins, fileChannel);
        }
    }

    /**
     * 流交換操作
     * @param ins 輸入流
     * @param channel 輸出流
     */
    private static void transferStream(InputStream ins, FileChannel channel) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 10);
        ReadableByteChannel rbcInst = Channels.newChannel(ins);
        try {
            while (-1 != (rbcInst.read(byteBuffer))) {
                byteBuffer.flip();
                channel.write(byteBuffer);
                byteBuffer.clear();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            if (null != rbcInst) {
                try {
                    rbcInst.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != channel) {
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 打印jar文件內(nèi)容信息
     * @param file jar文件
     */
    public static void printJarEntry(File file) {
        JarFile jfInst = null;;
        try {
            jfInst = new JarFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Enumeration enumEntry = jfInst.entries();
        while (enumEntry.hasMoreElements()) {
            log.info((enumEntry.nextElement()));
        }
    }

    /**
     * 創(chuàng)建文件
     * @param jarEntry jar實(shí)體
     * @param fileInst 文件實(shí)體
     * @throws IOException 拋出異常
     */
    public static void makeFile(JarEntry jarEntry, File fileInst) {
        if (!fileInst.exists()) {
            if (jarEntry.isDirectory()) {
                fileInst.mkdirs();
            } else {
                try {
                    fileInst.createNewFile();
                } catch (IOException e) {
                    log.error("創(chuàng)建文件失敗>>>".concat(fileInst.getPath()));
                }
            }
        }
    }

    public static void main(String[] args) {
        File jarFile = new File("/Users/mac/Documents/other/bw2/bopsdk-openapi-1.0.2-Release.jar");
        File targetDir = new File("/Users/mac/Documents/other/bw2/test/");
        try {
            JarDecompression.uncompress(jarFile, targetDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

關(guān)于如何解壓jar就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

jar
AI