test方法
@Test
@DisplayName("多文件导出")
void multipleFile() throws IOException {
List sgGuideList = dao.queryAll(null);
List> list = Lists.partition(sgGuideList, 50000);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream out = new ZipOutputStream(outputStream);
// ZipOutputStream out = new ZipOutputStream(new FileOutputStream(filePath + "test-" + System.currentTimeMillis() + ".zip"));
for (List sgGuides : list) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String fileName = filePath + "repeatedWrite-" + System.currentTimeMillis() + ".xlsx";
EasyExcel.write(byteArrayOutputStream, SgGuide.class).sheet("导购").doWrite(sgGuides);
ZipUtils.zip(out, new ByteArrayInputStream(byteArrayOutputStream.toByteArray()), "repeatedWrite-" + System.currentTimeMillis() + ".xlsx");
byteArrayOutputStream.close();
}
// 注意关闭流的顺序,在上传oss之前必须关闭流否则下载解压的时候会报“文件末端错误”的问题
outputStream.close();
out.close();
AppendObjectSample.uploadOSS(new ByteArrayInputStream(outputStream.toByteArray()), "test-" + System.currentTimeMillis() + ".zip");
}
OSS上传
# 追加上传
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.AppendObjectRequest;
import com.aliyun.oss.model.AppendObjectResult;
import com.aliyun.oss.model.OSSObject;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* @author LongKui
* @since 2021/12/28
*/
public class AppendObjectSample {
private static final String endpoint = "https://oss-cn-zhangjiakou.aliyuncs.com";
private static final String accessKeyId = "LTAI4GH9RrDPQ2yYsAPMKGkQ";
private static final String accessKeySecret = "QvbrMcUs6KoVaIww1HsmnD4TBK8aWB";
private static final String bucketName = "hb3-shopguide";
// 上传到OSS的文件名称。
private static final String key = "1/longkui.xlsx";
public static void uploadOSS(InputStream inputStream, String fileName) {
String key = "1/" + fileName;
/*
* Constructs a client instance with your account for accessing OSS
*/
OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// ObjectMetadata metadata = new ObjectMetadata();
// // 指定上传的内容类型。
// metadata.setContentType("application/zip");
// metadata.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
try {
boolean exist = client.doesObjectExist(bucketName, key);
if (exist) {
client.deleteObject(bucketName, key);
System.out.println("Deleting an appendable object....................");
}
/*
* Append an object from specfied input stream, keep in mind that
* position should be set to zero at first time.
*/
long firstPosition = 0L;
System.out.println("Begin to append object at position(" + firstPosition + ")");
AppendObjectResult appendObjectResult = client.appendObject(
new AppendObjectRequest(bucketName, key, inputStream).withPosition(0L));
System.out.println("\tNext position=" + appendObjectResult.getNextPosition() +
", CRC64=" + appendObjectResult.getObjectCRC() + "\n");
/*
* View object type of the appendable object
*/
OSSObject object = client.getObject(bucketName, key);
System.out.println("\tObject type=" + object.getObjectMetadata().getObjectType() + "\n");
// Do not forget to close object input stream if not use it any more
object.getObjectContent().close();
/*
* Delete the appendable object
*/
// System.out.println("Deleting an appendable object");
// client.deleteObject(bucketName, key);
System.out.println("over...................");
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message: " + oe.getErrorMessage());
System.out.println("Error Code: " + oe.getErrorCode());
System.out.println("Request ID: " + oe.getRequestId());
System.out.println("Host ID: " + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message: " + ce.getMessage());
} catch (IOException e) {
e.printStackTrace();
} finally {
/*
* Do not forget to shut down the client finally to release all allocated resources.
*/
client.shutdown();
}
}
}
引入 相关依赖
pom.xml增加com.aliyun.oss aliyun-sdk-oss 3.10.2 com.alibaba easyexcel 3.0.5 org.apache.ant ant LATEST
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
/**
* 压缩文件工具类
*
* @author LongKui
* @since 2021/12/30
*/
public class ZipUtils {
private ZipFile zipFile;
private ZipOutputStream zipOut; //压缩Zip
private ZipEntry zipEntry;
private static int bufSize; //size of bytes
private byte[] buf;
private int readedBytes;
public ZipUtils() {
this(512);
}
public ZipUtils(int bufSize) {
this.bufSize = bufSize;
this.buf = new byte[this.bufSize];
}
//压缩文件夹内的文件
public void doZip(String zipDirectory) {//zipDirectoryPath:需要压缩的文件夹名
File file;
File zipDir;
zipDir = new File(zipDirectory);
String zipFileName = zipDir.getName() + ".zip";//压缩后生成的zip文件名
try {
this.zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName)));
handleDir(zipDir, this.zipOut);
this.zipOut.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
//由doZip调用,递归完成目录文件读取
public void handleDir(File dir, ZipOutputStream zipOut) throws IOException {
FileInputStream fileIn;
File[] files;
files = dir.listFiles();
if (files.length == 0) {//如果目录为空,则单独创建之.
//ZipEntry的isDirectory()方法中,目录以"/"结尾.
this.zipOut.putNextEntry(new ZipEntry(dir.toString() + "/"));
this.zipOut.closeEntry();
} else {//如果目录不为空,则分别处理目录和文件.
for (File fileName : files) {
//System.out.println(fileName);
if (fileName.isDirectory()) {
handleDir(fileName, this.zipOut);
} else {
fileIn = new FileInputStream(fileName);
this.zipOut.putNextEntry(new ZipEntry(fileName.toString()));
while ((this.readedBytes = fileIn.read(this.buf)) > 0) {
this.zipOut.write(this.buf, 0, this.readedBytes);
}
}
}
}
}
public static void zip(ZipOutputStream zipOut, InputStream inputStream, String fileName) throws IOException {
zipOut.putNextEntry(new ZipEntry(fileName));
int readBytes = 512;
byte[] buf = new byte[readBytes];
while ((readBytes = inputStream.read(buf)) > 0) {
zipOut.write(buf, 0, readBytes);
}
zipOut.closeEntry();
}
//解压指定zip文件
public void unZip(String unZipfileName) {//unZipfileName需要解压的zip文件名
FileOutputStream fileOut;
File file;
InputStream inputStream;
try {
this.zipFile = new ZipFile(unZipfileName);
for (Enumeration entries = this.zipFile.getEntries(); entries.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) entries.nextElement();
file = new File(entry.getName());
if (entry.isDirectory()) {
file.mkdirs();
} else {
//如果指定文件的目录不存在,则创建之.
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
inputStream = zipFile.getInputStream(entry);
fileOut = new FileOutputStream(file);
while ((this.readedBytes = inputStream.read(this.buf)) > 0) {
fileOut.write(this.buf, 0, this.readedBytes);
}
fileOut.close();
inputStream.close();
}
}
this.zipFile.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
//设置缓冲区大小
public void setBufSize(int bufSize) {
this.bufSize = bufSize;
}
}