`
龙哥IT
  • 浏览: 237147 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

ZIP文件工具类 ZipUtils

 
阅读更多
/*
 * Copyright (c) 2014,KJFrameForAndroid Open Source Project,张涛.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.oschina.app.util;

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.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * ZIP文件工具类
 * 
 * @author kymjs(kymjs123@gmail.com)
 */
public class ZipUtils {
    private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte

    /**
     * 批量压缩文件(夹)
     * 
     * @param resFileList
     *            要压缩的文件(夹)列表
     * @param zipFile
     *            生成的压缩文件
     * @throws IOException
     *             当压缩过程出错时抛出
     */
    public static void zipFiles(Collection<File> resFileList, File zipFile)
            throws IOException {
        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 zipFile
     *            压缩文件
     * @param folderPath
     *            文件解压到指定目标路径
     * @throws IOException
     *             当解压缩过程出错时抛出
     */
    public static void upZipFile(File zipFile, String folderPath)
            throws ZipException, IOException {
        File desDir = new File(folderPath);
        if (!desDir.exists()) {
            desDir.mkdirs();
        }
        ZipFile zf = new ZipFile(zipFile);
        for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            InputStream in = zf.getInputStream(entry);
            String str = folderPath + File.separator + entry.getName();
            str = new String(str.getBytes("8859_1"), "GB2312");
            File desFile = new File(str);
            if (!desFile.exists()) {
                File fileParentDir = desFile.getParentFile();
                if (!fileParentDir.exists()) {
                    fileParentDir.mkdirs();
                }
                desFile.createNewFile();
            }
            OutputStream out = new FileOutputStream(desFile);
            byte buffer[] = new byte[BUFF_SIZE];
            int realLength;
            while ((realLength = in.read(buffer)) > 0) {
                out.write(buffer, 0, realLength);
            }
            in.close();
            out.close();
        }
    }

    /**
     * 解压文件名包含传入文字的文件
     * 
     * @param zipFile
     *            压缩文件
     * @param folderPath
     *            目标文件夹
     * @param nameContains
     *            传入的文件匹配名
     * @throws ZipException
     *             压缩格式有误时抛出
     * @throws IOException
     *             IO错误时抛出
     */
    public static ArrayList<File> upZipSelectedFile(File zipFile,
            String folderPath, String nameContains) throws ZipException,
            IOException {
        ArrayList<File> fileList = new ArrayList<File>();

        File desDir = new File(folderPath);
        if (!desDir.exists()) {
            desDir.mkdir();
        }

        ZipFile zf = new ZipFile(zipFile);
        for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            if (entry.getName().contains(nameContains)) {
                InputStream in = zf.getInputStream(entry);
                String str = folderPath + File.separator + entry.getName();
                str = new String(str.getBytes("8859_1"), "GB2312");
                // str.getBytes("GB2312"),"8859_1" 输出
                // str.getBytes("8859_1"),"GB2312" 输入
                File desFile = new File(str);
                if (!desFile.exists()) {
                    File fileParentDir = desFile.getParentFile();
                    if (!fileParentDir.exists()) {
                        fileParentDir.mkdirs();
                    }
                    desFile.createNewFile();
                }
                OutputStream out = new FileOutputStream(desFile);
                byte buffer[] = new byte[BUFF_SIZE];
                int realLength;
                while ((realLength = in.read(buffer)) > 0) {
                    out.write(buffer, 0, realLength);
                }
                in.close();
                out.close();
                fileList.add(desFile);
            }
        }
        return fileList;
    }

    /**
     * 获得压缩文件内文件列表
     * 
     * @param zipFile
     *            压缩文件
     * @return 压缩文件内文件名称
     * @throws ZipException
     *             压缩文件格式有误时抛出
     * @throws IOException
     *             当解压缩过程出错时抛出
     */
    public static ArrayList<String> getEntriesNames(File zipFile)
            throws ZipException, IOException {
        ArrayList<String> entryNames = new ArrayList<String>();
        Enumeration<?> entries = getEntriesEnumeration(zipFile);
        while (entries.hasMoreElements()) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            entryNames.add(new String(getEntryName(entry).getBytes("GB2312"),
                    "8859_1"));
        }
        return entryNames;
    }

    /**
     * 获得压缩文件内压缩文件对象以取得其属性
     * 
     * @param zipFile
     *            压缩文件
     * @return 返回一个压缩文件列表
     * @throws ZipException
     *             压缩文件格式有误时抛出
     * @throws IOException
     *             IO操作有误时抛出
     */
    public static Enumeration<?> getEntriesEnumeration(File zipFile)
            throws ZipException, IOException {
        ZipFile zf = new ZipFile(zipFile);
        return zf.entries();

    }

    /**
     * 取得压缩文件对象的注释
     * 
     * @param entry
     *            压缩文件对象
     * @return 压缩文件对象的注释
     * @throws UnsupportedEncodingException
     */
    public static String getEntryComment(ZipEntry entry)
            throws UnsupportedEncodingException {
        return new String(entry.getComment().getBytes("GB2312"), "8859_1");
    }

    /**
     * 取得压缩文件对象的名称
     * 
     * @param entry
     *            压缩文件对象
     * @return 压缩文件对象的名称
     * @throws UnsupportedEncodingException
     */
    public static String getEntryName(ZipEntry entry)
            throws UnsupportedEncodingException {
        return new String(entry.getName().getBytes("GB2312"), "8859_1");
    }

    /**
     * 压缩文件
     * 
     * @param resFile
     *            需要压缩的文件(夹)
     * @param zipout
     *            压缩的目的文件
     * @param rootpath
     *            压缩的文件路径
     * @throws FileNotFoundException
     *             找不到文件时抛出
     * @throws IOException
     *             当压缩过程出错时抛出
     */
    private static void zipFile(File resFile, ZipOutputStream zipout,
            String rootpath) throws FileNotFoundException, IOException {
        rootpath = rootpath
                + (rootpath.trim().length() == 0 ? "" : File.separator)
                + resFile.getName();
        rootpath = new String(rootpath.getBytes("8859_1"), "GB2312");
        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();
        }
    }
}

 

分享到:
评论

相关推荐

    java 中 zip压缩文件解压工具类

    对zip文件进行解压缩工具类,以及jar包

    ZIP格式压缩文件工具类

    ZIP格式压缩文件工具类

    Zip压缩和解压工具类

    该工具支持zip的压缩和解压 1 1

    Zip4j文件工具类

    使用 net.lingala.zip4j包实现相关操作,相对于JDK自带的zip操作类,其不仅支持密码操作,更加简单方便,且不依赖其他类库。

    zip文件压缩解压操作工具类

    zip文件压缩解压操作工具类. 1.递归压缩 2.压缩一个目录 3.压缩一个文件 4.解压文件

    压缩成zip包工具类

    压缩成zip包工具类 压缩成zip包工具类 压缩成zip包工具类

    java文件生成zip格式(ziputils.java)

    java文件生成zip格式(ziputils.java)

    Java实现Zip压缩文件操作的工具类.zip

    Java实现Zip压缩文件操作的工具类 文章介绍:https://blog.csdn.net/rongbo91/article/details/117747042 (可作为Jar依赖包直接使用) 1、项目使用前,请进入rdc-bom目录下,执行mvn clean install命令  2、可...

    封装Zip相关的工具类

    封装了Zip对文件的压缩和解压相关代码,可直接加到自己的本地nuget库中使用

    文件压缩与解压缩zip工具类

    文件压缩与解压缩zip工具类

    Zip文件修复工具

    DataNumen Zip Repair (DZIPR)(原Advanced Zip Repair)是一款功能强大的修复损坏的Zip和自解压文档的工具。它采用先进的技术扫描被损坏的Zip文件,尽可能多的恢复您的数据,以尽量减少损失。 主要功能: 1. 支持所有...

    使用 Java 实现的压缩/解压 ZIP 文件的工具类

    使用 Java 语言实现并封装的创建 ZIP 格式的压缩文件并解压到指定目录和解压 ZIP 文件到指定目录的工具类。

    解析so文件的工具类,很实用的一个工具类.zip

    解析so文件的工具类,很实用的一个工具类.zip

    zip压缩java工具类包含jar文件

    zip压缩以及解压工具类,本人纯手打。其中包括zip所需要的jar 文件,copy到项目中可直接运行,以及工具类添加注释

    FileUploadUtil.java(文件上传工具类)

    文件上传工具类,上传文件,上传导入文件到path文件夹下,判断图片大小是否超过要求,上传多张图片到服务器,上传图片到服务器,上传文件(在指定目录下创建年月日目录),上传导入文件到temp文件夹下,删除指定...

    读取yml文件工具类.zip

    java环境下,大多数都是微服务,一般都会用yml文件作为配置文件,特此,总结了一个读取yml文件的工具类

    ZipUtils.zip

    由于项目的需要使用到文档的压缩和解压功能,期初打算使用zlib,发现其只是提供了底层的接口供使用,主要是用来对文字的压缩...后来发现了zip utils的包,现再其基础上进行修改,目前满足自身对文件解压和压缩的使用。

    php实现的zip文件内容比较类

    本文实例讲述了php实现的zip文件内容比较类。是一个非常实用的PHP类文件。分享给大家供大家参考。 该php zip文件比较类主要实现比较两个zip文件的...这样可以节省资源与时间,因此需要编写一个能够比较zip内文件的类。

    java压缩文件源码--ZipUtils

    public class ZipUtils { private static final int BUFFER = 8192; private static void log(String msg){ System.out.println (msg); } private static String getFileName(String filePath){ int ...

    php真正的ZIP文件操作类.zip

    php真正的ZIP文件操作类,php将文件夹打包成zip文件,分析了php操作zip文件的技巧,有兴趣的朋友可以引用参考,或者朋友们有更好的zip类,可以上传到我们PHP中文网与我们一起学习分享。

Global site tag (gtag.js) - Google Analytics