im trying to implement a download all functionality, my requirement is file should be compressed in .zip, heres the UI
<p:toolbar>
<f:facet name="right">
<p:commandButton value="Download all photos" ajax="false" actionListener="#{ManageActivities.getAllParticipantPhoto}" onclick="PrimeFaces.monitorDownload(start, stop);" icon="ui-icon-image">
<p:fileDownload value="#{ManageActivities.file}" />
</p:commandButton>
</f:facet></p:toolbar>
and here is the managedbean code
private StreamedContent file;
public void getAllParticipantPhoto() {
ByteArrayInputStream bis = new ByteArrayInputStream(zipBytes());
InputStream stream = bis;
file = new DefaultStreamedContent(stream, "zip", "photos.zip");
}
private byte[] zipBytes () {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
try{
for(Participants p : partcpnts){
if(p.getPhoto() != null){
ZipEntry entry = new ZipEntry(p.getFirstName()+".jpg");
zos.putNextEntry(entry);
zos.write(p.getPhoto());
}
}
}catch(Exception e){
e.printStackTrace();
}
return baos.toByteArray();
}
i can download file as ZIP successfully but i am unable to extract it, windows is prompting the'cannot open file as archive' error
Unfortunately, there isn't a simple method to make a ZIP file smaller. Once you squeeze the files to their minimum size, you can't squeeze them again. So zipping a zipped file won't do anything, and on some occasions, it can make the size even bigger.
Right-click on the file or folder. To place multiple files into a zip folder, select all of the files while hitting the Ctrl button. Then, right-click on one of the files, move your cursor over the “Send to” option and select “Compressed (zipped) folder”.
Zip. A compressed zip file is an archive file format featuring lossless compression. This format means that your files and folders will compress, get smaller in data size, but not lose anything in terms of quality or data. Your original files will always remain intact.
changing
file = new DefaultStreamedContent(stream, "zip", "photos.zip");
to
file = new DefaultStreamedContent(stream, "application/zip", "photos.zip", Charsets.UTF_8.name());
fixed it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With