java代码压缩文件_使用Java代码压缩文件或文件夹

/**

文件注释:ReportUtils.java

*

作者:何伟坡

*

时间:2014年4月17日-下午12:06:31

*

类型:文件-ReportUtils.java

*

用途:该文件用于

*

备注:***

*/

package report.util;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipOutputStream;

/**

类型注释:

*

作者:何伟坡

*

时间:2014年4月17日-下午12:06:31

*

类型:ReportUtils

*

用途:该类型用于

*

备注:***

*/

public class ReportUtils {

/**

*

函数注释:zip()

*

作者:何伟坡

*

时间:2014年4月17日-下午2:38:16

*

类型:方法

*

用途:压缩文件或者文件夹

*

备注:***

*/

public static File zip(File srcFile){

File destFile = null;

if(srcFile != null){

destFile = new File(srcFile.getParent()+"\\"+srcFile.getName().split("\\.")[0]+".zip");

if(srcFile.isDirectory()){

zipFolder(srcFile,destFile);

}else{

zipFile(srcFile,destFile);

}

}

return destFile;

}

/**

*

函数注释:zipProcess()

*

作者:何伟坡

*

时间:2014年4月17日-下午2:38:36

*

类型:方法

*

用途:该方法用于压缩文件夹的递归处理

*

备注:***

*/

public static void zipProcess(ZipOutputStream out, File file, String base) throws Exception {

if (file.isDirectory()) {

File[] fl = file.listFiles();

out.putNextEntry(new ZipEntry(base + "/"));

base = base.length() == 0 ? "" : base + "/";

for (int i = 0; i < fl.length; i++) {

zipProcess(out, fl[i], base + fl[i].getName());

}

}else {

out.putNextEntry(new ZipEntry(base));

FileInputStream in = new FileInputStream(file);

int b;

while ( (b = in.read()) != -1) {

out.write(b);

}

in.close();

}

}

/**

*

函数注释:zipFoler()

*

作者:何伟坡

*

时间:2014年4月17日-下午2:06:20

*

类型:方法

*

用途:该方法用于压缩文件夹

*

备注:***

*/

public static void zipFolder(File srcFile, File destFile){

ZipOutputStream out = null;

try {

out = new ZipOutputStream(new FileOutputStream(destFile));

out.setLevel(9);

out.setEncoding("GBK");// 指定编码为gbk,否则部署到linux下会出现乱码

zipProcess(out, srcFile, "");

}catch (Exception e) {

e.printStackTrace();

}finally{

try {

if(out != null){

out.close() ;

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

*

函数注释:zipFile()

*

作者:何伟坡

*

时间:2014年4月17日-下午2:03:41

*

类型:方法

*

用途:该方法用于压缩文件

*

备注:***

*/

public static void zipFile(File srcFile, File destFile){

ZipOutputStream out = null;

FileInputStream in = null;

try {

out = new ZipOutputStream(new FileOutputStream(destFile));

out.setLevel(9);

out.setEncoding("GBK");// 指定编码为gbk,否则部署到linux下会出现乱码

in = new FileInputStream(srcFile);

out.putNextEntry(new ZipEntry(srcFile.getName()));

int length;

byte[] buffer = new byte[1024];

while ((length = in.read(buffer,0,1024)) != -1){

out.write(buffer, 0, length);

}

} catch (Exception e) {

e.printStackTrace();

}finally{

try {

if(in != null){

in.close();

}

if(out != null){

out.close() ;

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值