第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > Java 实现文件夹压缩成 ZIP 包(递归实现)

Java 实现文件夹压缩成 ZIP 包(递归实现)

时间:2024-03-14 06:09:35

相关推荐

Java 实现文件夹压缩成 ZIP 包(递归实现)

package org.jeecg.modules.system.util;import java.io.*;import java.util.zip.CRC32;import java.util.zip.CheckedInputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;/*** @ClassName ZipUtils* @Description 压缩文件工具类* @Author WangJing* @Date /9/2 3:06 下午* @Version V1.1.0*/public class ZipUtils {/*** 压缩成ZIP** @param sourceDirectory 源文件夹路径* @param zipFile压缩文件输出地址* @param compression是否压缩文件*true:*false:仅复制文件到ZIP包* @param keepDirStructure 是否保留原目录结构*true:保留目录结构*false:不保留(注意:不保留目录结构可能会出现同名文件,会导致压缩失败)* @throws RuntimeException*/public static void toZip(String sourceDirectory, String zipFile, boolean compression, boolean keepDirStructure)throws RuntimeException {try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(zipOut)) {File sourceFile = new File(sourceDirectory);compress(sourceFile, zipOut, sourceFile.getName(), compression, keepDirStructure, bufferedOutputStream);} catch (Exception e) {e.printStackTrace();}}/*** 递归压缩方法** @param sourceFile 源文件* @param zipOut压缩后的名称* @param name zip输出流* @param compression是否压缩文件* true:* false:仅复制文件到ZIP包* @param keepDirStructure是否保留原目录结构* true:保留目录结构* false:不保留(注意:不保留目录结构可能会出现同名文件,会导致压缩失败)* @param bufferedOutputStream* @throws Exception*/private static void compress(File sourceFile, ZipOutputStream zipOut, String name, boolean compression,boolean keepDirStructure, BufferedOutputStream bufferedOutputStream) throws Exception {if (sourceFile.isFile()) {//单文件,直接压缩try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(sourceFile))) {if (compression) {zipOut.putNextEntry(new ZipEntry(name));} else {ZipEntry entry = new ZipEntry(name);//核心,和复制粘贴效果一样,并没有压缩,但速度很快entry.setMethod(ZipEntry.STORED);entry.setSize(sourceFile.length());entry.setCrc(getFileCRCCode(sourceFile));zipOut.putNextEntry(entry);}int len = 0;byte[] data = new byte[8192];while ((len = bufferedInputStream.read(data)) != -1) {bufferedOutputStream.write(data, 0, len);}bufferedInputStream.close();bufferedOutputStream.flush();}} else {//目录或多文件File[] listFiles = sourceFile.listFiles();if (listFiles == null || listFiles.length == 0) {//目录复制// 需要保留原来的文件结构时,需要对空文件夹进行处理if (keepDirStructure) {// 空文件夹的处理zipOut.putNextEntry(new ZipEntry(name + "/"));// 没有文件,不需要文件的copyzipOut.closeEntry();}} else {for (File file : listFiles) {// 判断是否需要保留原来的文件结构if (keepDirStructure) {// 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,// 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了compress(file, zipOut, name + "/" + file.getName(), compression, keepDirStructure, bufferedOutputStream);} else {compress(file, zipOut, file.getName(), compression, keepDirStructure, bufferedOutputStream);}}}}}/*** 获取CRC32** @param file* @return* @throws Exception*/public static long getFileCRCCode(File file) throws Exception {BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));CRC32 crc32 = new CRC32();//CheckedInputStream一种输入流,它还维护正在读取的数据的校验和。 然后可以使用校验和来验证输入数据的完整性。CheckedInputStream checkedinputstream = new CheckedInputStream(bufferedInputStream, crc32);while (checkedinputstream.read() != -1) {}checkedinputstream.close();bufferedInputStream.close();return crc32.getValue();}public static void main(String[] args) throws Exception {long startTime = System.currentTimeMillis();String sourceDirectory = "/Users/wangjing/Desktop/22/operating_ambient";String zipFile = "/Users/wangjing/Desktop/22/1222.zip";boolean compression = true;boolean keepDirStructure = true;toZip(sourceDirectory, zipFile, compression, keepDirStructure);long endTime = System.currentTimeMillis();System.out.println("执行时间:" + (endTime - startTime) / 1000);}}

提供参考和交流,请勿用于商业用途,如有侵权联系本人删除!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。