title: Zip a great number of pdf file and download it: OutOfMemoryError: Java heap space
body: <p>I want create a zip file to download it. I have a great number of pdf file..</p>
  
  <p>I need to zip them into sub-zip of 50MB one, so I create a master zip that contain all other sub-zip.. </p>
  
  <p>But when run my code I obtain: </p>
  
  <pre><code>Grave: Servlet.service() for servlet [appServlet] in context with path [/myProg] threw exception [Handler processing failed; nested exception is java.lang.OutOfMemoryError: Java heap space] with root cause
  java.lang.OutOfMemoryError: Java heap space
  </code></pre>
  
  <p>This is my code to divide and zip all files:</p>
  
  <pre><code>public void zippatore(List&lt;File&gt; filesDaZippare, ZipOutputStream masterZos, String zipName, String pdfName) throws IOException{
      float newSize = 0;
      boolean first = true;
      int count = 1;
      File file;
      ByteArrayOutputStream subBaos = new ByteArrayOutputStream();
      ZipOutputStream subZos = new ZipOutputStream(subBaos);
  
      masterZos.putNextEntry(new ZipEntry(zipName+" "+count++ +".zip"));
      while(filesDaZippare.size() &gt; 0){
          file = filesDaZippare.remove(0);
          FileInputStream fis = new FileInputStream(file);
          newSize += file.length();
          String fileName = file.getName().substring(2);
          if(newSize &lt; MAX_SIZE || first){
              subZos.putNextEntry(new ZipEntry(pdfName+" "+fileName+".pdf"));
              IOUtils.copy(fis, subZos);
              file.delete();
              subZos.closeEntry();
              first = false;
          }else{
              subZos.flush();
              subZos.close();
  
              masterZos.write(subBaos.toByteArray());
              masterZos.closeEntry();
              masterZos.putNextEntry(new ZipEntry(zipName+" "+count++ +".zip"));
  
              subZos = new ZipOutputStream(subBaos);
              subZos.putNextEntry(new ZipEntry(pdfName+" "+fileName+".pdf"));
              IOUtils.copy(fis, subZos);
              file.delete();
  
              newSize = file.length();
              first = true;
          }
          subZos.closeEntry();
      }
      subZos.closeEntry();
      subZos.flush();
      subZos.close();
      masterZos.write(subBaos.toByteArray());
  
      masterZos.closeEntry();
  }
  </code></pre>
  
  <p>How can I fix it?? </p>
  
  <p><strong>EDIT:</strong>
  I added the code to save in a tmp folder my sub-zip file.. And than I do this:</p>
  
  <pre><code>    File folder = new File(VariabiliGlobali.PATH_TMP);
      byte[] buffer = new byte[1024];
  
      for(File file : folder.listFiles()) {
          FileInputStream fis = new FileInputStream(file);
          masterZos.putNextEntry(new ZipEntry(file.getName()));
  
          int length;
          while ((length = fis.read(buffer)) &gt; 0) {
              masterZos.write(buffer, 0, length);
          }
  
          masterZos.closeEntry();
          fis.close();
      }
      masterZos.flush();
      masterZos.close();
  </code></pre>
  
  <p>to get all zip file and put in my master zip file, but I obtain the same error when I put them in it...</p>