Apache Commons Compress libaryをご覧ください。必要な機能があります。
もちろん、「エリクソン」はあなたの質問に忠実です。ファイルconentetが必要で、java.io.File
オブジェクトは必要ありません。私の例では、fileNummer番目のファイルのファイル内容(メモリ内)を返すメソッド byte[] getTheContentFormSomewhere(int fileNummer)
があると仮定します。 - もちろん、この機能は貧弱なデザインですが、ilustrationのためだけです。
それはこのようにビットを動作するはずです:あなたのhttpコントローラの
void compress(final OutputStream out) {
ZipOutputStream zipOutputStream = new ZipOutputStream(out);
zipOutputStream.setLevel(ZipOutputStream.STORED);
for(int i = 0; i < 10; i++) {
//of course you need the file content of the i-th file
byte[] oneFileContent = getTheContentFormSomewhere(i);
addOneFileToZipArchive(zipOutputStream, "file"+i+"."txt", oneFileContent);
}
zipOutputStream.close();
}
void addOneFileToZipArchive(final ZipOutputStream zipStream,
String fileName,
byte[] content) {
ZipArchiveEntry zipEntry = new ZipArchiveEntry(fileName);
zipStream.putNextEntry(zipEntry);
zipStream.write(pdfBytes);
zipStream.closeEntry();
}
Snipets:
HttpServletResponse response
...
response.setContentType("application/zip");
response.addHeader("Content-Disposition", "attachment; filename=\"compress.zip\"");
response.addHeader("Content-Transfer-Encoding", "binary");
ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
compress(outputBuffer);
response.getOutputStream().write(outputBuffer.toByteArray());
response.getOutputStream().flush();
outputBuffer.close();
あなたはそれが 'java.io.File'何を意味するのですか? 'File'オブジェクトは単なる空想的なアドレスオブジェクトです。それはどんなファイルコンテンツも運びません。あなたのデータはどこですか? – erickson