一、前言
本篇博客记录一下用 java 实现文件夹压缩的功能,大家可根据自己的需求封装成工具类来使用。传入指定的文件夹路径,可在同级目录下面生成一个压缩后的文件,还可以指定压缩文件的根目录。
二、效果图
三、实现代码测试类
package top.zywork.test;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 将指定文件夹下的所有文件压缩
*/
public class ZipFilesUtils {
public static void compress(File f, String baseDir, ZipOutputStream zos){
if(!f.exists()){
System.out.println("待压缩的文件目录或文件"+f.getName()+"不存在");
return;
}
File[] fs = f.listFiles();
BufferedInputStream bis = null;
//ZipOutputStream zos = null;
byte[] bufs = new byte[1024*10];
FileInputStream fis = null;
try{
//zos = new ZipOutputStream(new FileOutputStream(zipFile));
for(int i=0; i<fs.length; i++){
String fName = fs[i].getName();
System.out.println("压缩:" + baseDir+fName);
if(fs[i].isFile()){
ZipEntry zipEntry = new ZipEntry(baseDir+fName);//
zos.putNextEntry(zipEntry);
//读取待压缩的文件并写进压缩包里
fis = new FileInputStream(fs[i]);
bis = new BufferedInputStream(fis, 1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10))!=-1){
zos.write(bufs, 0, read);
}
//如果需要删除源文件,则需要执行下面2句
//fis.close();
//fs[i].delete();
}
else if(fs[i].isDirectory()){
compress(fs[i], baseDir+fName+"/", zos);
}
}//end for
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
//关闭流
try {
if(null!=bis)
bis.close();
//if(null!=zos)
//zos.close();
if(null!=fis)
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main( String[] args ) throws ParseException
{
String sourceFilePath = "C:\\Users\\Wangchenchen\\Desktop\\test\\Release";
File sourceDir = new File(sourceFilePath);
File zipFile = new File(sourceFilePath+".zip");
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new FileOutputStream(zipFile));
String baseDir = "";
compress(sourceDir, baseDir, zos);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if(zos!=null)
try {
zos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
四、封装工具类代码
将上面的测试代码稍微封装一下,封装成工具类来调用。代码如下:
package top.zywork.common;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileUtils {
/**
* 压缩文件
* @param f 原文件
* @param baseDir 打包根目录
* @param zos 压缩文件
* @param deleteSourceFile 是否需要删除原文件,例如:true=删除原文件、false=不删除原文件
*/
public static void compress(File f, String baseDir, ZipOutputStream zos, boolean deleteSourceFile) {
if(!f.exists()){
System.out.println("待压缩的文件目录或文件"+f.getName()+"不存在");
return;
}
File[] fs = f.listFiles();
BufferedInputStream bis = null;
//ZipOutputStream zos = null;
byte[] bufs = new byte[1024*10];
FileInputStream fis = null;
try{
//zos = new ZipOutputStream(new FileOutputStream(zipFile));
for(int i=0; i<fs.length; i++){
String fName = fs[i].getName();
System.out.println("压缩:" + baseDir+fName);
if(fs[i].isFile()){
ZipEntry zipEntry = new ZipEntry(baseDir+fName);//
zos.putNextEntry(zipEntry);
//读取待压缩的文件并写进压缩包里
fis = new FileInputStream(fs[i]);
bis = new BufferedInputStream(fis, 1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10))!=-1){
zos.write(bufs, 0, read);
}
//如果需要删除源文件,则需要执行下面2句
if (deleteSourceFile) {
fis.close();
fs[i].delete();
}
}
else if(fs[i].isDirectory()){
compress(fs[i], baseDir+fName+"/", zos, false);
}
}//end for
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
//关闭流
try {
if (null!=bis) {
bis.close();
}
//if(null!=zos)
//zos.close();
if (null!=fis) {
fis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 打包指定路径的文件夹
* @param sourceFilePath 源文件加地址,例如:C:\\test 必填
* @param baseDir 打包的根目录名,例如:download/ 非必填
* @param deleteSourceFile 是否需要删除原文件,例如:true=删除原文件、false=不删除原文件 必填
*/
public static void compress(String sourceFilePath, String baseDir, boolean deleteSourceFile) {
File sourceDir = new File(sourceFilePath);
File zipFile = new File(sourceFilePath+".zip");
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new FileOutputStream(zipFile));
compress(sourceDir, baseDir, zos, deleteSourceFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if (zos!=null) {
try {
zos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
五、结尾
博主码字不易,如果你觉得本文对你有用,不妨点个赞再走吧。
更多精彩好文请移步:http://wjhsmart.vip
如果需要友联的朋友可联系博主互联哦