java 将文件夹进行压缩,按指定大小进行分卷压缩
使用方法:
SplitZip splitZip = new SplitZip();
splitZip.start(file.getPath(),file.getPath());
package ext.xxx.util; import java.io.*; import java.util.zip.*; import static org.apache.commons.lang3.StringUtils.isBlank; /** * 分卷压缩工具 */ public class SplitZip { private static final int MAX_ZIP_SIZE = 1024 * 1024 * 300; // 300MB,可以改为自己想要的大小 private ZipOutputStream currentZipOutputStream = null; private File currentZipFile = null; private long totalBytesWritten = 0; private int currentZipCount = 0; private String basePath = ""; // 基本路径,用于在ZIP中保持相对路径 private String outZipName = "";//输出压缩包的名称 public void addFileToZip(File fileToAdd, String parentPath) throws IOException { long approximateFileSize = getApproximateFileSize(fileToAdd);//计算要压缩的文件(文件夹)大小 if (currentZipOutputStream == null || (totalBytesWritten + approximateFileSize > MAX_ZIP_SIZE && totalBytesWritten != 0)) { closeCurrentZip(); openNewZip(); } if (fileToAdd.isDirectory()) { File[] files = fileToAdd.listFiles(); if (files != null) { for (File file : files) { addFileToZip(file, parentPath + fileToAdd.getName() + "/"); } } } else { ZipEntry zipEntry = new ZipEntry(parentPath + fileToAdd.getName()); currentZipOutputStream.putNextEntry(zipEntry); FileInputStream fis = new FileInputStream(fileToAdd); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) >= 0) { currentZipOutputStream.write(buffer, 0, length); totalBytesWritten += length; // 更新已写入的总字节数 } fis.close(); currentZipOutputStream.closeEntry(); } } private long getApproximateFileSize(File file) { if (file.isDirectory()) { long length = 0; File[] files = file.listFiles(); if (files != null) { for (File childFile : files) { length += getApproximateFileSize(childFile); } } return length; } else { return file.length(); } } private void openNewZip() throws IOException { totalBytesWritten = 0; // 重置已写入的字节数 currentZipCount++; currentZipFile = new File(outZipName + "_" + currentZipCount + ".zip"); currentZipOutputStream = new ZipOutputStream(new FileOutputStream(currentZipFile)); } private void closeCurrentZip() throws IOException { if (currentZipOutputStream != null) { currentZipOutputStream.close(); currentZipOutputStream = null; } } public void zipDirectory(File directoryToZip, String baseDirectoryPath) throws IOException { basePath = baseDirectoryPath; if (directoryToZip.isDirectory()) { addFileToZip(directoryToZip, ""); } else { throw new IOException("The specified path is not a directory."); } closeCurrentZip(); // 确保关闭最后一个ZIP文件 } /** * * @param folder 要压缩的文件夹 * @param targetZipName 生成压缩包的名称 */ public void start(String folder,String targetZipName) throws Exception { if (isBlank(folder) || isBlank(targetZipName)) { throw new Exception("不能压缩。。。。。。"); } outZipName = targetZipName; File dirToZip = new File(folder); // 替换为你要压缩的目录路径 String baseDirPath = dirToZip.getName(); // 通常我们想要基于目录名来创建ZIP文件的根目录 try { zipDirectory(dirToZip, baseDirPath + "/"); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { SplitZip splitZip = new SplitZip(); splitZip.start("D:\\新建文件夹","D:\\新建文件夹");
} }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律