我想从某些外部来源(如S3)下载多个文件,创建一个zip包含所有这些文件的文件,并向用户显示下载该zip文件的链接。
显然,我可以按顺序处理文件,读取每个文件的输入流并将其写入ZipOutputStream。
如何并行读取所有输入文件流并写入单个输出流,这样我就可以向用户显示下载链接,而不必等到zip文件完全写入后才可以?
我当前的代码:
String realpath = getServletContext().getRealPath("/");
response.setContentType("application/zip");
response.setHeader("Content-Disposition","attachment; filename="+fi.replace('/', '-')+"_"+ff.replace('/', '-')+".zip");
ServletOutputStream out = null;
ZipOutputStream zipfile = null;
try
{
List<Object[]> cfdis = /*my hibernate criteria source, your Database?*/
out = response.getOutputStream();
zipfile = new ZipOutputStream(out);
ZipEntry zipentry = null;
for(Object[] cfdi:cfdis)
{
zipentry = new ZipEntry(cfdi[1].toString()+".xml");
zipfile.putNextEntry(zipentry);
InputStream in = new FileInputStream(new File(realpath+cfdi[0].toString()));
byte[] bytes = new byte[FILEBUFFERSIZE];
int bytesRead;
while ((bytesRead = in.read(bytes)) != -1)
{
zipfile.write(bytes, …Run Code Online (Sandbox Code Playgroud)