话不多说上代码
package com.lens;
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class maintext {
public static void main(String[] args) throws Exception {
try {
File file = new File("D:\\谷歌下载\\8881e36387184c8185475b73f5f64c94.zip");
//unZipFiles(file, "D:\\谷歌下载\\老许");
// traverse("D:\\谷歌下载\\8ee541af4b8242a6b7eed359a62ab443","");
traverseFolder2("D:\\apache-tomcat-9.0.20-8847\\webapps\\dmap-web\\temp\\9f6f5b2ff18b4efc90c88c333664685d\\8881e36387184c8185475b73f5f64c94");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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 = "";
traverseFolder2(descDir);
}
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 ="1111111111111";
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("文件不存在!");
}
}
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;
}
public static void traverseFolder2(String path) {
File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
if (null == files || files.length == 0) {
System.out.println("文件夹是空的!");
return;
} else {
for (File file2 : files) {
if (file2.isDirectory()) {
System.out.println("文件夹:" + file2.getAbsolutePath());
traverseFolder2(file2.getAbsolutePath());
} else {
System.out.println("文件:" + file2.getAbsolutePath());
}
}
}
} else {
System.out.println("文件不存在!");
}
}
public static void zipUncompress(String inputFile, String destDirPath) throws Exception {
File srcFile = new File(inputFile);// 获取当前压缩文件
// 判断源文件是否存在
if (!srcFile.exists()) {
throw new Exception(srcFile.getPath() + "所指文件不存在");
}
// 开始解压
// 构建解压输入流
ZipInputStream zIn = new ZipInputStream(new FileInputStream(srcFile), Charset.forName("GBK"));
ZipEntry entry = null;
File file = null;
while ((entry = zIn.getNextEntry()) != null) {
if (!entry.isDirectory()) {
file = new File(destDirPath, entry.getName());
if (!file.exists()) {
new File(file.getParent()).mkdirs();// 创建此文件的上级目录
}
System.out.println(file.getAbsolutePath());
OutputStream out = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(out);
int len = -1;
byte[] buf = new byte[1024];
while ((len = zIn.read(buf)) != -1) {
bos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭
bos.close();
out.close();
}
}
zIn.close();
}
}