0

Filedownload & ZipOutputStream

asked 2018-10-16 15:24:52 +0800

IngoB gravatar image IngoB flag of Germany
256 6

Hi,

I want to offer a download of multiple files, which are compressed. Since I have limited space on the hdd, I want to zip the files while downloading.
This seems to be possible, if you write to the response's output stream (Creating Zip file while client is downloading) / (Compress dynamic content to ServletOutputStream).

I tried to write directly to the outputstream, but it fails with: (illegal character (SyntaxError))

HttpServletResponse response = ((HttpServletResponse) Executions.getCurrent().getNativeResponse());
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=test.zip");
try (ZipOutputStream out = new ZipOutputStream(response.getOutputStream())) {
    byte[] readAllBytes = Files.readAllBytes(Paths.get("test.xlsx"));
    ZipEntry zipEntry = new ZipEntry("test.xlsx");
    zipEntry.setSize(readAllBytes.length);
    zipEntry.setTime(System.currentTimeMillis());
    out.putNextEntry(zipEntry);
    out.write(readAllBytes);
    out.closeEntry();
    // You may add other files here if you want to
    out.finish();
} catch (Exception e) {
    e.printStackTrace();
}


Another, probably better option would be to use the Filedownload, but the only stream is an Inputstream.

Does anyone got an idea how to solve this? Thank you :)

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-10-17 16:49:16 +0800

MDuchemin gravatar image MDuchemin
1336 1 4
ZK Team

Hi Ingo,

Executions.getCurrent.getResponse() will return the response object associated with the current /zkau request. You cannot use this to start a file download because by the time you get access to this object, ZK have already written into it, and will write again. Furthermore, the /zkau request is not made by the browser navigation, but as an Ajax request by the client engine, which is not equipped to process a binary file (it only expects JSON data as a reply).This is a limited communication channel between ZK server and client.

This mean that you cannot use the execution HttpResponse to download a file. To do this, the FileDownload utils in ZK open a separate request to a different AU Extension (sort of mini-servlet). To write to response directly, you would need to do something similar and send the client to load from your own servlet, or your own AuExtension.

If you'd like more details on that part, I'd recommend contacting support.

link publish delete flag offensive edit

Comments

Thank you, I will try that :)

IngoB ( 2018-10-17 19:49:40 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2018-10-16 15:24:52 +0800

Seen: 7 times

Last updated: Oct 17

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More