package com.c2f.hbos.mcc.common.utils;
import com.c2f.hbos.mcc.core.catalog.enums.SHMedicationCatalogDataSourceEnum;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* @description: Zip包工具
* @author: Yang JianXiong
* @since: 2023/3/6
*/
public class DataPackageServiceImpl {
public void upload(MultipartFile dataPackage) throws IOException {
if (dataPackage == null || dataPackage.getSize() <= 0) {
return;
}
File file = multipartFileToFile(dataPackage);
if (file == null || !file.exists()) {
return;
}
ZipFile zipFile = new ZipFile(file, Charset.forName("GBK"));
Enumeration<? extends ZipEntry> dirEntries = zipFile.entries();
while (dirEntries.hasMoreElements()) {
ZipEntry entry = dirEntries.nextElement();
if (entry.isDirectory()) {
continue;
}
// 获取节点输入流
// InputStream inputStream = zipFile.getInputStream(entry);
// 获取节点元数据
System.out.println(entry.getName());
}
// 回收资源
IOUtils.closeQuietly(zipFile);
file.delete();
}
// MultipartFile 转 File
private 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;
}
private 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);
}
}
}
标签:outputStream,java,Springboot,Zip,buffer,inputStream,import,null,上传
From: https://www.cnblogs.com/JaxYoun/p/17184679.html