打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java压缩解压代码
下边是在项目用到的压缩解压class ,粘出来,分享。
 
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.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.apache.commons.lang.StringUtils;
public class ZipTool {
    private static final int BUFFER = 2048;
    private int level;
    public ZipTool() {
        level = 0;
    }
    /**
     * setLevel
     * @param level int
     */
    public void setLevel(int level) {
        this.level = level;
    }
    /**
     * zip a File or Directory
     * @param inputFile File
     * @param outputFile File
     * @throws ZipException
     */
    public void zipFile(File inputFile, File outputFile) throws ZipException {
        try {
            BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(
                outputFile), BUFFER);
            ZipOutputStream out = new ZipOutputStream(bout);
            zip(out, inputFile, inputFile.getName());
            out.close();
        }
        catch (IOException ex) {
            throw new ZipException(ex.getMessage());
        }
    }
    /**
     * zip some Files
     * @param inputFiles File[]
     * @param outputFile File
     * @throws ZipException
     */
    public void zipFiles(File[] inputFiles, File outputFile) throws ZipException {
        try {
            BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(outputFile),
                BUFFER);
            ZipOutputStream out = new ZipOutputStream(bout);
            for (int i = 0; i < inputFiles.length; i++) {
                zip(out, inputFiles[i], inputFiles[i].getName());
            }
            out.close();
        }
        catch (IOException ex) {
            throw new ZipException(ex.getMessage());
        }
    }
    /**
     * unzip a File
     *
     * @param inputFile File
     * @param outputFile File
     * @throws ZipException
     */
    public void unZipFile(File inputFile, File outputFile) throws ZipException {
        try {
            FileInputStream tin = new FileInputStream(inputFile);
            CheckedInputStream cin = new CheckedInputStream(tin, new CRC32());
            BufferedInputStream bufferIn = new BufferedInputStream(cin, BUFFER);
            ZipInputStream in = new ZipInputStream(bufferIn);
            ZipEntry z = in.getNextEntry();
            while (z != null) {
                String name = z.getName();
                if (z.isDirectory()) {
                    File f = new File(outputFile.getPath() + File.separator + name);
                    f.mkdir();
                }
                else {
                    File f = new File(outputFile.getPath() + File.separator + name);
                    f.createNewFile();
                    FileOutputStream out = new FileOutputStream(f);
                    byte data[] = new byte[BUFFER];
                    int b;
                    while ( (b = in.read(data, 0, BUFFER)) != -1) {
                        out.write(data, 0, b);
                    }
                    out.close();
                }
                z = in.getNextEntry();
            }
            in.close();
        }
        catch (IOException ex) {
            throw new ZipException(ex.getMessage());
        }
    }
    private void zip(ZipOutputStream out, File f, String base) throws
        FileNotFoundException, ZipException {
        if (level != 0) {
            out.setLevel(level);
        }
        if (f.isDirectory()) {
            zipDirectory(out, f, base);
        }
        else {
            if (StringUtils.isEmpty(base)) {
                base = f.getName();
            }
            zipLeapFile(out, f, base);
        }
    }
    private void zipDirectory(ZipOutputStream out, File f, String base) throws
        FileNotFoundException, ZipException {
        File[] files = f.listFiles();
        if (level != 0) {
            out.setLevel(level);
        }
        try {
            out.putNextEntry(new ZipEntry(base + "/"));
        }
        catch (IOException ex) {
            throw new ZipException(ex.getMessage());
        }
        if (StringUtils.isEmpty(base)) {
            base = new String();
        }
        else {
            base = base + "/";
        }
        for (int i = 0; i < files.length; i++) {
            zip(out, files[i], base + files[i].getName());
        }
    }
    private void zipLeapFile(ZipOutputStream out, File f, String base) throws
        FileNotFoundException, ZipException {
        if (level != 0) {
            out.setLevel(level);
        }
        try {
            out.putNextEntry(new ZipEntry(base));
            FileInputStream in = new FileInputStream(f);
            BufferedInputStream bufferIn = new BufferedInputStream(in, BUFFER);
            byte[] data = new byte[BUFFER];
            int b;
            while ( (b = bufferIn.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, b);
            }
            bufferIn.close();
        }
        catch (IOException ex) {
            throw new ZipException(ex.getMessage());
        }
    }
}
 
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/chfzhb/archive/2008/10/23/3129558.aspx
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java上传文件跟批量下载文件
网页文档在线浏览(仿百度文库设计)
java实现zip压缩文件
如何将文件打成zip包并下载
实现 Android 应用在开机时自启动
Java 多线程文件拷贝
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服