-1

I want to Zip a set of files given the location of the set of files.

Send that Zip file to the below-mentioned method

private StreamingOutput buildStreamingOutput(final File pdfFile, final boolean isGeneratedPdf) {
        return new StreamingOutput() {
            @Override
            public void write(OutputStream output) throws IOException, WebApplicationException {
                java.nio.file.Path path = Paths.get(pdfFile.getAbsolutePath());
                byte[] data = CryptoUtil.decryptAsByteArray(path);
                output.write(data);
                output.flush();
                output.close();
                if (isGeneratedPdf) {
                    pdfFile.delete();
                }
            }
        };
    }

Is it possible to zip files and send that Zip file as file(File.class)

Praveen Kumar
  • 93
  • 1
  • 8

1 Answers1

0

May It is useful to you.

Use Below Code :

         public void buildStreamingOutput(HttpServletResponse 
              resp,java.util.List<File> files) throws IOException {
              ZipOutputStream  zipOutstream = new 
              ZipOutputStream(resp.getOutputStream());
           resp.setContentType("application/octet-stream");
           resp.setHeader("Content-Disposition", "attachment; filename= 
        {zipfilename.zip}");

           for(File file : files) {
               appendFileToZIP(zipOutstream,file);
           }

       }


       public void appendFileToZIP( ZipOutputStream _zipOutstream  ,final File pdfFile) {

            int nlength = 0;

            FileInputStream fis = null;
            DataInputStream disObj = null;

            try {

                if (!pdfFile.exists()) {
                    System.out.println(" file does not exists "+pdfFile.getName());
                    return;
                }

                fis = new FileInputStream(pdfFile);

                _zipOutstream.putNextEntry(new ZipEntry(pdfFile.getName()));

                disObj = new DataInputStream(fis);

                byte[] bbuf = new byte[1024];
                while ((disObj != null) && ((nlength = disObj.read(bbuf)) != -1)) {
                    _zipOutstream.write(bbuf, 0, nlength);
                }
                _zipOutstream.flush();

            } catch (IOException e) {

            } catch (Exception e) {

            } finally {
                try {
                    fis.close();
                } catch (Exception e) {

                }

            }
        }
Ranjith Bokkala
  • 379
  • 1
  • 10