java 启动解压 jar包

1 .添加工具类

package com.michaels.emailmanagement.email.job;


import com.michaels.emailmanagement.shared.common.BusinessCode;
import com.michaels.emailmanagement.shared.exception.MikException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.stream.Collectors;

@Slf4j
public class DataFlowUtils {

    private static final String BOOT_INF = "BOOT-INF";

    private static final String CLASSES = "classes";

    private static final String LIB = "lib";

    public static void extractJar(String jarFile, String destDir) {
        java.util.jar.JarFile jar = null;
        try {
            log.info("processing jarFile:{}", jarFile);
            jar = new java.util.jar.JarFile(jarFile);
            java.util.Enumeration<JarEntry> enumEntries = jar.entries();
            while (enumEntries.hasMoreElements()) {
                JarEntry file = enumEntries.nextElement();
                File f = new File(destDir + java.io.File.separator + file.getName());
                if (file.isDirectory()) {
                    // if its a directory, create it
                    if (!f.mkdir()) {
                        throw new IOException("mkdirs error, path [" + f + "]");
                    }
                    continue;
                }

                java.io.InputStream is = null;
                // get the input stream
                java.io.FileOutputStream fos = null;
                try {
                    is = jar.getInputStream(file);
                    fos = new java.io.FileOutputStream(f);
                    byte[] buffer = new byte[8192];
                    int read;
                    while ((read = is.read(buffer)) != -1) {
                        // write contents of 'is' to 'fos'
                        fos.write(buffer, 0, read);
                    }
                } finally {
                    if (fos != null)
                        safeClose(fos);
                    if (is != null)
                        safeClose(is);
                }

            }
        } catch (IOException e) {
            log.error("unzip file error", e);
            throw new MikException(HttpStatus.BAD_REQUEST, BusinessCode.MSG_00002, jarFile);
        } finally {
            if (jar != null){
                safeClose(jar);
            }
        }

    }


    public static List<String> getFileToStagedFiles(String basePath) {
        String path = basePath + File.separator + BOOT_INF;
        log.info("class path {}", path);
        File file = new File(path + File.separator + LIB);
        File[] files = file.listFiles();
        log.info("need upload {} lib files to staging locations", files.length);
        List<File> ls = new ArrayList<>(Arrays.asList(files));
        File classesFolder = new File(path + File.separator + CLASSES);
        log.info("need upload {} folder to staging locations", classesFolder);
        ls.add(classesFolder);

        return ls.stream().map(File::toString).collect(Collectors.toList());
    }


    public static void safeClose(Closeable fis) {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                log.error("error release resource", e);
            }
        }
    }
}

  1. 添加事件
package com.michaels.emailmanagement.email.job;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;

@Slf4j
public class DecompressJarEvent extends ApplicationEvent {
    /**
     * Create a new {@code ApplicationEvent}.
     *
     * @param source the object on which the event initially occurred or with
     *               which the event is associated (never {@code null})
     */
    public DecompressJarEvent(Object source) {
        super(source);
        log.info("start customer initialize event");
        decompressJarFile();
    }

    private void decompressJarFile() {
        String path = System.getProperty("user.dir");
        String jarFile = path + java.io.File.separator + "api.jar";
        log.info("jar file {} , dest dir {}", jarFile, path);
        DataFlowUtils.extractJar(jarFile, path);
    }
}
  1. 修改启动类
   ConfigurableApplicationContext context = SpringApplication.run(EmailManagementApplication.class, args);
        context.publishEvent(new DecompressJarEvent("decompressJarEvent"));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值