title: How to send zip file without creating it on physical location?
body: <p>I want to send email with zip file attachment.. I m able to send pdf files without saving them on physical location using ByteArrayOutputStream. But when I try zip those file an send it its not working. It gives exception illegal attachment. </p>
  
  <p>Below is the code which I have written to create zip.</p>
  
  <pre><code>private MimeBodyPart zipAttachment( List&lt;ByteArrayOutputStream&gt; attachmentList, List&lt;String&gt; reportFileNames )
  {
      MimeBodyPart messageBodyPart = null;
      try
      {
          // File file = File.createTempFile( "Reports.zip",".tmp" );
          // FileOutputStream fout = new FileOutputStream(file);
          ByteArrayOutputStream bout = new ByteArrayOutputStream(attachmentList.size());
          ZipOutputStream zos = new ZipOutputStream( bout );
          ZipEntry entry;
          for( int i = 0; i &lt; attachmentList.size(); i++ )
          {
              ByteArrayOutputStream attachmentFile = attachmentList.get( i );
              byte[] bytes = attachmentFile.toByteArray();
              entry = new ZipEntry( reportFileNames.get( i ) );
              entry.setSize( bytes.length );
              zos.putNextEntry( entry );
              zos.write( bytes );
          }
          messageBodyPart = new MimeBodyPart();
          DataSource source = new ByteArrayDataSource( bout.toByteArray(), "application/zip" );
          messageBodyPart.setDataHandler( new DataHandler( source ) );
          messageBodyPart.setFileName( "Reports.zip" );
  
      }
      catch( Exception e )
      {
          // TODO: handle exception            
      }
      return messageBodyPart;
  }
  </code></pre>