如何在不使用Java保存到磁盘的情况下生成zip文件? [英] How can I generate zip file without saving to the disk with Java?

查看:56
本文介绍了如何在不使用Java保存到磁盘的情况下生成zip文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在内存中生成了许多BufferedImages,我想将它们压缩为一个zip文件,然后再将其作为电子邮件附件发送.如何在不从磁盘读取文件的情况下将文件保存为zip.

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.

有什么方法可以压缩那些文件而不创建临时文件吗?

Is there any way I can compress those files without creating a temp file ?

由于要创建成千上万个文件,因此写入磁盘非常耗时.

Writing to disk is time consuming due to thousands of files being created.

/*
 * 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));
    }
}

推荐答案

我不确定您在这里使用的用例,如果内存中有成千上万个文件,则可能很快就会用完内存.

I'm not sure about the use-case you're having here, if you have thousands of files in memory you might run out of memory rather quickly.

但是,无论如何,通常都是通过流生成zip文件,因此无需将它们临时存储在文件中-最好将其存储在内存中或直接传输到远程接收者(只有很小的内存缓冲区,以避免很大的缓冲)内存占用).

However, zip files are typically generated with streams anyway, so there's no need to temporarily store them in a file - might as well be in memory or streamed directly to a remote recipient (with only a small memory buffer to avoid a large memory footprint).

我找到了几年前写的一个旧的zip实用程序,并针对您的用例进行了稍微的修改.它从文件列表创建一个存储在字节数组中的zip文件,该文件也存储在字节数组中.由于您在内存中表示了很多文件,因此我添加了一个小的帮助程序类 MemoryFile ,其中仅包含文件名和包含内容的字节数组.哦,我公开了这些领域,以避免样板化的吸气剂/装填剂的使用-当然是为了节省一些空间.

I found an old zip utility written years ago and modified it slightly for your use-case. It creates a zip file stored in a byte array from a list of files, also stored in byte arrays. Since you have a lot of files represented in memory, I added a small helper class MemoryFile with just the filename and a byte array containing the contents. Oh, and I made the fields public to avoid the boilerplate getter/setter stuff - just to save some space here of course.

public class MyZip {

    public static class MemoryFile {
        public String fileName;
        public byte[] contents;
    }

    public byte[] createZipByteArray(List<MemoryFile> memoryFiles) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
        try {
            for (MemoryFile memoryFile : memoryFiles) {
                ZipEntry zipEntry = new ZipEntry(memoryFile.fileName);
                zipOutputStream.putNextEntry(zipEntry);
                zipOutputStream.write(memoryFile.contents);
                zipOutputStream.closeEntry();
            }
        } finally {
            zipOutputStream.close();
        }
        return byteArrayOutputStream.toByteArray();
    }

}

这篇关于如何在不使用Java保存到磁盘的情况下生成zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