首页>代码>Java解压缩文件,尤其对于Rar5.0版本的解压实现>/decompress/src/main/java/com/hgq/util/ZipUtil.java
package com.hgq.util;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.utils.IOUtils;

import java.io.*;
import java.util.Enumeration;

/**
 * TODO 类的作用信息
 *
 * @author huguangquan
 * @date 22-9-16 016
 */
public class ZipUtil {
    public static final int BUFFER_SIZE = 2048;

    public static void unZip(File file, String destDir) throws IOException {
        // windows设置GBK编码,中文乱码问题
        ZipFile zipFile = new ZipFile(file, "GBK");
        byte[] buffer = new byte[BUFFER_SIZE];
        ZipArchiveEntry entry;
        // 获取全部文件的迭代器,循环解压所有文件
        Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
        InputStream inputStream;
        while (entries.hasMoreElements()) {
            entry = entries.nextElement();
            if (entry.isDirectory()) {
                continue;
            }

            File outputFile = new File(destDir + entry.getName());

            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }

            inputStream = zipFile.getInputStream(entry);
            try (FileOutputStream fos = new FileOutputStream(outputFile)) {
                while (inputStream.read(buffer) > 0) {
                    fos.write(buffer);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        zipFile.close();
    }

    /**
     * 解压 zip 文件
     *
     * @param zipFileInputStream zip压缩文件输入流
     * @param destDir            zip 压缩文件解压后保存的目录
     * @return 返回 zip 压缩文件里的文件名的 list
     * @throws Exception
     */
    public static void unZip(InputStream zipFileInputStream, String destDir) throws Exception {
        ZipArchiveInputStream is = null;
        try {
            is = new ZipArchiveInputStream(zipFileInputStream, "GBK", true);
            ZipArchiveEntry entry = null;

            while ((entry = is.getNextZipEntry()) != null) {
                if (entry.isDirectory()) {
                    File directory = new File(destDir, entry.getName());
                    directory.mkdirs();
                } else {
                    OutputStream os = null;
                    try {
                        // 解压的时候如果是无.dcm后缀的dicom文件,默认给文件加上.dcm后缀
                        os = new BufferedOutputStream(new FileOutputStream(new File(destDir, entry.getName())), BUFFER_SIZE);
                        IOUtils.copy(is, os);
                    } finally {
                        IOUtils.closeQuietly(os);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
}
最近下载更多
Seaskye  LV14 2023年11月10日
小幸运xiao  LV1 2023年11月3日
逐风796  LV2 2023年8月17日
wangguojie  LV1 2023年5月16日
最代码官方  LV167 2022年9月18日
最近浏览更多
cnng007 2023年11月21日
暂无贡献等级
Seaskye  LV14 2023年11月10日
小幸运xiao  LV1 2023年11月3日
graceful 2023年10月18日
暂无贡献等级
我是恁爹记住 2023年9月27日
暂无贡献等级
二进制2  LV3 2023年9月12日
逐风796  LV2 2023年8月17日
zw5097  LV23 2023年7月15日
sanye333  LV1 2023年6月13日
wangguojie  LV1 2023年5月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友