Extract the files in the specified location in the jar to the specified directory on the disk

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

@Slf4j
public class JarUtils {

    public static void main(String[] args) {
        String jarPath = "E:\\git\\xxx\\xxx-xxx-service\\target\\xxx-xxx-service-1.0.0.jar";
        String srcFolderInJar = "BOOT-INF/classes/windows_dll/";
        String targetFolderInDisk = "E:\\git\\xxx\\dll\\windows";
        extractFilesFromJarToDisk( jarPath,srcFolderInJar,targetFolderInDisk );
    }

    /**
     *
     * @param jarPath 例如:E:\git\xxx\xxx-xxx-service\target\xxx-xxx-service-1.0.0.jar
     * @param srcFolderInJar 例如:BOOT-INF/classes/windows_dll/
     * @param targetFolderInDisk 例如:E:\git\xxx\dll\windows
     */
    public static void extractFilesFromJarToDisk( String jarPath,String srcFolderInJar,String targetFolderInDisk ) {
        JarFile jar = null;
        try{
            jar = new JarFile( jarPath );
            Enumeration entries = jar.entries();
            while (entries.hasMoreElements()) {
                JarEntry jarEntry = (JarEntry) entries.nextElement();
                String path_inJar = jarEntry.getName();
                if( !path_inJar.startsWith( srcFolderInJar ) ){
                   continue;
                }
                boolean directory = jarEntry.isDirectory();
                if( directory ){
                    if( path_inJar.equals( srcFolderInJar ) ){
                        continue;
                    }
                    String path_disk = targetFolderInDisk + "\\" +  path_inJar.replaceAll(srcFolderInJar, "");
                    System.out.println( path_inJar + " 目录 复制到本次磁盘 " +path_disk  );
                    File file = new File(path_disk);
                    file.mkdirs();
                }
            }

            entries = jar.entries();
            while (entries.hasMoreElements()) {
                JarEntry jarEntry = (JarEntry) entries.nextElement();
                String path_inJar = jarEntry.getName();
                if( !path_inJar.startsWith( srcFolderInJar ) ){
                    continue;
                }
                boolean directory = jarEntry.isDirectory();
                if( !directory ){
                    // 是文件
                    InputStream fis = null;
                    FileOutputStream fos = null;
                    try {
                        String path_disk = targetFolderInDisk + "/" + path_inJar.replaceAll(srcFolderInJar, "");
                        fis = jar.getInputStream( jarEntry );
                        fos = new FileOutputStream(path_disk);
                        FileCopyUtils.copy( fis,fos );
                    }catch ( Exception e ){
                        e.printStackTrace();
                    }finally {
                        if( fos != null ){
                            try {
                                fos.close();
                            }catch ( Exception e ){
                                e.printStackTrace();
                            }
                        }
                        if( fis != null ){
                            try {
                                fis.close();
                            }catch ( Exception e ){
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            log.error("提取文件异常", e);
        }finally {
            if( jar != null ){
                try {
                    jar.close();
                }catch ( Exception e ){
                    e.printStackTrace();
                }
            }
        }
    }

     public static String getJarDirPath() throws FileNotFoundException, UnsupportedEncodingException {
        // 第一种
        File file = new File(ResourceUtils.getURL("classpath:").getPath());
        if(!file.exists()){
            file = new File("");
        }
        String path1 = file.getAbsolutePath();
        System.out.println( "path1 = " + path1 );

        //第二种
        String path2 = System.getProperty("user.dir");
        System.out.println( "path2 = " + path2 );

        //第三种
        String path3 = URLDecoder.decode(ClassUtils.getDefaultClassLoader().getResource("").getPath(), "utf-8");
        System.out.println( "path3 = " + path3 );

        //第四种
        String path4 = ResourceUtils.getURL("classpath:").getPath();
        System.out.println( "path4 = " + path4 );

        //第五种
        String path5 = new ApplicationHome(JarUtils.class).getSource().getParentFile().toString();
        System.out.println( "path5 = " + path5 );
        path1 = MyStringUitls.null2emptyWithTrim( path1 );
        path2 = MyStringUitls.null2emptyWithTrim( path2 );
        path3 = MyStringUitls.null2emptyWithTrim( path3 );
        path4 = MyStringUitls.null2emptyWithTrim( path4 );
        path5 = MyStringUitls.null2emptyWithTrim( path5 );
        if( path1.length() > 0 && !path1.contains( ".jar" ) ){
            return path1;
        }
        if( path2.length() > 0 && !path2.contains( ".jar" ) ){
            return path2;
        }
        if( path3.length() > 0 && !path3.contains( ".jar" ) ){
            return path3;
        }
        if( path4.length() > 0 && !path4.contains( ".jar" ) ){
            return path4;
        }
        if( path5.length() > 0 && !path5.contains( ".jar" ) ){
            return path5;
        }
        return null;
    }
}

Guess you like

Origin blog.csdn.net/heshiyuan1406146854/article/details/130398730