导语
  接到一个需求,说是要把文件夹打包成成zip包,还要求使用Java代码来实现,想想既然是工具类,那么就来编写一个工具来试试。

废话不多说直接上代码

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.Arrays;
import java.util.LinkedList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;/*** 文件夹打压缩包** @author ZYGisComputer*/
public final class FileTOZip {/*** 将存放在sourceFilePath目录下的源文件,打包成fileName名称的ZIP文件,并存放到zipFilePath。** @param sourceFilePath 待压缩的文件路径* @param zipFilePath    压缩后存放路径* @param fileName       压缩后文件的名称* @return flag*/public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) {String fileNameExit=sourceFilePath+".zip";File file=new File(fileNameExit);if(file.exists()){file.delete();}boolean flag = false;File sourceFile = new File(sourceFilePath);FileInputStream fis = null;BufferedInputStream bis = null;FileOutputStream fos = null;ZipOutputStream zos = null;if (!sourceFile.exists()) {System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath + " 不存在. <<<<<<");} else {try {File zipFile = new File(zipFilePath + "/" + fileName + ".zip");if (zipFile.exists()) {System.out.println(">>>>>> " + zipFilePath + " 目录下存在名字为:"+ fileName + ".zip" + " 打包文件. <<<<<<");} else {File[] sourceFiles = sourceFile.listFiles();if (null == sourceFiles || sourceFiles.length < 1) {System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath+ " 里面不存在文件,无需压缩. <<<<<<");} else {fos = new FileOutputStream(zipFile);zos = new ZipOutputStream(new BufferedOutputStream(fos));byte[] bufs = new byte[1024 * 10];for (int i = 0; i < sourceFiles.length; i++) {// 创建ZIP实体,并添加进压缩包ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());zos.putNextEntry(zipEntry);sourceFiles[i].setExecutable(true);sourceFiles[i].setReadable(true);sourceFiles[i].setWritable(true);// 读取待压缩的文件并写进压缩包里fis = new FileInputStream(sourceFiles[i]);bis = new BufferedInputStream(fis, 1024 * 10);int read = 0;while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {zos.write(bufs, 0, read);}}flag = true;}}} catch (FileNotFoundException e) {e.printStackTrace();throw new RuntimeException(e);} catch (IOException e) {e.printStackTrace();throw new RuntimeException(e);} finally {// 关闭流try {if (null != bis) {bis.close();}if (null != zos) {zos.close();}} catch (IOException e) {e.printStackTrace();throw new RuntimeException(e);}}}return flag;}/*** 将文件打包成ZIP压缩文件,main方法测试** @param args*/public static void main(String[] args) {String sourceFilePath = "E:\\ruoyi\\uploadPath\\upload\\200";String zipFilePath = "E:\\ruoyi\\uploadPath\\upload";String fileName = "200";boolean flag = FileTOZip.fileToZip(sourceFilePath, zipFilePath,fileName);if (flag) {System.out.println(">>>>>> 文件打包成功. <<<<<<");} else {System.out.println(">>>>>> 文件打包失败. <<<<<<");}}public static void ZIPFile() throws Exception {String sourceDir = "E:\\ruoyi\\uploadPath\\upload";int parentDirectoryLen = sourceDir.lastIndexOf(File.separator) + 1;File[] copyfoldersList = new File(sourceDir).listFiles();FileOutputStream fos = new FileOutputStream("E:\\ruoyi\\uploadPath\\upload\\down.zip");ZipOutputStream zipOut = new ZipOutputStream(fos);for (int k = 0; k < copyfoldersList.length; k++) {if (copyfoldersList[k].isDirectory()) {LinkedList copysourcepath = new LinkedList(Arrays.asList(copyfoldersList[k].getAbsolutePath()));while (copysourcepath.size() > 0) {File folders = new File(copysourcepath.peek().toString());String[] file = folders.list();for (int i = 0; i < file.length; i++) {File ff = new File(copysourcepath.peek().toString(), file[i]);if (ff.isFile()) {FileInputStream fis = null;try {fis = new FileInputStream(ff);ZipEntry entry = new ZipEntry(ff.getAbsoluteFile().toString().substring(parentDirectoryLen));zipOut.putNextEntry(entry);int nNumber;Long len=Long.MIN_VALUE;byte[] buffer = new byte[len.intValue()];while ((nNumber = fis.read(buffer)) != -1)
//                                    System.out.println(buffer.toString());zipOut.write(buffer, 0, nNumber);} catch (IOException e) {e.printStackTrace();zipOut.close();fos.close();} finally {try {fis.close();} catch (IOException e) {}}} else if (ff.isDirectory()) {for (File f : ff.listFiles()) {if (f.isDirectory())copysourcepath.add(f.getPath());else if (f.isFile()) {FileInputStream fis = null;try {fis = new FileInputStream(f);ZipEntry entry = new ZipEntry(f.getAbsoluteFile().toString().substring(parentDirectoryLen));zipOut.putNextEntry(entry);int nNumber;Long len=Long.MIN_VALUE;byte[] buffer = new byte[len.intValue()];while ((nNumber = fis.read(buffer)) != -1)System.out.println(ff.getName());zipOut.write(buffer, 0, nNumber);} catch (IOException e) {e.printStackTrace();zipOut.close();fos.close();} finally {try {fis.close();} catch (IOException e) {}}}}}}copysourcepath.removeFirst();}}}try {zipOut.flush();} catch (IOException e) {e.printStackTrace();} finally {try {zipOut.close();fos.close();} catch (IOException e) {}}}
}

Java实现文件夹打包相关推荐

