栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

ZipUtils

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ZipUtils

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;

    

    public static void zipFiles(Collection 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();

    }

    

    public static void zipFiles(Collection 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();

    }

    

    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();

        }

    }

    

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

        upZipFile(zipFile, folderPath, null);

    }

    

    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();

    }

    

    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();

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/271881.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号