工具类-解压jar(v2)

工具类-解压jar(v2)

Scroll Down

使用方式

try {
                LOGGER.info("uncompress1");
                JarUtil.uncompress1(file, folder);
            }catch (Exception e){
                LOGGER.info("uncompress2");
                deleteDir(folder);
                JarUtil.uncompress2(file, folder);
            }

解压jar

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
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;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


public class JarUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(JarUtil.class);

    public static void uncompress1(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);
        }
    }

    public static void uncompress2(File zipFilePath, File destDir) {
        // create output directory if it doesn't exist
        if(!destDir.exists()) destDir.mkdirs();
        FileInputStream fis;
        //buffer for read and write data to file
        byte[] buffer = new byte[1024];
        try {
            fis = new FileInputStream(zipFilePath);
            ZipInputStream zis = new ZipInputStream(fis);
            ZipEntry ze = zis.getNextEntry();
            while(ze != null){
                String fileName = ze.getName();
                File newFile = new File(destDir + File.separator + fileName);
                //create directories for sub directories in zip
                new File(newFile.getParent()).mkdirs();
                FileOutputStream fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                //close this ZipEntry
                zis.closeEntry();
                ze = zis.getNextEntry();
            }
            //close last ZipEntry
            zis.closeEntry();
            zis.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 流交换操作
     * @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();
                }
            }
        }
    }

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