Java zip解压,并遍历zip中的配置文件 .cfg或.properties

1.解析cfg或properties配置文件

讲配置文件,读取,并封装成为map类型数据

/**
     * 解析cfg文件
     *
     * @param cfgFile
     * @return
     */
    public static Map<String, Object> readCfg(FileInputStream cfgFile) {
        Properties prop = new Properties();
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            // 读取cfg属性文件
            InputStream in = new BufferedInputStream(cfgFile);
            prop.load(new InputStreamReader(in, "GBK")); /// 加载属性列表
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while (it.hasNext()) {
                String key = it.next();
                map.put(key, prop.getProperty(key).replaceAll("\"", "").replaceAll(";", ""));
            }
            in.close();
        } catch (Exception e) {
            System.out.println(e);
        }
        return map;
    }

2、文件夹遍历

 /**
     * 文件夹遍历
     * @param path
     * @throws Exception
     */
    public static void traverse(String path,String parent_id) throws Exception {
        System.out.println("path---->" + path);
        File file = new File(path);
        Map<String, Object> map = new HashMap<String, Object>();
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length == 0) {
                System.out.println("文件夹是空的!");
                return;
            } else {
                String k_id = UuidUtil.get32UUID();

                for (File file2 : files) {
                    if (file2.isDirectory()) {//文件夹

                        traverse(file2.getAbsolutePath(),parent_id);
                        parent_id =  k_id;
                    } else if (file2.isFile()){//文件
                        if (file2.getName().endsWith(".cfg")) {
                            System.out.println("文件:" + file2.getAbsolutePath());
                            map = readCfg(new FileInputStream(file2));
                            System.out.println("-------------"+file2.getAbsolutePath()+"--start-----------");
                            map.put("path_url", getUrlPath(file2.getAbsolutePath()));
                            map.put("k_id", k_id);
                            map.put("parent_id", parent_id);
                            for (String key : map.keySet()) {
                                System.out.println(key+"--->"+map.get(key));
                            }
                            parent_id = k_id;
                            System.out.println("-------------"+file2.getAbsolutePath()+"--end-----------");
                        }
                    }
                }
            }
        } else {
            System.out.println("文件不存在!");
        }
    }

3、解压zip

 /**
     * 解压zip
     *
     * @param zipFile
     * @param descDir
     * @throws Exception
     */
    public static void unZipFiles(File zipFile, String descDir) throws Exception {
        System.out.println("******************解压开始********************");
        File pathFile = new File(descDir);
        if (!pathFile.exists()) {
            pathFile.mkdirs();
        }
        ZipFile zip = new ZipFile(zipFile);
        for (Enumeration entries = zip.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            String zipEntryName = entry.getName();
            InputStream in = zip.getInputStream(entry);
            String outPath = (descDir + "/" + zipEntryName).replaceAll("\\*", "/");
            // 判断路径是否存在,不存在则创建文件路径
            File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
            if (!file.exists()) {
                file.mkdirs();
            }
            // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
            if (new File(outPath).isDirectory()) {
                continue;
            }
            OutputStream out = new FileOutputStream(outPath);
            byte[] buf1 = new byte[1024];
            int len;
            while ((len = in.read(buf1)) > 0) {
                out.write(buf1, 0, len);
            }
            if ((zipEntryName.trim().lastIndexOf("/")) == -1) {

            }
            in.close();
            out.close();
        }

        System.out.println("******************解压完毕********************");
        System.out.println("******************遍历文件夹********************");
        String parent_id = "";
        traverse(descDir,parent_id);
    }

4,main方法

 
private static ZipFile zf;
private static ZipInputStream zin;
public static void main(String[] args) throws Exception {
        try {
            File file = new File("D:\\CCC.zip");
            unZipFiles(file, "D:\\CCC");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

代码下载地址:

http://download.csdn.net/download/u010308359/10143689

转载于:https://www.cnblogs.com/invban/p/7975583.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值