Java递归查找指定目录下的特定格式的文件 并压缩到指定目录下
2014-09-28 12:13
405 查看
本文参考了网上的文章,搜索指定目录下以某种后缀名为结尾的文件,本文中查找的是以.log结尾的文件,然后压缩到指定目标文件夹中。该功能适合做系统维护用,比如可以定期清理服务器某个目录下的日志文件,实现压缩存档,减少服务器的占用空间等,也可以在压缩后删除原来的指定后缀的文件,只要增加点删除的代码就可以了。
本人亲测可用。
* 算法简述:
* 从某个给定的需查找的文件夹出发,搜索该文件夹的所有子文件夹及文件,
* 若为文件,则进行匹配,匹配成功则加入结果集,若为子文件夹,则进队列。同时将加入List中的文件压缩到指定的目标目录下
* 队列不空,重复上述操作,队列为空,程序结束,返回结果。
* 支持中文,解决压缩后中文名乱码的问题,用的是Ant.jar
*
废话不多说,上代码:
功能包含两个类:
FileZipUtil类代码:
本人亲测可用。
* 算法简述:
* 从某个给定的需查找的文件夹出发,搜索该文件夹的所有子文件夹及文件,
* 若为文件,则进行匹配,匹配成功则加入结果集,若为子文件夹,则进队列。同时将加入List中的文件压缩到指定的目标目录下
* 队列不空,重复上述操作,队列为空,程序结束,返回结果。
* 支持中文,解决压缩后中文名乱码的问题,用的是Ant.jar
*
废话不多说,上代码:
功能包含两个类:
FileSearchUtil类代码:
package org.jack.tools.file; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class FileSearchUtil { /** * 递归查找文件 * @param baseDirName 查找的文件夹路径 * @param targetFileName 需要查找的文件名 * @param fileList 查找到的文件集合 */ public static void findFiles(String baseDirName, String targetFileName, List<File> fileList) { /** * 算法简述: * 从某个给定的需查找的文件夹出发,搜索该文件夹的所有子文件夹及文件, * 若为文件,则进行匹配,匹配成功则加入结果集,若为子文件夹,则进队列。同时将加入List中的文件压缩到指定的目标目录下 * 队列不空,重复上述操作,队列为空,程序结束,返回结果。 */ String tempName = null; //判断目录是否存在 File baseDir = new File(baseDirName); if (!baseDir.exists() || !baseDir.isDirectory()){ System.out.println("文件查找失败:" + baseDirName + "不是一个目录!"); } else { String[] filelist = baseDir.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(baseDirName + "\\" + filelist[i]); //System.out.println(readfile.getName()); if(!readfile.isDirectory()) { tempName = readfile.getName(); if (FileSearchUtil.wildcardMatch(targetFileName, tempName)) { //匹配成功,将文件名添加到结果集 fileList.add(readfile.getAbsoluteFile()); } } else if(readfile.isDirectory()){ findFiles(baseDirName + "\\" + filelist[i],targetFileName,fileList); } } } } /** * 通配符匹配 * @param pattern 通配符模式 * @param str 待匹配的字符串 * @return 匹配成功则返回true,否则返回false */ private static boolean wildcardMatch(String pattern, String str) { int patternLength = pattern.length(); int strLength = str.length(); int strIndex = 0; char ch; for (int patternIndex = 0; patternIndex < patternLength; patternIndex++) { ch = pattern.charAt(patternIndex); if (ch == '*') { //通配符星号*表示可以匹配任意多个字符 while (strIndex < strLength) { if (wildcardMatch(pattern.substring(patternIndex + 1), str.substring(strIndex))) { return true; } strIndex++; } } else if (ch == '?') { //通配符问号?表示匹配任意一个字符 strIndex++; if (strIndex > strLength) { //表示str中已经没有字符匹配?了。 return false; } } else { if ((strIndex >= strLength) || (ch != str.charAt(strIndex))) { return false; } strIndex++; } } return (strIndex == strLength); } //格式化日期 public static String formatDate(Date date, String pattern){ String dateStr = null;; DateFormat format = new SimpleDateFormat(pattern); dateStr = format.format(date); return dateStr; } public static void main(String[] paramert) { //在此目录中找文件 String baseDIR = "D:/user/test/"; String datePat1 = "yyyy-MM-dd hh:mm:ss"; String datePat2 = "yyyyMMddhhmmss"; String targetZipPath = "D:/user/testcopy/" + "archive_" + formatDate(new Date(), datePat2) + ".zip"; ; String comment = "log文件压缩存档,存档时间: " + formatDate(new Date(), datePat1); //找扩展名为log的文件,找到后存入List<File> resultList中 String fileName = "*.log"; List<File> resultList = new ArrayList<File>(); FileSearchUtil.findFiles(baseDIR, fileName, resultList); if (resultList.size() == 0) { System.out.println("No File Found."); } else { for (int i = 0; i < resultList.size(); i++) { System.out.println(resultList.get(i));//显示查找结果。 } try {//把找到的指定后缀名的文件压缩到指定的文件夹中,压缩后的文件是以zip结尾,并添加注释到压缩包中 FileZipUtil.compress(resultList, targetZipPath, "GBK", comment); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }
FileZipUtil类代码:
package org.jack.tools.file; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import java.util.Enumeration; import java.util.List; import java.util.zip.CRC32; import java.util.zip.CheckedOutputStream; import java.util.zip.Deflater; import java.util.zip.ZipException; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream; public class FileZipUtil { /** * @Description: * 压缩文件 * @param sourcePath 将要压缩的文件或目录的路径,请使用绝对路径 * @param zipPath 生成压缩文件的路径,请使用绝对路径。如果该路径以“.zip”为结尾, * 则压缩文件的名称为此路径;如果该路径不以“.zip”为结尾,则压缩文件的名称 * 为该路径加上将要压缩的文件或目录的名称,再加上以“.zip”结尾 * @param encoding 压缩编码 * @param comment 压缩注释 */ public static void compress(String sourcePath, String zipPath, String encoding, String comment) throws FileNotFoundException, IOException { // 判断要压缩的文件是否存在 File sourceFile = new File(sourcePath); if (!sourceFile.exists() || (sourceFile.isDirectory() && sourceFile.list().length == 0)) { throw new FileNotFoundException("要压缩的文件或目录不存在,或者要压缩的目录为空"); } // 设置压缩文件路径,默认为将要压缩的路径的父目录为压缩文件的父目录 if (zipPath == null || "".equals(zipPath)) { String sourcePathName = sourceFile.getAbsolutePath(); int index = sourcePathName.lastIndexOf("."); zipPath = (index > -1 ? sourcePathName.substring(0, index) : sourcePathName) + ".zip"; } else { // 如果压缩路径为目录,则将要压缩的文件或目录名做为压缩文件的名字,这里压缩路径不以“.zip”为结尾则认为压缩路径为目录 if(!zipPath.endsWith(".zip")){ // 如果将要压缩的路径为目录,则以此目录名为压缩文件名;如果将要压缩的路径为文件,则以此文件名(去除扩展名)为压缩文件名 String fileName = sourceFile.getName(); int index = fileName.lastIndexOf("."); zipPath = zipPath + File.separator + (index > -1 ? fileName.substring(0, index) : fileName) + ".zip"; } } // 设置解压编码 if (encoding == null || "".equals(encoding)) { encoding = "GBK"; } // 要创建的压缩文件的父目录不存在,则创建 File zipFile = new File(zipPath); if (!zipFile.getParentFile().exists()) { zipFile.getParentFile().mkdirs(); } // 创建压缩文件输出流 FileOutputStream fos = null; try { fos = new FileOutputStream(zipPath); } catch (FileNotFoundException e) { if (fos != null) { try{ fos.close(); } catch (Exception e1) {} } } // 使用指定校验和创建输出流 CheckedOutputStream csum = new CheckedOutputStream(fos, new CRC32()); // 创建压缩流 ZipOutputStream zos = new ZipOutputStream(csum); // 设置编码,支持中文 zos.setEncoding(encoding); // 设置压缩包注释 zos.setComment(comment); // 启用压缩 zos.setMethod(ZipOutputStream.DEFLATED); // 设置压缩级别为最强压缩 zos.setLevel(Deflater.BEST_COMPRESSION); // 压缩文件缓冲流 BufferedOutputStream bout = null; try { // 封装压缩流为缓冲流 bout = new BufferedOutputStream(zos); // 对数据源进行压缩 compressRecursive(zos, bout, sourceFile, sourceFile.getParent()); } finally { if (bout != null) { try{ bout.close(); } catch (Exception e) {} } } } /** * @Description: * 压缩文件,支持将多个文件或目录压缩到同一个压缩文件中 * @param sourcePath 将要压缩的文件或目录的路径的集合,请使用绝对路径 * @param zipPath 生成压缩文件的路径,请使用绝对路径。该路不能为空,并且必须以“.zip”为结尾 * @param encoding 压缩编码 * @param comment 压缩注释 */ public static void compress(List<File> sourcePaths, String zipPath, String encoding, String comment) throws FileNotFoundException, IOException { // 设置压缩文件路径,默认为将要压缩的路径的父目录为压缩文件的父目录 if (zipPath == null || "".equals(zipPath) || !zipPath.endsWith(".zip")) { throw new FileNotFoundException("必须指定一个压缩路径,而且该路径必须以'.zip'为结尾"); } // 设置解压编码 if (encoding == null || "".equals(encoding)) { encoding = "GBK"; } // 要创建的压缩文件的父目录不存在,则创建 File zipFile = new File(zipPath); if (!zipFile.getParentFile().exists()) { zipFile.getParentFile().mkdirs(); } // 创建压缩文件输出流 FileOutputStream fos = null; try { fos = new FileOutputStream(zipPath); } catch (FileNotFoundException e) { if (fos != null) { try{ fos.close(); } catch (Exception e1) {} } } // 使用指定校验和创建输出流 CheckedOutputStream csum = new CheckedOutputStream(fos, new CRC32()); // 创建压缩流 ZipOutputStream zos = new ZipOutputStream(csum); // 设置编码,支持中文 zos.setEncoding(encoding); // 设置压缩包注释 zos.setComment(comment); // 启用压缩 zos.setMethod(ZipOutputStream.DEFLATED); // 设置压缩级别为最强压缩 zos.setLevel(Deflater.BEST_COMPRESSION); // 压缩文件缓冲流 BufferedOutputStream bout = null; try { // 封装压缩流为缓冲流 bout = new BufferedOutputStream(zos); // 迭代压缩每一个路径 for (int i=0,len=sourcePaths.size(); i<len; i++) { // 对数据源进行压缩 compressRecursive(zos, bout, sourcePaths.get(i), sourcePaths.get(i).getParent()); } } finally { if (bout != null) { try{ bout.close(); } catch (Exception e) {} } } } /** * @Description: * 压缩文件时,所使用的迭代方法 * @param zos 压缩输出流 * @param bout 封装压缩输出流的缓冲流 * @param sourceFile 将要压缩的文件或目录的路径 * @param prefixDir 整个将要压缩的文件或目录的父目录,传入此值为了获取压缩条目的名称 */ private static void compressRecursive(ZipOutputStream zos, BufferedOutputStream bout, File sourceFile, String prefixDir) throws IOException, FileNotFoundException { // 获取压缩条目名,初始时将要压缩的文件或目录的相对路径 String entryName = sourceFile.getAbsolutePath().substring(prefixDir.length() + File.separator.length()); // 判断是文件还是目录,如果是目录,则继续迭代压缩 if (sourceFile.isDirectory()) { // 如果是目录,则需要在目录后面加上分隔符('/') //ZipEntry zipEntry = new ZipEntry(entryName + File.separator); //zos.putNextEntry(zipEntry); // 获取目录中的文件,然后迭代压缩 File[] srcFiles = sourceFile.listFiles(); for (int i = 0; i < srcFiles.length; i++) { // 压缩 compressRecursive(zos, bout, srcFiles[i], prefixDir); } } else { // 开始写入新的ZIP文件条目并将流定位到条目数据的开始处 ZipEntry zipEntry = new ZipEntry(entryName); // 向压缩流中写入一个新的条目 zos.putNextEntry(zipEntry); // 读取将要压缩的文件的输入流 BufferedInputStream bin = null; try{ // 获取输入流读取文件 bin = new BufferedInputStream(new FileInputStream(sourceFile)); // 读取文件,并写入压缩流 byte[] buffer = new byte[1024]; int readCount = -1; while ((readCount = bin.read(buffer)) != -1) { bout.write(buffer, 0, readCount); } // 注,在使用缓冲流写压缩文件时,一个条件完后一定要刷新,不然可能有的内容就会存入到后面条目中去了 bout.flush(); // 关闭当前ZIP条目并定位流以写入下一个条目 zos.closeEntry(); } finally { if (bin != null) { try { bin.close(); } catch (IOException e) {} } } } } /** * @Description: * 解压文件 * @param zipPath 被压缩文件,请使用绝对路径 * @param targetPath 解压路径,解压后的文件将会放入此目录中,请使用绝对路径 * 默认为压缩文件的路径的父目录为解压路径 * @param encoding 解压编码 */ @SuppressWarnings("unchecked") public static void decompress(String zipPath, String targetPath, String encoding) throws FileNotFoundException, ZipException, IOException { // 获取解缩文件 File file = new File(zipPath); if (!file.isFile()) { throw new FileNotFoundException("要解压的文件不存在"); } // 设置解压路径 if (targetPath == null || "".equals(targetPath)) { targetPath = file.getParent(); } // 设置解压编码 if (encoding == null || "".equals(encoding)) { encoding = "GBK"; } // 实例化ZipFile对象 ZipFile zipFile = new ZipFile(file, encoding); // 获取ZipFile中的条目 Enumeration<ZipEntry> files = zipFile.getEntries(); // 迭代中的每一个条目 ZipEntry entry = null; // 解压后的文件 File outFile = null; // 读取压缩文件的输入流 BufferedInputStream bin = null; // 写入解压后文件的输出流 BufferedOutputStream bout = null; while (files.hasMoreElements()) { // 获取解压条目 entry = files.nextElement(); // 实例化解压后文件对象 outFile = new File(targetPath + File.separator + entry.getName()); // 如果条目为目录,则跳向下一个 if (entry.getName().endsWith(File.separator)) { outFile.mkdirs(); continue; } // 创建目录 if (!outFile.getParentFile().exists()) { outFile.getParentFile().mkdirs(); } // 创建新文件 outFile.createNewFile(); // 如果不可写,则跳向下一个条目 if (!outFile.canWrite()) { continue; } try { // 获取读取条目的输入流 bin = new BufferedInputStream(zipFile.getInputStream(entry)); // 获取解压后文件的输出流 bout = new BufferedOutputStream(new FileOutputStream(outFile)); // 读取条目,并写入解压后文件 byte[] buffer = new byte[1024]; int readCount = -1; while ((readCount = bin.read(buffer)) != -1) { bout.write(buffer, 0, readCount); } } finally { try { bin.close(); bout.flush(); bout.close(); } catch (Exception e) {} } } } public static void main(String[] args) throws Exception{ compressTest(); } public static void compressTest() throws Exception { String sourcePath = "D:/user/test/"; String zipPath = "D:/user/testcopy/" + "archive.zip"; String comment = "log文件压缩存档,时间: " + new Date(); compress(sourcePath, zipPath, "GBK", comment); } }
相关文章推荐
- grep如何递归目录并在指定类型文件中查找
- 查找指定目录下的特定文件 (包括子目录)
- JAVA读取zip文件 JAVA读取压缩文件 解压zip里面的指定格式文件
- java递归删除指定目录下的文件和文件夹
- IO流_递归输出指定目录下所有的java文件的绝对路径案例
- shell 定时删除某个目录指定格式文件(递归)
- Java基础知识强化之IO流笔记14:递归之输出指定目录下所有java文件绝对路径的案例
- find命令查找特定文件,并通过-exec 选项将其按照原来的目录树打包成压缩文件
- 对指定的目录进行递归获取递归过程所有的java文件的路径。
- 递归 / 迭代两种方法实现查找指定目录下所有文件
- JavaIO之删除指定目录(递归删除,先删除目录下的文件再删除目录)
- findfirst findnext 递归查找指定目录下所有子目录下所有文件,为什么总是死机?
- java 使用递归获取指定文件路径目录,删除指定的文件后缀(可扩展,根据具体需要的删除的后缀进行配置)~~
- linux在指定目录下查找包含特定文字的文件
- C# 查找指定目录下指定文件名或格式的文件并将结果保存到文本
- 用python查找在指定目录下特定文件夹下的指定文件
- 续:C# 趣味小程序(4)——遍历特定目录及其子目录 (应用实例,压缩指定目录下的所有文件)
- java如何查找目录下是否有特定文件
- java 把指定文件夹下的所有文件目录和文件都压缩到指定文件夹下
- Rhel7 find查找属于特定用户或组的文件,并将其拷贝到指定目录