package com.ruoyi.common.utils.file;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
/**
* <p>Description : </p>
* <p>Copyright : Copyright (c) 2018</p>
* <p>Company : tgram </p>
*
* @author eric
* @version 1.0
* @Date 2022/9/21 上午10:10
*/
public class ZipUtils {
public static List<MultipartFile> readZipFile(MultipartFile mf) {
File file = getFile(mf);
return readZipFile(file, null);
}
/**
* multipartFile转file
*
* @param mf
* @return
*/
public static File getFile(MultipartFile mf) {
//文件上传前的名称
String fileName = mf.getOriginalFilename();
File file = new File(fileName);
OutputStream out = null;
try {
//获取文件流,以文件流的方式输出到新文件
// InputStream in = multipartFile.getInputStream();
out = new FileOutputStream(file);
byte[] ss = mf.getBytes();
for (int i = 0; i < ss.length; i++) {
out.write(ss[i]);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return file;
}
/**
* zip文件的来源,file 表示网络来源, localPath表示本地文件
*
* @param file
* @param localPath
* @return
*/
public static List<MultipartFile> readZipFile(File file, String localPath) {
List<MultipartFile> mfList = new ArrayList<>();
try {
ZipFile zipFile = null;
InputStream in = null;
if (file == null) {
zipFile = new ZipFile(localPath);
in = new BufferedInputStream(new FileInputStream(localPath));
} else {
zipFile = new ZipFile(file);
in = new BufferedInputStream(new FileInputStream(file));
}
ZipInputStream zin = new ZipInputStream(in);
ZipEntry ze;
while ((ze = zin.getNextEntry()) != null) {
if (ze.isDirectory()) {
} else {
System.err.println("file - " + ze.getName() + " : " + ze.getSize() + " bytes");
long size = ze.getSize();
if (size > 0) {
InputStream stream = zipFile.getInputStream(ze);
FileItem fileItem = createFileItem(stream, ze.getName());
MultipartFile mf = new CommonsMultipartFile(fileItem);
mfList.add(mf);
}
}
}
zin.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
return mfList;
}
/**
* 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[10 * 1024 * 1024];
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) {
e.printStackTrace();
throw new IllegalArgumentException("文件上传失败");
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return item;
}
}