关于压缩一个文件或是解压一个文件的处理

本文介绍了一个Java实现的文件压缩与解压工具类,支持单文件、多文件及文件夹的压缩,并能解压Zip格式文件。该工具利用了Java标准库中的ZipInputStream和ZipOutputStream等类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 /*
 * @(#)ZipTool.java
 * Copyright (c) 2013-2014 ZhongShiAn
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
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 android.util.Log;

/**
 * 文件压缩处理公共类
 * 
 * @version 1.0
 */
public class ZipTool {
	private static final String LOG_TAG = ZipTool.class
			.getSimpleName();
	/** 字节流缓冲区 */
    private static final int BUFFER = 2048;
    /** 压缩级别  */
    private int level = 0;

    /**
     * 构造函数
     */
    public ZipTool() {
    }

    /**
     * 压缩级别设定
     * 
     * @param level 压缩级别
     */
    public void setLevel(int level) {
        this.level = level;

    }

    /**
     * 压缩一个文件或者一个文件夹
     * 
     * @param inputFile 要压缩的文件
     * @param outputFile 压缩后的文件
     * @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());
        }
    }

    /**
     * 压缩一组文件
     * 
     * @param inputFiles 待压缩的文件
     * @param outputFile 压缩后的文件
     * @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++) {
            	Log.i(LOG_TAG, "第"+i+"个  /"+inputFiles.length+ inputFiles[i].getName()+"  "+inputFiles[i].getTotalSpace());
                zip(out, inputFiles[i], inputFiles[i].getName());
            }
            out.close();
        }
        catch (IOException ex) {
            throw new ZipException(ex.getMessage());
        }
    }

    /**
     * 解压文件
     *
     * @param inputFile 待解压的文件
     * @param outputFile 解压后的文件
     * @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();

            if (!outputFile.exists()) {
            	outputFile.mkdirs();
            }
            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());
        }

    }

    /**
     * 文件压缩处理
     * 
     * @param out 压缩输出流
     * @param zipFile 待压缩文件
     * @param zipName 压缩后文件名
     * @throws ZipException 异常
     */
    private void zip(ZipOutputStream out, File zipFile, String zipName) throws
         ZipException {
        if (level != 0) {
            out.setLevel(level);
        }
        if (zipFile.isDirectory()) {
            zipDirectory(out, zipFile, zipName);
        }
        else {
            if (null==zipName || "".equals(zipName)) {
            	zipName = zipFile.getName();
            }
            zipLeapFile(out, zipFile, zipName);
        }

    }

    /**
     * 目录压缩处理
     * 
     * @param out 压缩输出流
     * @param zipFile 待压缩文件
     * @param zipName 压缩后文件名
     * @throws ZipException 异常
     */
    private void zipDirectory(ZipOutputStream out, File zipFile, String zipName) throws
         ZipException {
        File[] files = zipFile.listFiles();
        if (level != 0) {
            out.setLevel(level);
        }
        try {
            out.putNextEntry(new ZipEntry(zipName + "/"));
        }
        catch (IOException ex) {
            throw new ZipException(ex.getMessage());
        }
        if (null==zipName || "".equals(zipName)) {
        	zipName = new String();
        }
        else {
        	zipName = zipName + "/";
        }

        for (int i = 0; i < files.length; i++) {
            zip(out, files[i], zipName + files[i].getName());
        }

    }

    /**
     * 单文件压缩处理
     * 
     * @param out 压缩输出流
     * @param zipFile 待压缩文件
     * @param zipName 压缩后文件名
     * @throws ZipException 异常
     */
    private void zipLeapFile(ZipOutputStream out, File zipFile, String zipName) throws
         ZipException {
        if (level != 0) {
            out.setLevel(level);
        }
        try {
            out.putNextEntry(new ZipEntry(zipName));
            FileInputStream in = new FileInputStream(zipFile);
            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());
        }
    }
    
    public static void main(String[] args) throws ZipException {
    	ZipTool tool = new ZipTool();
    	String zipfile = "d:\\test-zip.csv";
    	String zip = "d:\\test-zip.zip";
    	
    	File inputFile = new File(zipfile);
    	File outputFile = new File(zip);
//    	
    	tool.zipFile(inputFile, outputFile);
//    	
//    	zipfile = "D:\\test_1";
//    	zip = "D:\\test_f.zip";
//    	inputFile = new File(zipfile);
//    	outputFile = new File(zip);
//    	tool.zipFile(inputFile, outputFile);
    	
    	zipfile = "D:\\test_1\\abc";
//    	zip = "D:\\test_f.zip";
    	inputFile = new File(zip);
    	outputFile = new File(zipfile);
    	tool.unZipFile(inputFile, outputFile);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值