调用7z提取压缩包里的文件(包括文件夹)

因为我们不能够直接替换压缩包里某个文件夹下里的文件,所以我们就需要先提取出来。

import部分:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

代码:

        public static void extractFileOnArchiveByCall7z() {
        String filePathStr = "testUpdateZipFile"; // 提取的文件名
        String zipToolPath = "D:\\7-Zip\\7z.exe"; // 7z工具路径
        String sourceFilePath = "E:\\Test_JAVAProgram\\test7z\\testUpdateZipFile.zip"; // 压缩包,支持的压缩包类型挺多的,我目前只试过zip, ear
        String outputFilePath = "E:\\Test_JAVAProgram\\test7z"; // 提取出文件安放的路径
        String cmd = zipToolPath + " " + "x" + " " + sourceFilePath + " " + "-o" + outputFilePath + " " +  filePathStr + " "  + "-y";
        System.out.println(cmd);
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            Process process = Runtime.getRuntime().exec(cmd);
            is = process.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            String line = null;
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_42488909/article/details/113857654
今日推荐