Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

download files from server and compress it in zip format [duplicate]

Tags:

jsf

primefaces

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

like image 233
carlo del rosario Avatar asked Jul 20 '16 00:07

carlo del rosario


People also ask

Can you double zip a file to make it smaller?

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.

How do I convert multiple files into a zip file?

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”.

Does compressing to zip file lose quality?

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.


1 Answers

changing

file = new DefaultStreamedContent(stream, "zip", "photos.zip"); 

to

file = new DefaultStreamedContent(stream, "application/zip", "photos.zip",  Charsets.UTF_8.name());

fixed it

like image 143
carlo del rosario Avatar answered Oct 19 '22 19:10

carlo del rosario