将生成的 Zip 的 Zip 提供给客户端 [英] Serve Zip of generated Zips to Client
问题描述
我正在生成一个 ZIP 文件,其中包含一堆自动生成的 XML 文件.最近需求发生了变化,现在我必须多次生成 ZIP(带有 XML 数据的变化)并将它们直接提供给客户端,而无需使用服务器中的实际文件.这就是我在做的:
I'm generating a ZIP file that contains a bunch of autogenerated XML files. Recently requirements changed and now i must generate several times that ZIP (with variations on XML data) and serve them directly to client without using actual files in server. This is what im doing:
// [... servlet handling ... ]
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=cierresZ_a_tangonet" + java.time.LocalDate.now() + ".zip");
// stream straight to client
ServletOutputStream out = response.getOutputStream();
ZipOutputStream zipped_out = new ZipOutputStream(out);
for( each data block from db ){
//CREATION AND PROCESSING OF XML FILES AS ZIP ENTRIES
byte[] xmlBinData = xmlData.toString().getBytes();
zipped_out.write(xmlBinData, 0, xmlBinData.length);
zipped_out.flush();
}
zipped_out.finish();
out.close();
}
尝试这样做但给了我错误:
Tried to do this but gives me errors:
// [... servlet handling ... ]
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=cierresZ_a_tangonet" + java.time.LocalDate.now() + ".zip");
// stream straight to client
ServletOutputStream out = response.getOutputStream();
for( each zip needed ){
ZipOutputStream zipped_out = new ZipOutputStream(out);
for( each data block from db ){
//CREATION AND PROCESSING OF XML FILES AS ZIP ENTRIES
byte[] xmlBinData = xmlData.toString().getBytes();
zipped_out.write(xmlBinData, 0, xmlBinData.length);
zipped_out.flush();
}
zipped_out.finish();
}
out.close();
}
对其进行了一些细微的更改,但给出了相同的错误
Made some slight changes to it, but gives same error
// [... servlet handling ... ]
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=cierresZ_a_tangonet" + java.time.LocalDate.now() + ".zip");
// stream straight to client
ServletOutputStream out = response.getOutputStream();
ZipOutputStream zipped_outs = new ZipOutputStream(out);
for( each zip needed ){
//creates new zip inside big one
ZipEntry zipFile = new ZipEntry(salaActual + ".zip");
zipped_outs.putNextEntry(zipFile);
//opens stream for this new zip
ZipOutputStream zipped_out = new ZipOutputStream(zipped_outs);
for( each data block from db ){
//CREATION AND PROCESSING OF XML FILES AS ZIP ENTRIES
byte[] xmlBinData = xmlData.toString().getBytes();
zipped_out.write(xmlBinData, 0, xmlBinData.length);
zipped_out.flush();
}
//completes zip and closes it then goes for the next one
byte[] zipBinData = zipFile.toString().getBytes();
zipped_outs.write(zipBinData, 0, zipBinData.length);
zipped_outs.flush();
}
//closes the big zip filled with zips and returns
zipped_outs.finish();
out.close();
}
推荐答案
你需要 finish()
和 flush()
所有内部 ZipOutputStreams and 外部 ZipOutputStream.
You need to finish()
and flush()
all the inner ZipOutputStreams and the outer ZipOutputStream.
基本上:
response.setContentType("application/zip");
response.setHeader("Content-Disposition", ...);
ZipOutputStream mainZip = new ZipOutputStream(response.getOutputStream());
for (each file to download) {
ZipEntry zipEntry = new ZipEntry(fileName);
mainZip.putNextEntry(zipEntry);
if (file data available) {
// write file data to mainZip here
} else {
ZipOutputStream subZip = new ZipOutputStream(mainZip);
for (each subfile to download) {
ZipEntry zipEntry = new ZipEntry(subFileName);
subZip.putNextEntry(zipEntry);
// write subfile data to subZip here
}
subZip.finish();
subZip.flush(); // do not close
}
}
mainZip.finish();
mainZip.close(); // flushes for you
请注意 mainZip
和 subZip
如何获取 zip 条目并且都完成.您的代码中缺少这一点.
Notice how both mainZip
and subZip
gets zip entries and both are finished. That was missing from your code.
这篇关于将生成的 Zip 的 Zip 提供给客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!