title: How can I generate zip file without saving to the disk with Java?
body: <p>I have generated many BufferedImages in memory and I want to compress them into one zip file before sending it as a email attachment. How do I save the files to a zip without reading files from disk.</p>
  
  <p>Is there any way I can compress those files without creating a temp file ?</p>
  
  <p>Writing to disk is time consuming due to thousands of files being created.</p>
  
  <pre><code>/*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package cccprog;
  
  import java.awt.Component;
  import java.awt.Panel;
  import java.awt.event.KeyEvent;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import javax.imageio.ImageIO;
  import javax.swing.JFrame;
  import javax.swing.JRadioButton;
  
  /**
   *
   * @author Z
   */
  public class N {
  
      public static void main(String[] args) throws Exception {
          for (int i = 0; i &lt; 10; i++) {
              JFrame jf = new JFrame();
              Panel a = new Panel();
  
              JRadioButton birdButton = new JRadioButton();
              birdButton.setSize(100, 100);
  
              birdButton.setSelected(true);
              jf.add(birdButton);
  
              getSaveSnapShot(birdButton, i + ".bmp");
  
          }
      }
  
      public static BufferedImage getScreenShot(Component component) {
  
          BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
          // paints into image's Graphics
          component.paint(image.getGraphics());
          return image;
      }
  
      public static void getSaveSnapShot(Component component, String fileName) throws Exception {
          BufferedImage img = getScreenShot(component);
  //        BufferedImage img = new BufferedImage(image.getWidth(),image.getHeight(),BufferedImage.TYPE_BYTE_BINARY);
  
          // write the captured image as a bmp
          ImageIO.write(img, "bmp", new File(fileName));
      }
  }
  </code></pre>