解压jar工具类

Posted 阿拉的梦想

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解压jar工具类相关的知识,希望对你有一定的参考价值。

解压jar工具类

package com.demo.devops.commons.utils;

import cn.hutool.core.io.FileUtil;
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.nio.file.Paths;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
 * 解压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文件内容信息
     * @param file jar文件
     */
    private 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()));
        
    

    /**
     * 创建文件
     * @param jarEntry jar实体
     * @param fileInst 文件实体
     * @throws IOException 抛出异常
     */
    private static void makeFile(JarEntry jarEntry, File fileInst) 
        if (!fileInst.exists()) 
            if (jarEntry.isDirectory()) 
                fileInst.mkdirs();
             else 
                try 
                    fileInst.createNewFile();
                 catch (IOException e) 
                    log.error("创建文件失败>>>".concat(fileInst.getPath()));
                
            
        
    

    public static void main(String[] args) 
        File jarFile = new File("D:\\\\ideaProject\\\\autotest\\\\backend\\\\target\\\\backend-1.18.jar");
        File targetDir = new File("D:\\\\autotest");
        try 
            JarDecompression.uncompress(jarFile, targetDir);
            //使用hutool复制文件夹
            FileUtil.copy(Paths.get(targetDir.getAbsolutePath()+"/BOOT-INF/classes/jmeter"),Paths.get(targetDir.getParentFile().getAbsolutePath()));
         catch (IOException e) 
            e.printStackTrace();
        
    

以上是关于解压jar工具类的主要内容,如果未能解决你的问题,请参考以下文章

压缩工具类 - ZipUtils.java

换个新的思路 代替解压jar包 例证:wechat4j 框架中的templateMsg类

Linux下如何在不解压jar包查看或修改配置文

jar解压后 如何把解压出来的文件夹 重新编译成jar ???

jar解压后 如何把解压出来的文件夹 重新编译成jar ???

JAVA 将中文转化为拼音工具类