如何在java6中创建多个文件夹作为ZIP流

时间:2019-06-10 09:06:13

标签: jsf java-6 zipoutputstream

我们在下面的示例代码中将创建一个zip文件,但是我们有一种方案,可以在每个示例文件中添加100个具有不同文件大小的samplefolder

 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;
  }

使用当前的实现,其返回的zip而不添加所有文件夹。请提供一些方法来实现此方案

0 个答案:

没有答案