压缩和解压

使用下面的工具类

压缩方法:

doZip(String theFile, String toFile);

解压方法:

unZip(String thefile, String toFile)



工具类:

public class ZipUtil {
private ZipFile         zipFile; 
    private ZipOutputStream zipOut;     //压缩Zip 
    private  int       bufSize;    //size of bytes 
    private byte[]          buf; 
    private int             readedBytes; 
    public ZipUtil(){ 
        this(512); 
    } 


    public ZipUtil(int bufSize){ 
        this.bufSize = bufSize; 
        this.buf = new byte[this.bufSize]; 
    } 
     
/**

* @param srcFile  需要 压缩的目录或者文件
* @param destFile 压缩文件的路径
*/
public void doZip(String srcFile, String destFile) {// zipDirectoryPath:需要压缩的文件夹名
File zipDir;
String dirName;


zipDir = new File(srcFile);
dirName = zipDir.getName();
try {
this.zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(destFile)));
//设置压缩的注释
zipOut.setComment("comment");
//设置压缩的编码,如果要压缩的路径中有中文,就用下面的编码
zipOut.setEncoding("GBK");
//启用压缩 
zipOut.setMethod(ZipOutputStream.DEFLATED); 


//压缩级别为最强压缩,但时间要花得多一点 
zipOut.setLevel(Deflater.BEST_COMPRESSION); 

handleDir(zipDir, this.zipOut,dirName);
this.zipOut.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}


/**
*  由doZip调用,递归完成目录文件读取
* @param dir
* @param zipOut
* @param dirName  这个主要是用来记录压缩文件的一个目录层次结构的
* @throws IOException
*/
private void handleDir(File dir, ZipOutputStream zipOut,String dirName) throws IOException {
System.out.println("遍历目录:"+dir.getName());
FileInputStream fileIn;
File[] files;


files = dir.listFiles();


if (files.length == 0) {// 如果目录为空,则单独创建之.
// ZipEntry的isDirectory()方法中,目录以"/"结尾.
System.out.println("压缩的 Name:"+dirName);
this.zipOut.putNextEntry(new ZipEntry(dirName));
this.zipOut.closeEntry();
} else {// 如果目录不为空,则分别处理目录和文件.
for (File fileName : files) {
// System.out.println(fileName);


if (fileName.isDirectory()) {
handleDir(fileName, this.zipOut,dirName+File.separator+fileName.getName()+File.separator);
} else {
System.out.println("压缩的 Name:"+dirName + File.separator+fileName.getName());
fileIn = new FileInputStream(fileName);
this.zipOut.putNextEntry(new ZipEntry(dirName + File.separator+fileName.getName()));


while ((this.readedBytes = fileIn.read(this.buf)) > 0) {
this.zipOut.write(this.buf, 0, this.readedBytes);
}


this.zipOut.closeEntry();
}
}
}
}


/**
* 解压指定zip文件
* @param unZipfile 压缩文件的路径
* @param destFile   解压到的目录 
*/
public void unZip(String unZipfile, String destFile) {// unZipfileName需要解压的zip文件名
FileOutputStream fileOut;
File file;
InputStream inputStream;


try {
this.zipFile = new ZipFile(unZipfile);


for (Enumeration entries = this.zipFile.getEntries(); entries
.hasMoreElements();) {
ZipEntry entry = (ZipEntry) entries.nextElement();
file = new File(destFile+File.separator+entry.getName());


if (entry.isDirectory()) {
file.mkdirs();
} else {
// 如果指定文件的目录不存在,则创建之.
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}


inputStream = zipFile.getInputStream(entry);


fileOut = new FileOutputStream(file);
while ((this.readedBytes = inputStream.read(this.buf)) > 0) {
fileOut.write(this.buf, 0, this.readedBytes);
}
fileOut.close();


inputStream.close();
}
}
this.zipFile.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}


// 设置缓冲区大小
public void setBufSize(int bufSize) {
this.bufSize = bufSize;
}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值