Apache Commons Compress libary'ye bir göz atın, ihtiyacınız olan işlevselliği sağladı.
Tabii ki "erickson", sorunuzun haklı övgüdür. java.io.File
nesnesine değil, dosya conentetine ihtiyacınız olacak. Örneğimde, dosyaYönetici dosyası için dosya içeriğini (bellekte) döndüren bir yöntem byte[] getTheContentFormSomewhere(int fileNummer)
olduğunu varsayalım. - Tabii ki bu fonksiyon zayıf bir tasarımdır, ancak sadece ilustrasyon içindir.
Bir böyle bit çalışması gerekir: En http kumandanın
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();
bunu bir 'java.io.File' ne demek? Bir 'File' nesnesi sadece süslü bir adres nesnesidir; herhangi bir dosya içeriği taşımamaktadır. Verileriniz nerede? – erickson