  1. java 把文件打包成zip文件_java将文件或是文件夹打包压缩成zip格式

    导读热词 下面是编程之家 jb51.cc 通过网络收集整理的代码片段. 编程之家小编现在分享给大家,也给大家做个参考. import java.io.BufferedInputStream; impo ...

  2. 把文件或者文件夹打包成jar

    手动打包可以给任何文件夹打包,但是打包的文件夹中必须存在MANIFEST.MF文件,一般是在文件夹中有一个META-INF文件夹,在该文件夹中放MANIFEST.MF文件,如果没有,你可以创建META ...

  3. 一个java删除文件夹的小方法

    java删除文件夹都是从里向外删除,使用递归的方法. public class IO_FILEdemo09 {public static void main(String[] args) {// TO ...

  4. php将文件夹打包zip文件,php将文件夹打包成zip文件

    php将文件夹打包成zip文件:function addFileToZip($path,$zip){ $handler=opendir($path); //打开当前文件夹由$path指定. while ...

  5. java 创建文件夹的方法_Java创建文件夹的方法

    Java创建文件夹的方法 /** * 用于创建文件夹的方法 * @param mkdirName */ public void mkdir(String mkdirName) { try { File ...

  6. linux文件夹打包命令

    linux文件夹打包命令 .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) --------- ...

  7. java 创建文件夹的方法_java中创建文件夹的方法

    java中创建文件夹的方法 发布时间:2020-06-10 11:46:49 来源:亿速云 阅读:461 作者:Leah 这篇文章给大家分享的是java中创建文件夹的方法.小编觉得挺实用的,因此分享给 ...

  8. 解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法

    2019独角兽企业重金招聘Python工程师标准>>> 建立好一个Maven项目后,如果Java Resources资源文件下没有src/main/java文件夹,并且在手动创建这个 ...

  9. Java判断文件夹是否存在,不存在则新建

    1.Java判断是否存在文件夹,不存在则新建 File file = new File("D:/test/filetest/test.txt"); if (!file.getPar ...

最新文章

  1. Set和存储顺序深入探讨、SortedSet排序的示例
  2. snort完整安装(snort-2.8.3.1)
  3. 《剑指offer》c++版本 5.替换空格
  4. Android应用开发的一些规则
  5. 语音怎么进入滤波器matlab,基于Matlab的语音信号滤波器的设计与实现
  6. meta http-equiv=refresh content=3 什么意思?
  7. neo4j图形界面_图形处理:betweeness中心性– neo4j的密码与graphstream
  8. c#winform演练 ktv项目 MediaPlayer控件播放音乐
  9. python Supervisor
  10. 指定精确度(*号的使用)
  11. Internet Explorer 8 Beta1 开始测试,且含简体中文版
  12. TensorFlow基础笔记(6) 图像风格化实验
  13. 一、图解Java中String不可变性
  14. 如何成为一个网红照骗?一个插件搞定,亲妈都认不出!
  15. python模糊匹配_python 字符串模糊匹配 Fuzzywuzzy
  16. 太神了-图片可以转换成Word文档了
  17. 微信小程序 后端接口(thinkphp)
  18. python设置excel套打_你不一定知道这个用 Python 快速设置 Excel 表格边框的技巧
  19. C++ Learning (Next)
  20. C++实现超简单的文件加密

热门文章

  1. python 命名空间报错_python命名空间与作用域
  2. web端功能自动化定位元素(暂不更新)
  3. 2017 3月21日,下午
  4. SpringMVC 拦截器实现
  5. 【转】Cache Buffer Chain 第二篇
  6. “Could not change executable permissions on the application”的原因和解决方法
  7. 存储专家论IP存储现实可行性
  8. 调整窗口大小时进行页面刷新(设定定时器)
  9. 卷影副本--给你后悔的机会,文件误删除,误更改,能够找到以前的版本。
  10. erlang的epmd指定端口范围验证及端口权限配置