初入新公司,领导让我对上传文件解析进行调优,部分都已经进行调优,唯独这个解压压缩包的方法没有调整,便自己弄个方法进行比较。
公司使用的:
/*
* @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