文件压缩工具

package test;


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.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import org.apache.log4j.Logger;


/**
* 功能描述:ZIP压缩与解压工具类
*/
public class ZipUtil {
private static final Logger log = Logger.getLogger(ZipUtil.class);

/**
* 方法用途和描述: 解压ZIP文件
*
* @param zipFilePath
* 要解压的ZIP文件路径
* @param unZipDir
* 要解压到目标文件夹路径
*/
@SuppressWarnings("unchecked")
public static boolean unZip(String zipFilePath, String unZipDir) {

ZipFile zfile = null;
OutputStream os = null;
InputStream is = null;

try {
zfile = new ZipFile(zipFilePath);
log.debug(zfile.getName());
Enumeration zList = zfile.entries();
ZipEntry ze = null;
byte[] buf = new byte[1024];
String path = null;
while (zList.hasMoreElements()) {
// 从ZipFile中得到一个ZipEntry
ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory()) {
log.debug("Dir: " + ze.getName() + " skipped..");
continue;
}
log.debug("Extracting: " + ze.getName() + "\t" + ze.getSize()
+ "\t" + ze.getCompressedSize());
// 以ZipEntry为参数得到一个InputStream,并写到OutputStream中
path = unZipDir.concat("/").concat(ze.getName());
FileUtil.checkFileDirectoryExists(path);
os = new BufferedOutputStream(new FileOutputStream(path));
is = new BufferedInputStream(zfile.getInputStream(ze));
int readLen = 0;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
}
os.flush();
log.debug("Extracted: " + ze.getName());
ze = null;
}
zList = null;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (zfile != null) {
try {
zfile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}

/**
*
* 方法用途和描述: 压缩文件
* @param sourceFilePath 需要压缩的文件路径
* @param targetFilePath 压缩后放置的文件路径,带.zip
* @param bufferSize 缓冲长度
*/
public static void zip(String sourceFilePath,String targetFilePath,int bufferSize){
FileOutputStream dest = null;
ZipOutputStream out = null;
try {
BufferedInputStream origin = null;
dest = new FileOutputStream(targetFilePath);
out = new ZipOutputStream(new BufferedOutputStream(
dest));
byte data[] = new byte[bufferSize];
File f = new File(sourceFilePath);
File files[] = f.listFiles();

for (int i = 0; i < files.length; i++) {
FileInputStream fi = new FileInputStream(files[i]);
origin = new BufferedInputStream(fi, bufferSize);
ZipEntry entry = new ZipEntry(files[i].getName());
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, bufferSize)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dest != null) {
try {
dest.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public static void main(String argv[]) {
zip("E:\\htmltext\\text","E:\\myfiles.zip",2048);
}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值