读取文件夹下所有压缩包内容

/**
 * </p> * description * <p> * * @author wentao.liu01@foxmail.com 2021/06/03 21:00
 */

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
 * java 读取zip demo
 */
public class ZipRead {

    private final static String path = System.getProperty("user.dir");

    public static void main(String[] a) throws Exception {
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter a groupId :");
        String groupId = scan.next();

        List<String> files = getFiles(path + File.separator + "dm");
        List<String> results = new ArrayList<>();
        for (String file : files) {
            results.addAll(handleZip(file, groupId));
        }
        File file = new File(path + File.separator + "formId.txt");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }

        //2:准备输出流
        Writer out = new FileWriter(file);
        for (String result : results) {
            // 这里额外处理下继承的单据 把他处理成 对应的im模块
            result = result.replace("digi_inventory", "im");

            out.write(result);
            out.write(System.getProperty("line.separator"));
        }
        File bizFile = new File(path + File.separator + "bizCloud.txt");
        Writer bizOut = new FileWriter(bizFile);
        for (String result : files) {
            bizOut.write(String.format("<BizModule Id=\"%s\"/>",
                    result.substring(result.indexOf("-") + 1, result.lastIndexOf("-dm"))));
            bizOut.write(System.getProperty("line.separator"));
        }
        out.close();
        bizOut.close();
    }

    /**
     * <p>
     * 获取某个目录下所有直接下级文件,不包括目录下的子目录的下的文件,所以不用递归获取
     * </p>
     *
     * @param path 路径
     * @return List<String>
     * @author wentao.liu01@foxmail.com 2021-06-03 21:07
     */
    public static List<String> getFiles(String path) {
        List<String> files = new ArrayList<>();
        File file = new File(path);
        File[] tempList = file.listFiles();
        assert tempList != null;
        for (File value : tempList) {
            if (value.isFile()) {
                //===============================================================================
                //  TODO 获取你想要的文件
                //===============================================================================
                if (value.toString().endsWith("zip")) {
                    files.add(value.toString());
                }
                //文件名,不包含路径
                //String fileName = tempList[i].getName();
            }
            if (value.isDirectory()) {
                //这里就不递归了,
            }
        }
        return files;
    }


    /**
     * <p>
     * 获取zip文件内 处理好的文件内容数据
     * </p>
     *
     * @param path 路径
     * @return List<String>
     * @author wentao.liu01@foxmail.com 2021-06-03 21:07
     */
    public static List<String> handleZip(String path, String groupId) {
        List<String> list = new ArrayList<>();

        File file = new File(path);


        Charset gbk = Charset.forName("GBK");
        try (ZipFile zipFile = new ZipFile(file, gbk);
             FileInputStream fis = new FileInputStream(file);
             ZipInputStream zin = new ZipInputStream(fis, gbk);) {

            ZipEntry ze = null;
            //===============================================================================
            //  TODO 处理压缩包内文件逻辑
            //===============================================================================
            while ((ze = zin.getNextEntry()) != null) {
                if (!ze.isDirectory() && ze.toString().endsWith("txt")) {
                    InputStream inputStream = zipFile.getInputStream(ze);
                    List<String> texts = readLines(inputStream, "UTF-8");
                    for (String text : texts) {
                        if (text.endsWith(".dym")) {
                            list.add(
                                    String.format("<Objects BizModule=\"%s\" Id=\"%s\" ApplicationGroupId=\"%s\"/>",
                                            path.substring(path.indexOf("-") + 1, path.lastIndexOf("-dm")),
                                            text.substring(text.lastIndexOf("/") + 1, text.lastIndexOf(".")),
                                            groupId));
                            /*list.add(text.substring(text.lastIndexOf("/") + 1, text.lastIndexOf(".")) + "     " +
                                    path.substring(path.lastIndexOf("\\") + 1, path.lastIndexOf("-dm")));*/
                        }

                    }
                    inputStream.close();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }

    /**
     * <p>
     * 传入一个输入流 按行读取行
     * </p>
     *
     * @param input   输入流
     * @param charset 字符编码
     * @return List<String> 输出对应的文件流
     * @author wentao.liu01@foxmail.com 2021-06-04 10:31
     */
    public static List<String> readLines(final InputStream input, String charset) throws IOException {
        final InputStreamReader readerIn = new InputStreamReader(input, charset);
        final BufferedReader reader = new BufferedReader(readerIn);
        final List<String> list = new ArrayList<>();
        String line;
        while ((line = reader.readLine()) != null) {
            list.add(line);
        }
        return list;
    }
}

基本上都是代码,内容都在代码里面,逻辑不复杂,不重复赘述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值