工具类 打包文件成压缩包文件
package com.myFirstSpring.test;
/**
* @author tqf
* @version 创建时间:2019-9-25 下午1:50:02
* 类说明:文件打包成压缩包
*/
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.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.cloopen.rest.sdk.utils.DateUtil;
public class ZipUtil {
public static void main(String[] args){
String sourceFilePath = "D://Qrcoe/";
String zipFilePath = "D://Qrcoe/";
String fileName = DateUtil.dateToStr(new Date(),"yyyyMMddHHmmss");
String fn = ZipUtil.fileToZip(sourceFilePath, zipFilePath, fileName);
System.out.println(fn);
}
/**
* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
* @param sourceFilePath :待压缩的文件路径
* @param zipFilePath :压缩后存放路径
* @param fileName :压缩后文件的名称
* @return
*/
public static String fileToZip(String sourceFilePath,String zipFilePath,String fileName){
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 +".rar");
//判断压缩后文件是否会重复
if(zipFile.exists()){
System.out.println(zipFilePath + "目录下已存在名字为" + fileName +".rar" +"文件");
}else{
//每次生成压缩包之前先删除原有的压缩包
zipFile.delete();
//获取源文件夹下的所有文件
File[] sourceFiles = sourceFile.listFiles();
if(null == sourceFiles || sourceFiles.length<1){
System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
}else{
//先删除之前的压缩包文件
for(int i=0; i<sourceFiles.length; i++){
if(sourceFiles[i].getName().indexOf(".rar")!=-1){
File zipFile2 = new File(sourceFiles[i].getPath());
zipFile2.delete();
break;
}
}
//重新获取文件夹所以文件
sourceFiles = sourceFile.listFiles();
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);
//读取待压缩的文件并写进压缩包里
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);
}
}
}
}
} 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 fileName + ".rar";
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战