ZipUtils 压缩工具包

package io;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * ZipUtils 压缩文件帮助类
 * Created by heqianqian on 2017/4/14.
 */
public class ZipUtils {

    /**
     * 压缩单个文件
     *
     * @param source 源文件
     * @param dest   目的文件
     */
    public static void zipFile(File source, File dest) {
        InputStream inputStream = null;
        ZipOutputStream zipOutputStream = null;
        try {
            inputStream = new FileInputStream(source);
            zipOutputStream = new ZipOutputStream(new FileOutputStream(dest));
            zipOutputStream.putNextEntry(new ZipEntry(source.getName()));
            byte b[] = new byte[1024];
            while (inputStream.read(b) != -1) {
                zipOutputStream.write(b);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                zipOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 压缩多个文件
     *
     * @param dir  源文件目录
     * @param dest 目的文件
     */
    public static void zipFiles(File dir, File dest) {
        if (dir.isDirectory()) {
            zipFiles(dir.listFiles(), dest);
        }
    }

    /**
     * 压缩多个文件
     *
     * @param files 源文件数组
     * @param dest  目的文件
     */
    public static void zipFiles(File[] files, File dest) {
        InputStream inputStream = null;
        ZipOutputStream zipOutputStream = null;
        try {
            zipOutputStream = new ZipOutputStream(new FileOutputStream(dest));
            for (File f : files) {
                inputStream = new FileInputStream(f);
                zipOutputStream.putNextEntry(new ZipEntry(f.getName()));
                byte b[] = new byte[1024];
                while (inputStream.read(b) != -1) {
                    zipOutputStream.write(b);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                zipOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 解压多个文件
     *
     * @param file 待解压文件
     */
    public static void unzipFiles(File file) {
        String path = file.getParent();
        File outFile = null;
        InputStream inputStream = null;
        OutputStream outputStream = null;
        ZipInputStream zipInputStream = null;
        ZipEntry entry;
        try {
            ZipFile zipFile = new ZipFile(file);
            zipInputStream = new ZipInputStream(new FileInputStream(file));
            while ((entry = zipInputStream.getNextEntry()) != null) {
                outFile = new File(path + File.separator + entry.getName());
                if (!outFile.getParentFile().exists()) {
                    outFile.getParentFile().mkdir();
                }
                if (!outFile.exists()) {
                    outFile.createNewFile();
                }
                inputStream = zipFile.getInputStream(entry);
                outputStream = new FileOutputStream(outFile);
                int temp = 0;
                while ((temp = inputStream.read()) != -1) {
                    outputStream.write(temp);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                outputStream.close();
                zipInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值