两种解压压缩包方法的对比

初入新公司,领导让我对上传文件解析进行调优,部分都已经进行调优,唯独这个解压压缩包的方法没有调整,便自己弄个方法进行比较。

公司使用的:

/*
     * @param srcZipFile 需解压的文件名
     *
     * @return 如果解压成功返回true
     */
    private List<String> unzip(String fullpath) {
        logger.info("unzip:" + fullpath);
        List<String> extractFileList = new ArrayList<String>();
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fullpath));
            ZipInputStream zis = new ZipInputStream(bis);

            BufferedOutputStream bos = null;

            ZipEntry entry = null;
            while ((entry = zis.getNextEntry()) != null) {
                String entryName = entry.getName();

                logger.debug("解压文件:" + entryName);
                if (!entryName.contains(".json")) {
                    continue;
                }

                String tempPath = FileUtil.getPath(fullpath) + FileUtil.SYSTEM_SEPARATOR + FileUtil.getFilenameWithoutSuffix(fullpath);
                File sysTempPathDir = new File(tempPath);
                if (!sysTempPathDir.exists()) {
                    sysTempPathDir.mkdirs();
                }
                extractFileList.add(tempPath + FileUtil.SYSTEM_SEPARATOR + entryName);
                System.out.println("filePath====================================" + tempPath + FileUtil.SYSTEM_SEPARATOR + entryName);
                bos = new BufferedOutputStream(new FileOutputStream(tempPath + FileUtil.SYSTEM_SEPARATOR + entryName));
                int b = 0;
                while ((b = zis.read()) != -1) {
                    bos.write(b);
                }
                bos.flush();
                bos.close();
            }
            zis.close();
        } catch (IOException e) {

            e.printStackTrace();
            return null;
        }
        return extractFileList;
    }

我自己百度的方法:

package com.ultrarmor.sptsm.presentation.web.manager.util;

import com.ultrarmor.ntcard.util.FileUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @Author: Mr.wang
 * @Date: 2019/7/2 17:41
 * @Version 1.0
 * @项目名称 sptsm-parent
 * @包名:com.ultrarmor.sptsm.presentation.web.manager.util QQ:1452561187
 **/
public class UnzipUtil {
    /**
     * Size of the buffer to read/write data
     */
    private static final int BUFFER_SIZE = 4096;

    /**
     * Extracts a zip file specified by the zipFilePath to a directory specified
     * by destDirectory (will be created if does not exists)
     *
     * @param zipFilePath
     * @throws IOException
     */
    public static List<String> unzip(String zipFilePath) throws IOException {
        List<String> extractFileList = new ArrayList<String>();
        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
        ZipEntry entry = zipIn.getNextEntry();
        // iterates over entries in the zip file
        while (entry != null) {
            if (!entry.getName().contains(".json")) {
                continue;
            }
            System.out.println("entry.getName()=======" + entry.getName());
            String tempPath = FileUtil.getPath(zipFilePath) + FileUtil.SYSTEM_SEPARATOR + FileUtil.getFilenameWithoutSuffix(zipFilePath);
            File sysTempPathDir = new File(tempPath);
            if (!sysTempPathDir.exists()) {
                sysTempPathDir.mkdirs();
            }
            extractFileList.add(tempPath + FileUtil.SYSTEM_SEPARATOR + entry.getName());

            if (!entry.isDirectory()) {
                // if the entry is a file, extracts it
                extractFile(zipIn, tempPath + FileUtil.SYSTEM_SEPARATOR + entry.getName());
            } else {
                // if the entry is a directory, make the directory
                File dir = new File(tempPath + FileUtil.SYSTEM_SEPARATOR + entry.getName());
                dir.mkdir();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();
        return extractFileList;
    }

    /**
     * Extracts a zip entry (file entry)
     *
     * @param zipIn
     * @param filePath
     * @throws IOException
     */
    public static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        System.out.println("filePath=========================================" + filePath);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[BUFFER_SIZE];
        int read = 0;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.flush();
        bos.close();
    }
}

测试一下两万条数据的压缩包分别消耗:121s    61s

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值