ZipUtils

25 篇文章 1 订阅
10 篇文章 0 订阅

ZipUtils

import android.util.Log;

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.io.InputStream;

import java.io.OutputStream;

import java.nio.charset.StandardCharsets;

import java.util.Collection;

import java.util.Enumeration;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

import java.util.zip.ZipOutputStream;

public class ZipUtils {

    private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte

    public static String zipName;

    /**

     * 批量压缩文件(夹)

     *

     * @param resFileList 要压缩的文件(夹)列表

     * @param zipFile     生成的压缩文件

     * @throws IOException 当压缩过程出错时抛出

     */

    public static void zipFiles(Collection<File> resFileList, File zipFile)

            throws IOException {

        if (zipFile.exists())

            zipFile.delete();

        ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(

                new FileOutputStream(zipFile), BUFF_SIZE));

        for (File resFile : resFileList) {

            zipFile(resFile, zipOut, "");

        }

        zipOut.close();

    }

    /**

     * 批量压缩文件(夹)

     *

     * @param resFileList 要压缩的文件(夹)列表

     * @param zipFile     生成的压缩文件

     * @param comment     压缩文件的注释

     * @throws IOException 当压缩过程出错时抛出

     */

    public static void zipFiles(Collection<File> resFileList, File zipFile,

                                String comment) throws IOException {

        ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(

                new FileOutputStream(zipFile), BUFF_SIZE));

        for (File resFile : resFileList) {

            zipFile(resFile, zipOut, "");

        }

        zipOut.setComment(comment);

        zipOut.close();

    }

    /**

     * 压缩文件

     *

     * @param resFile  需要压缩的文件(夹)

     * @param zipOut   压缩的目的文件

     * @param rootpath 压缩的文件路径

     * @throws FileNotFoundException 找不到文件时抛出

     * @throws IOException           当压缩过程出错时抛出

     */

    public static void zipFile(File resFile, ZipOutputStream zipOut,

                               String rootpath) throws IOException {

        rootpath = rootpath

                + (rootpath.trim().length() == 0 ? "" : File.separator)

                + resFile.getName();

        rootpath = new String(rootpath.getBytes(), StandardCharsets.UTF_8);

        if (resFile.isDirectory()) {

            File[] fileList = resFile.listFiles();

            for (File file : fileList) {

                zipFile(file, zipOut, rootpath);

            }

        } else {

            byte[] buffer = new byte[BUFF_SIZE];

            BufferedInputStream in = new BufferedInputStream(

                    new FileInputStream(resFile), BUFF_SIZE);

            zipOut.putNextEntry(new ZipEntry(rootpath));

            int realLength;

            while ((realLength = in.read(buffer)) != -1) {

                zipOut.write(buffer, 0, realLength);

            }

            in.close();

            zipOut.flush();

            zipOut.closeEntry();

            // resFile.delete();

        }

    }

    /**

     * upZipFile:(解压缩文件)

     * 将zipFile文件解压到folderPath目录下)

     *

     * @return int DOM对象

     * @throws

     */

    public static void upZipFile(File zipFile, String folderPath) throws IOException {

        upZipFile(zipFile, folderPath, null);

    }

    /**

     * upZipFile:(解压缩文件)

     * 将zipFile文件解压到folderPath目录下)

     *

     * @return int DOM对象

     * @throws

     */

    public static void upZipFile(File zipFile, String folderPath, IAutelUnZipCallback iAutelUnZipCallback) throws IOException {

        ZipFile zFile = new ZipFile(zipFile);

        Enumeration zList = zFile.entries();

        ZipEntry ze;

        byte[] buf = new byte[1024];

        while (zList.hasMoreElements()) {

            ze = (ZipEntry) zList.nextElement();

            if (ze.isDirectory()) {

                String dirstr = folderPath + ze.getName();

                dirstr = new String(dirstr.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);

                File f = new File(dirstr);

                f.mkdir();

                continue;

            }

            Log.d("upZipFile", "ze.getName() = " + ze.getName());

            OutputStream os = new BufferedOutputStream(

                    new FileOutputStream(getRealFileName(folderPath, ze.getName())));

            zipName = ze.getName();

            InputStream is = new BufferedInputStream(zFile.getInputStream(ze));

            int readLen;

            while ((readLen = is.read(buf, 0, 1024)) != -1) {

                os.write(buf, 0, readLen);

            }

            is.close();

            os.close();

        }

        zFile.close();

        if (null != iAutelUnZipCallback)

            iAutelUnZipCallback.unZipSuccess();

    }

    /**

     * getRealFileName:(给定根目录,返回一个相对路径所对应的实际文件名)

     *

     * @param baseDir     指定根目录

     * @param absFileName 相对路径名,来自于ZipEntry中的那么

     * @return java.io.File 实际的文件

     */

    private static File getRealFileName(String baseDir, String absFileName) {

        String[] dirs = absFileName.split("/");

        File ret = new File(baseDir);

        ret.mkdirs();

        String substr;

        if (dirs.length > 0) {

            for (int i = 0; i < dirs.length - 1; i++) {

                substr = dirs[i];

                ret = new File(ret, substr);

            }

            if (!ret.exists()) {

                ret.mkdirs();

            }

            substr = dirs[dirs.length - 1];

            ret = new File(ret, substr);

            return ret;

        }

        return ret;

    }

    public interface IAutelUnZipCallback{

        void unZipSuccess();

    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时代我西

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值