java实用ZIP压缩和解压类

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class Zip {

    private ZipInputStream zipIn; // 解压Zip

    private ZipOutputStream zipOut; // 压缩Zip

    private ZipEntry zipEntry;

    private int bufSize; // size of bytes

    private byte[] buf;

    private int readedBytes;

    public Zip() {
	this(512);
    }

    public Zip(int bufSize) {

	this.bufSize = bufSize;

	this.buf = new byte[this.bufSize];

    }

    // 压缩文件夹内的文件
    public void doZip(String zipDirectory) throws IOException {// zipDirectoryPath:需要压缩的文件夹名
	File zipDir = null;
	zipDir = new File(zipDirectory);
	String filePath = zipDir.getAbsolutePath();
	String zipFileName = (filePath.indexOf(".") == -1 ? filePath : filePath
		.substring(0, filePath.lastIndexOf("."))) + ".zip";// 压缩后生成的zip文件名
	File zipFile = new File(zipFileName);

	if (zipFile.exists()) {
	    zipFile.delete();
	} else {
	    zipFile.createNewFile();
	}
	this.zipOut = new ZipOutputStream(new BufferedOutputStream(
		new FileOutputStream(zipFile)));
	if (zipDir.isDirectory())
	    handleDir(zipDir, this.zipOut);
	else
	    handleFile(zipDir, this.zipOut);

	this.zipOut.close();

    }

    private void handleFile(File file, ZipOutputStream zipOut)
	    throws IOException {
	FileInputStream fileIn = new FileInputStream(file);

	this.zipOut.putNextEntry(new ZipEntry(file.getName()));

	while ((this.readedBytes = fileIn.read(this.buf)) > 0) {

	    this.zipOut.write(this.buf, 0, this.readedBytes);

	}
	this.zipOut.closeEntry();
    }

    // 由doZip调用,递归完成目录文件读取

    private void handleDir(File dir, ZipOutputStream zipOut) throws IOException {
	File[] files;
	files = dir.listFiles();
	for (File fileName : files) {
	    // System.out.println(fileName);
	    if (fileName.isDirectory()) {
		handleDir(fileName, this.zipOut);
	    } else {
		handleFile(fileName, this.zipOut);
	    }
	}

    }

    public void unZip(String unZipfileName) {
	try {
	    unZip(unZipfileName,
		    unZipfileName.substring(0, unZipfileName.indexOf(".zip")));
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }

    // 解压指定zip文件
    public void unZip(String unZipfileName, String descFile) throws IOException {// unZipfileName需要解压的zip文件名
	this.zipIn = new ZipInputStream(new FileInputStream(unZipfileName));
	this.zipEntry = this.zipIn.getNextEntry();
	while (this.zipEntry != null) {
	    File f = new File(descFile);
	    f.mkdir();
	    if (this.zipEntry.isDirectory()) {
		String name = this.zipEntry.getName();
		name = name.substring(0, name.length() - 1);
		f = new File(descFile + File.separator + name);
		f.mkdir();
	    } else {
		f = new File(descFile + File.separator
			+ this.zipEntry.getName());
		f.createNewFile();
		FileOutputStream out = new FileOutputStream(f);
		int b;
		while ((b = this.zipIn.read()) != -1) {
		    out.write(b);
		}
		out.close();
	    }
	    // 读取下一个ZipEntry
	    this.zipEntry = this.zipIn.getNextEntry();
	}
	this.zipIn.close();
    }

    // 设置缓冲区大小

    public void setBufSize(int bufSize) {
	this.bufSize = bufSize;
    }

    // 测试Zip类

    // public static void main(String[] args) throws Exception {
    // Zip zip = new Zip();
    // zip.unZip("D:\\temp\\upLoadDir.zip");
    //
    // }
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值