1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 批量压缩16万个文件夹为压缩包(.zip格式)

批量压缩16万个文件夹为压缩包(.zip格式)

时间:2021-02-08 01:28:06

相关推荐

批量压缩16万个文件夹为压缩包(.zip格式)

🍅程序员小王的博客:程序员小王的博客

🍅 欢迎点赞 👍 收藏 ⭐留言 📝

🍅 如有编辑错误联系作者,如果有比较好的文章欢迎分享给我,我会取其精华去其糟粕

🍅java自学的学习路线:java自学的学习路线

一、需求:将16万个文件夹批量压缩成.zip文件

二、代码实现步骤

1、将某个文件夹压缩为.zip文件实现

package com.tjcu.util;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;/*** @author :王恒杰* @date :Created in /3/25 18:13* @description:测试压缩文件夹*/public class TestCompress {public static void main(String[] args) throws FileNotFoundException {//目前文件夹的位置String srcDir="D:/test/K022600000001";File file =new File (srcDir);//解压到的位置String targetPath="D:/test/";new File(targetPath+file+".zip");FileOutputStream fos1 = new FileOutputStream(new File(srcDir+".zip"));boolean KeepDirStructure =true;toZip(srcDir, fos1, KeepDirStructure);}public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure)throws RuntimeException{long start = System.currentTimeMillis();ZipOutputStream zos = null ;try {zos = new ZipOutputStream(out);File sourceFile = new File(srcDir);compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure);long end = System.currentTimeMillis();System.out.println("压缩完成,耗时:" + (end - start) +" ms");} catch (Exception e) {throw new RuntimeException("zip error from ZipUtils",e);}finally{if(zos != null){try {zos.close();} catch (IOException e) {e.printStackTrace();}}}}private static final int BUFFER_SIZE = 2 * 1024;private static void compress(File sourceFile, ZipOutputStream zos, String name,boolean KeepDirStructure) throws Exception{byte[] buf = new byte[BUFFER_SIZE];if(sourceFile.isFile()){// 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字zos.putNextEntry(new ZipEntry(name));// copy文件到zip输出流中int len;FileInputStream in = new FileInputStream(sourceFile);while ((len = in.read(buf)) != -1){zos.write(buf, 0, len);}// Complete the entryzos.closeEntry();in.close();} else {File[] listFiles = sourceFile.listFiles();if(listFiles == null || listFiles.length == 0){// 需要保留原来的文件结构时,需要对空文件夹进行处理if(KeepDirStructure){// 空文件夹的处理zos.putNextEntry(new ZipEntry(name + "/"));// 没有文件,不需要文件的copyzos.closeEntry();}}else {for (File file : listFiles) {// 判断是否需要保留原来的文件结构if (KeepDirStructure) {// 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,// 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了compress(file, zos, name + "/" + file.getName(),KeepDirStructure);} else {compress(file, zos, file.getName(),KeepDirStructure);}}}}}}

2、判断是否需要保留原来的文件结构

// 判断是否需要保留原来的文件结构if (KeepDirStructure) {// 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,// 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了compress(file, zos, name + "/" + file.getName(),KeepDirStructure);} else {compress(file, zos, file.getName(),KeepDirStructure);}

三、循环遍历文件夹进行压缩最终版

package com.tjcu;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class TestZip {public static void main(String[] args) throws IOException {String srcDir=null;FileOutputStream fos1=null;String targetPath="D:\\0325\\";File file =new File ("F:\\亿米报备");File filetarget =null;File[] fileList = file.listFiles();boolean KeepDirStructure =true;for (File file2 : fileList) {srcDir=file2.getAbsolutePath();System.out.println(targetPath+file2.getName()+".zip");filetarget =new File(targetPath+file2.getName()+".zip");fos1 = new FileOutputStream(filetarget);toZip(srcDir, fos1, KeepDirStructure);System.out.println(srcDir);}fos1.close();}public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure)throws RuntimeException{long start = System.currentTimeMillis();ZipOutputStream zos = null ;try {zos = new ZipOutputStream(out);File sourceFile = new File(srcDir);compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure);long end = System.currentTimeMillis();System.out.println("压缩完成,耗时:" + (end - start) +" ms");} catch (Exception e) {throw new RuntimeException("zip error from ZipUtils",e);}finally{if(zos != null){try {zos.close();} catch (IOException e) {e.printStackTrace();}}}}private static final int BUFFER_SIZE = 2 * 1024;private static void compress(File sourceFile, ZipOutputStream zos, String name,boolean KeepDirStructure) throws Exception{byte[] buf = new byte[BUFFER_SIZE];if(sourceFile.isFile()){// 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字zos.putNextEntry(new ZipEntry(name));// copy文件到zip输出流中int len;FileInputStream in = new FileInputStream(sourceFile);while ((len = in.read(buf)) != -1){zos.write(buf, 0, len);}// Complete the entryzos.closeEntry();in.close();} else {File[] listFiles = sourceFile.listFiles();if(listFiles == null || listFiles.length == 0){// 需要保留原来的文件结构时,需要对空文件夹进行处理if(KeepDirStructure){// 空文件夹的处理zos.putNextEntry(new ZipEntry(name + "/"));// 没有文件,不需要文件的copyzos.closeEntry();}}else {for (File file : listFiles) {// 判断是否需要保留原来的文件结构if (KeepDirStructure) {// 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,// 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了compress(file, zos, name + "/" + file.getName(),KeepDirStructure);} else {compress(file, zos, file.getName(),KeepDirStructure);}}}}}}

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