Tôi đã tạo nhiều BufferedImages trong bộ nhớ và tôi muốn nén chúng thành một tệp zip trước khi gửi nó dưới dạng tệp đính kèm email. Làm cách nào để lưu tệp vào zip mà không đọc tệp từ đĩa.Làm cách nào để tạo tệp zip mà không lưu vào đĩa bằng Java?
Có cách nào để tôi có thể nén các tệp đó mà không tạo tệp tạm thời không?
Ghi vào đĩa mất nhiều thời gian do hàng nghìn tệp được tạo.
/*
* 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 < 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));
}
}
Xem java.utli.zip. –
Chỉ vì sự tò mò của tôi, tại sao bạn muốn tránh tạo một tệp tạm thời? –
Điều này dường như có liên quan http://stackoverflow.com/questions/7108035/how-to-flush-the-zipoutputstream-periodically-in-java – Ted