zip解压
package com.cxl.ky.util;
import com.cxl.ky.entry.vo.UnzipFileVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/**
-
@Author Biejt
-
@Date 2022/12/8 19:43
-
@Version 1.0
*/
@Slf4j
public class ZipUtils {// 默认utf8
public static List Ectract(MultipartFile multipartFile, String strChar) throws IOException {
Charset charset;
List list = new ArrayList<>();if ("gbk".equals(strChar.toLowerCase())) { charset = Charset.forName("GBK"); } else { charset = StandardCharsets.UTF_8; } File file = multipartToFile(multipartFile); ZipFile zipFile = new ZipFile(file, charset); Enumeration<? extends ZipEntry> entries = zipFile.entries(); // 定义ZipEntry置为null,避免由于重复调用zipInputStream.getNextEntry造成的不必要的问题 ZipEntry ze = null; while (entries.hasMoreElements()) { ze = entries.nextElement(); InputStream is = zipFile.getInputStream(ze); if (is == null) { log.error("没有更多的文件可读取了"); return list; } UnzipFileVo unzipFileVo = new UnzipFileVo(); unzipFileVo.setFile(getMultipartFile(is, ze.getName())); list.add(unzipFileVo); } zipFile.close(); file.delete(); return list;
}
/**
- 获取封装得MultipartFile
- @param inputStream inputStream
- @param fileName fileName
- @return MultipartFile
*/
public static MultipartFile getMultipartFile(InputStream inputStream, String fileName) {
FileItem fileItem = createFileItem(inputStream, fileName);
// CommonsMultipartFile是feign对multipartFile的封装,但是要FileItem类对象
return new CommonsMultipartFile(fileItem);
}
private static File multipartToFile(MultipartFile multipartFile) throws IOException {
if (multipartFile == null || multipartFile.getSize() <= 0) {
return null;
}
File file = multipartFileToFile(multipartFile);
if (file == null || !file.exists()) {
return null;
}return file;
}
public static File multipartFileToFile(MultipartFile multipartFile) {
File file = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = multipartFile.getInputStream();
file = new File(multipartFile.getOriginalFilename());
outputStream = new FileOutputStream(file);
write(inputStream, outputStream);
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (inputStream != null) {
try {
inputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
return file;
}public static void write(InputStream inputStream, OutputStream outputStream) {
byte[] buffer = new byte[4096];
try {
int count = inputStream.read(buffer, 0, buffer.length);
while (count != -1) {
outputStream.write(buffer, 0, count);
count = inputStream.read(buffer, 0, buffer.length);
}
}
catch (RuntimeException e) {
throw e;
}
catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}/**
-
FileItem类对象创建
-
@param inputStream inputStream
-
@param fileName fileName
-
@return FileItem
*/
public static FileItem createFileItem(InputStream inputStream, String fileName) {
FileItemFactory factory = new DiskFileItemFactory(16, null);
String textFieldName = “file”;
FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
OutputStream os = null;
// 使用输出流输出输入流的字节
try {
os = item.getOutputStream();
while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
inputStream.close();
}
catch (IOException e) {throw new IllegalArgumentException("文件上传失败");
}
finally {
if (os != null) {
try {
os.close();
}
catch (IOException e) {} } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { } }
}
return item;
}
public static void createZipFileByExcel(HttpServletResponse resp, Map<String, XSSFWorkbook> workbooks,
String fileName) throws IOException {
ServletOutputStream out = resp.getOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(out);
try {
resp.setContentType(“application/octet-stream; charset=UTF-8”);
// response.addHeader(“filename”, URLEncoder.encode(returnName, “UTF-8”));
resp.setHeader(“Content-Disposition”, “attachment; filename=” + fileName + “.zip”);
for (Map.Entry<String, XSSFWorkbook> subEntry : workbooks.entrySet()) {
String key = subEntry.getKey();
XSSFWorkbook workbook = subEntry.getValue();
ZipEntry z = new ZipEntry(key);
zipOutputStream.putNextEntry(z);
workbook.write(zipOutputStream);
}
zipOutputStream.flush();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
// 注意关闭顺序,否则可能文件错误
if (zipOutputStream != null) {
zipOutputStream.close();
}
if (out != null) {
out.close();
}
}}
/**
-
将svg文件打包
*/
public static void packageZip(List srcfile, HttpServletResponse response, HttpServletRequest request,
String filename) {
// 响应头的设置
response.reset();
response.setCharacterEncoding(“utf-8”);
response.setContentType(“multipart/form-data”);// 设置压缩包的名字
// 解决不同浏览器压缩包名字含有中文时乱码的问题
String downloadName = filename + “.zip”;
String agent = request.getHeader(“USER-AGENT”);
try {
if (agent.contains(“MSIE”) || agent.contains(“Trident”)) {
downloadName = java.net.URLEncoder.encode(downloadName, “UTF-8”);
}
else {
downloadName = new String(downloadName.getBytes(“UTF-8”), “ISO-8859-1”);
}
}
catch (Exception e) {
e.printStackTrace();
}
response.setHeader(“Content-Disposition”, “attachment;fileName=”" + downloadName + “”");
// 设置压缩流:直接写入response,实现边压缩边下载
ZipOutputStream zipos = null;
try {
zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
zipos.setMethod(ZipOutputStream.DEFLATED); // 设置压缩方法
}
catch (Exception e) {
e.printStackTrace();
}// 循环将文件写入压缩流
DataOutputStream os = null;try {
for (int i = 0; i < srcfile.size(); i++) {
try {
// 添加ZipEntry,并ZipEntry中写入文件流
// 这里,加上i是防止要下载的文件有重名的导致下载失败
zipos.putNextEntry(new ZipEntry(srcfile.get(i).getName()));
os = new DataOutputStream(zipos);
InputStream is = new FileInputStream(srcfile.get(i));
byte[] b = new byte[100];
int length = 0;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
zipos.closeEntry();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
// 关闭流
try {
os.flush();
os.close();
zipos.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
依赖
commons-io
commons-io
2.6
commons-fileupload
commons-fileupload
1.4
package com.cxl.ky.entry.vo;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;
@Data
public class UnzipFileVo {
private MultipartFile file;
}