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

Java zip解压,并遍历zip中的配置文件 .cfg或.properties的更多相关文章

  1. Java解压上传zip或rar文件,并解压遍历文件中的html的路径

    1.本文只提供了一个功能的代码 public String addFreeMarker() throws Exception { HttpSession session = request.getSe ...

  2. 原生java 压缩解压zip文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  3. 解决ubuntu中zip解压的中文乱码问题

    转自解决ubuntu中zip解压的中文乱码问题 在我的ubuntu12.10中,发现显示中文基本都是正常的,只有在解压windows传过来的zip文件时,才会出现乱码.所以,我用另一个方法解决中文乱码 ...

  4. JAVA zip解压 MALFORMED 错误

    最近在在使用zip 解压时,使用JDK1.7及以上版本在解压时,某些文件会报异常 Exception in thread "main" java.lang.IllegalArgum ...

  5. linux中tar命令(打包、压缩、解压)、zip和unzip、rar多种压缩文件

    一.名词解释 打包:将一大堆文件或目录变成一个总的文件[tar命令] 压缩:将一个大的文件通过一些压缩算法变成一个小文件[gzip,bzip2等] Linux中很多压缩程序只能针对一个文件进行压缩,这 ...

  6. java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)

    // java批量解压文件夹下的所有压缩文件(.rar..zip..gz..tar.gz) 新建工具类: package com.mobile.utils; import com.github.jun ...

  7. Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)

    本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...

  8. java解压多目录Zip文件(解决中文乱码问题)--转载

    原文地址:http://zhangyongbo.iteye.com/blog/1749439 import java.io.BufferedOutputStream; import java.io.F ...

  9. Linux中解压、压缩 ZIP文件

    解压 unzip -o -d /home/v-gazh myfile.zip # 把myfile.zip文件解压到 /home/v-gazh/ # -o:不提示的情况下覆盖文件: # -d:-d /h ...

随机推荐

  1. 11.sklearn.preprocessing.LabelEncoder的作用

    In [5]: from sklearn import preprocessing ...: le =preprocessing.LabelEncoder() ...: le.fit(["p ...

  2. (转)Springboot 中filter 注入对象

    问题:我建立一个全局拦截器,当然,这是测试的时候建立的,我把它命名为LogFilter,它继承了Filter,web应用启动的顺序是:listener->filter->servlet,而 ...

  3. container / pull-left

    <div class="container"> <h2>实例</h2> <div class="pull-left"& ...

  4. 机器学习理论基础学习13--- 隐马尔科夫模型 (HMM)

    隐含马尔可夫模型并不是俄罗斯数学家马尔可夫发明的,而是美国数学家鲍姆提出的,隐含马尔可夫模型的训练方法(鲍姆-韦尔奇算法)也是以他名字命名的.隐含马尔可夫模型一直被认为是解决大多数自然语言处理问题最为 ...

  5. webdriver鼠标上下滑动

    有时候我们需要对窗口显示的页面上下滑动,以显示当前正在处理的位置,这就需要用到webdriver模拟鼠标上下滑动 package test20161201; import org.openqa.sel ...

  6. Jason使用

    Jason是一种数据传输时候的一种格式,类似XML. package liferay; import java.beans.IntrospectionException; import java.be ...

  7. Bus,Exclusive access,memory attribute

    指令LDREX,STREX是在armv6中新加的指令,配合AMBA3--AXI中的lock[1:0]信号. 在Atomic Access一节中是这么规定的:ARLOCK[1:0]/AWLOCK[1:0 ...

  8. LINUX环境变量(二)

    一.Shell变量分为本地变量和环境变量. 1.本地变量:在用户现有运行的脚本中使用 a) 定义本地变量 格式: variable-name=value b) 显示本地变量 格式: set  c) 清 ...

  9. python之路----黏包的解决方案

    黏包的解决方案 远程执行命令 # server 下发命令 给client import socket sk = socket.socket() sk.bind(('127.0.0.1',8080)) ...

  10. python之路----模块调用

    如何使用模块? 1 import 示例文件:自定义模块my_module.py,文件名my_module.py,模块名my_module #my_module.py print('from the m ...