Witam mam taki servlet:
public class ZipDownloadServlet extends HttpServlet {
public static final String FILE_SEPARATOR = System.getProperty("file.separator");
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//
// The path below is the root directory of data to be
// compressed.
//
String path = getServletContext().getRealPath("data");
File directory = new File(path);
String[] files = directory.list();
//
// Checks to see if the directory contains some files.
//
if (files != null && files.length > 0) {
//
// Call the zipFiles method for creating a zip stream.
//
byte[] zip = zipFiles(directory, files);
//
// Sends the response back to the user / browser. The
// content for zip file type is "application/zip". We
// also set the content disposition as attachment for
// the browser to show a dialog that will let user
// choose what action will he do to the sent content.
//
ServletOutputStream sos = response.getOutputStream();
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename="DATA.ZIP"");
sos.write(zip);
sos.flush();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* Compress the given directory with all its files.
*/
private byte[] zipFiles(File directory, String[] files) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
byte bytes[] = new byte[2048];
for (String fileName : files) {
FileInputStream fis = new FileInputStream(directory.getPath() +
ZipDownloadServlet.FILE_SEPARATOR + fileName);
BufferedInputStream bis = new BufferedInputStream(fis);
zos.putNextEntry(new ZipEntry(fileName));
int bytesRead;
while ((bytesRead = bis.read(bytes)) != -1) {
zos.write(bytes, 0, bytesRead);
}
zos.closeEntry();
bis.close();
fis.close();
}
zos.flush();
baos.flush();
zos.close();
baos.close();
return baos.toByteArray();
}
}
Działa jak widać czyli pobiera pliki z dysku dodaje je do zipa a potem zwraca zipa jako atachment. Próbuje sobie teraz utrudnić zadanie i pobieram z bazy danych plik. Mam go zapisanego jako byte[]. I teraz moje pytanie czy da się tak zrobić aby w podobny sposób dodawać pliki przechowywane jako tablice bajtów?
Jeśli tak to proszę o wskazówki jakby mialo to wyglądać. Dzięki
--edit
Dodam że zależy mi aby po stronie servletu działa się taka akcja. Nie chce najpierw pobierać je na dysk i potem zipować bo to bez sensu.