java压缩文件或复杂文件夹,并对重复文件自动重命名

tech2026-06-15  1

最近整了一个压缩文件的工具类,功能:

压缩文件夹,也可以压缩单独的文件压缩空文件夹(额..)目录结构是否保留可选压缩复杂文件夹(文件夹中带有文件夹)不保留目录结构时重命名同名文件

参考代码 

import java.io.*; import java.util.HashMap; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * @author 落尘 */ public class ZipUtils { private static HashMap<Object, Object> index = new HashMap<>(); private static int count = 0; public static void main(String[] args) { try { File file = zip("zipfile.zip", new File("C:\\TEMP\\doc"),false); System.out.println(file.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } } /** * 压缩目标文件/夹, 存放于相同目录下(同级) * @param compressedFileName 压缩后的文件名 * @param filePath 需要压缩的文件路径 * @param keepStructure 是否保留目录结构 * @return 压缩后的文件对象 * @throws IOException 压缩异常 */ public static File zip(String compressedFileName,String filePath,boolean keepStructure) throws IOException { return zip(compressedFileName,new File(filePath),keepStructure); } /** * 压缩目标文件/夹, 存放于相同目录下(同级) * @param compressedFileName 压缩后的文件名 * @param file 需要压缩的文件 * @param keepStructure 是否保留目录结构 * @return 压缩后的文件对象 * @throws IOException 压缩异常 */ public static File zip(String compressedFileName,File file,boolean keepStructure) throws IOException { if (file == null || !file.exists()){ return null; } if (isEmpty(compressedFileName)){ compressedFileName = file.getName(); } // 维护一个目录 if (keepStructure){ count = 0; index = new HashMap<>(10); } // 定义压缩文件名称 File zipFile = new File(file.getParentFile().getAbsolutePath() + File.separator + compressedFileName); // 压缩流对象 ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); // 文件直接压缩,文件夹递归压缩 if (!file.isDirectory()){ addCompressedFile(zipOut,null,file); }else { addCompressedFloder(zipOut,null,file,keepStructure); } zipOut.close(); System.out.println("压缩完成"); return zipFile; } /** * 压缩文件夹 * @param zipOut 压缩流 * @param parentPath 压缩文件路径 * @param file 文件夹 * @param keepStructure 是否保留目录结构 */ private static void addCompressedFloder(ZipOutputStream zipOut,String parentPath,File file,boolean keepStructure) throws IOException { if (file == null || !file.exists()) { return; } parentPath = empty(parentPath); if (!file.isDirectory()){ if (keepStructure){ addCompressedFile(zipOut,parentPath,file); }else{ addCompressedFile(zipOut,null,file); } }else{ System.out.println("读取目标文件夹"+file.getName()); File[] files = file.listFiles(); assert files != null; // 生成空文件夹 if (keepStructure) { zipOut.putNextEntry(new ZipEntry(parentPath + file.getName() + File.separator)); } for (File openFile: files) { addCompressedFloder(zipOut,parentPath + file.getName() + File.separator,openFile,keepStructure); } } } /** * 压缩文件 * @param zipOut 压缩流 * @param parentPath 压缩文件目录 * @param file 文件夹 * @throws IOException */ private static void addCompressedFile(ZipOutputStream zipOut,String parentPath,File file) throws IOException { InputStream input = new FileInputStream(file); String fileName = file.getName(); System.out.print("压缩目标文件"+fileName); if (parentPath != null){ System.out.println(" 目录:" + parentPath + fileName); zipOut.putNextEntry(new ZipEntry(parentPath + fileName)); } else { System.out.println(" 目录:" + fileName); if (index.containsKey(fileName)){ count ++; zipOut.putNextEntry(new ZipEntry("(" + count + ")" + fileName)); index.put("(" + count + ")" + fileName,true); } else { zipOut.putNextEntry(new ZipEntry(fileName)); index.put(fileName,true); } } int temp; while((temp=input.read())!=-1){ zipOut.write(temp); } zipOut.closeEntry(); input.close(); } private static String empty(String s){ if (isEmpty(s)){ return ""; } return s; } private static boolean isEmpty(String str) { return str == null || "".equals(str.trim()); } }

压缩单个文件

 

压缩文件夹 

保留目录结构           不保留目录结构

 压缩复杂文件夹

 

简单的测试...

 压缩复杂文件夹不保留目录结构时同名文件的处理

 

对于同名文件处理是在前面加个数字,保留目录结构时:

这边是维护了一个hashmap作为目录保证文件名的唯一,有更好的做法欢迎评论留言一起讨论

最新回复(0)