日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2023/12/10 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的Java zip解压,并遍历zip中的配置文件 .cfg或.properties的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。