【发布时间】:2013-06-03 20:50:41
【问题描述】:
我正在尝试动态创建一个 zip 文件,其中包含一堆要从 servlet 返回的 csv 文件,这非常令人困惑。一点指导会很棒。以下是我需要以某种方式协同工作的代码块:
// output stream coming from httpResponse, thats all fine
ZipOutputStream zip = new ZipOutputStream(outputStream);
// using the openCSV library to create the csv file
CSVWriter writer = new CSVWriter(Writer?);
// what writer do I use? I want to write to memory, not a file
writer.writeNext(entries);
writer.close();
// at this point should I have the csv file in memory somewhere?
//and then try to copy it into the zip file?
int length;
byte[] buffer = new byte[1024 * 32];
zip.putNextEntry(new ZipEntry(getClass() + ".csv"));
// the 'in' doesn't exist yet - where am I getting the input stream from?
while((length = in.read(buffer)) != -1)
zip.write(buffer, 0, length);
zip.closeEntry();
zip.flush();
【问题讨论】:
-
一个
ByteArrayOutputStream? -
写入器接受写入器参数而不是输出流 - 我可以将它包装在 printWriter 中吗?
-
这个答案可能对stackoverflow.com/a/68492465/3946706有帮助