4 Replies Latest reply on Nov 8, 2013 9:32 AM by manuel.moroni

    save zip file

    manuel.moroni

      hi,

      i have the following code

       

      ZipOutputStream zos=new ZipOutputStream(response.getOutputStream());

       

       

        byte[] buffer=new byte[1024];

        int len;

        int zipSize=0;

       

        for(Allegato a:allegati){

        ZipEntry ze=new ZipEntry(a.getNome());

        zos.putNextEntry(ze);

       

        while((len=a.getDocumento().read(buffer))>0){

        zos.write(buffer,0,len);

        zipSize+=len;

        }

       

        a.getDocumento().close();

        }

       

       

        zos.closeEntry();

        zos.close();

       

       

       

        response.setContentType("application/zip");

        response.setHeader("Pragma", "no-cache");

        response.setHeader("Content-Encoding", "zip");

        response.setHeader("Content-Disposition", "attachment;filename="+System.currentTimeMillis()+".zip");

      // response.setHeader("Content-disposition", "attachment;filename=zip.zip");

      // response.setHeader("Content-Length", ""+zipSize);

      // response.setHeader("Expires",             "0");

        response.getOutputStream().flush();

        response.getOutputStream().close();

      // IOUtils.copy(zos, response);

      in order to allow the user to download a zip file.

      if i save the zip on the server it works file.

      if i try to send it as a reponse (Internet Explorer) to the user i get the following error:

      the compress (zipped) folder ..... is invalid

      what am i missing?

        • 1. Re: save zip file
          bleathem
          • 2. Re: save zip file
            manuel.moroni

            i'm a complete noob....

            i tried but it dues not work

             

            <a4j:mediaOutput element="allegati" cacheable="false" createContent="#{sessionContainer.findSession.allegato.processDownloadAllegato}" uriAttribute="href" mimeType="application/zip"> 

              <h:graphicImage url="../........." title="download"/> 

              </a4j:mediaOutput>

             

            public void processDownloadAllegato(OutputStream out, Object data) throws ParseException, SQLException

              {

              logStatistic("------------------------------ START METHOD processDownloadAllegato ------------------------------");

              FacesContext fc = FacesContext.getCurrentInstance();

              String listLegame="";

              List<Allegato> allegati=new ArrayList<Allegato>();

            // HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

              try

              {

              for(Allegato all:listMarca){

              //listLegame+=all.getIdDocumento()+";";

              if(all.getSelected()){

              listLegame+=all.getIdDocumento()+";";

              }

              }

             

              logInfo("lista legami: "+listLegame);

              logInfo("size listMarca: "+listMarca.size());

              allegati=allegatiDBMng.downloadAllegatiFromDB(listLegame);

              listLegame="";

             

              //FileOutputStream fos = new FileOutputStream("/rcg/resources/RCG/zip.zip");

              //ZipOutputStream zos=new ZipOutputStream(out);

              ZipOutputStream zos= (ZipOutputStream) data;

            // ZipOutputStream zos=new ZipOutputStream(response.getOutputStream());

             

             

              byte[] buffer=new byte[1024];

              int len;

              int zipSize=0;

             

              for(Allegato a:allegati){

              ZipEntry ze=new ZipEntry(a.getNome());

              zos.putNextEntry(ze);

             

              while((len=a.getDocumento().read(buffer))>0){

              zos.write(buffer,0,len);

              zipSize+=len;

              }

             

              a.getDocumento().close();

              }

             

             

              zos.closeEntry();

              zos.close();

             

             

             

            // response.setContentType("application/zip");

            // response.setHeader("Pragma", "no-cache");

            // response.setHeader("Content-Encoding", "zip");

            // response.setHeader("Content-Disposition", "attachment;filename="+System.currentTimeMillis()+".zip");

            // response.setHeader("Content-disposition", "attachment;filename=zip.zip");

            // response.setHeader("Content-Length", ""+zipSize);

            // response.setHeader("Expires",             "0");

            // response.getOutputStream().flush();

            // response.getOutputStream().close();

             

             

              fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Download eseguito con successo.", ""));

             

            // ret = "OK";

              } catch(Exception x)

              {

              logError("processDownloadAllegato", x);

              FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, x.getMessage(), x.getMessage());

              FacesContext.getCurrentInstance().addMessage(null, message);

              }

             

              logStatistic("------------------------------ END METHOD processDownloadAllegato ------------------------------");

            // return ret;

              }

            • 3. Re: save zip file
              bleathem

              Any errors in your browser console?  Anything in your server log?  Also, what version of RichFaces are you using?

              • 4. Re: save zip file
                manuel.moroni

                no error and no logs at all.

                setting properly the response i got it to work but i'm interesting on the use of the component itself.

                 

                the richfaces version is 3.3.3 (migrating to 4.3.3 meanwhile)