0

we have below sample code which will a create zip file, but we have a scenario to add 100 samplefolder with various file size in each

 void createzipfile() {
   Map < String, byte[] > outputMap = new HashMap < String, byte[] > (); // will be 100 samplefolder folder with various size in each folder
   outputMap.put(samplefolder + "data.xml", xml.getBytes());
   outputMap.put(samplefolder + aa + "data.txt", data.getBytes());
   outputMap.put(samplefolder + bb + "data1.txt", data1.getBytes());
   outputMap.put(samplefolder + cc + "data2.txt", data2.getBytes());
   outputMap.put(samplefolder + "dd.txt", data3.getBytes());

   // Create output of zip file
   zipFile.createZipFile("data.zip");
   for (Map.Entry < String, byte[] > entry: outputMap.entrySet()) {
    zipFile.addFileToZip(entry.getKey(), entry.getValue());
   }
   zipFile.setDownloadStatus(DownloadTracker.Status.STOPPED_SUCCESS, "zip were successfully fetched");

   // Finalize zip file
   try {
    zipFile.writeAndClose();
   } catch (Exception e) {
    zipFile.setDownloadStatus(DownloadTracker.Status.STOPPED_ERROR, e.getMessage());
    logger.error("Error while creating zip file", e);
   } finally {
    zipFile.completeOutputFile();
    vinButtons = true;
   }
   logger.info("finished");
  }

  public void addFileToZip(String zipHierarchy, File file) throws FileNotFoundException {
   InputStream is = new FileInputStream(file);
   this.addFileToZip(zipHierarchy, (InputStream) is);
   try {
    is.close();
   } catch (IOException var5) {}

  }

  public boolean createZipFile(String zipName) {
   ExternalContext externalContext = this.facesContext.getExternalContext();
   externalContext.setResponseContentType("application/zip");
   externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"" + zipName + "\"");

   OutputStream responseStream;
   try {
    responseStream = externalContext.getResponseOutputStream();
   } catch (IOException var5) {
    logger.error("Failed to create zip file.");
    this.facesContext.responseComplete();
    return false;
   }

   this.zipStream = new ZipOutputStream(responseStream);
   this.downloadTracker.setStatus(Status.STARTED);
   return true;
  }

with current implementation its returning zip without adding all folder .kindly provide some way to accomplish this scenario

senthil kumar
  • 237
  • 1
  • 6
  • 18
  • How is this jsf related? Other than it being used. Can you return a plain text stream in jsf? – Kukeltje Jun 10 '19 at 13:01
  • @Kukeltje thx for the reply.i need to have content type as ZIP but its should rendered zipfile once its complete package, then only user need to see the zip file...but currently its rendering before completing the process as **data.zip.08d1ekm.partial **(still some of the process happening on background) how to avoid the scenario with modified above code? – senthil kumar Jun 10 '19 at 13:48
  • Dou you have errors logged in log files? Please provide a [mcve]. – Selaron Jun 11 '19 at 10:05

0 Answers0