0

我将如何将 BufferedImage 直接保存到 zip 文件中。

这是我当前用于将 BufferedImage 保存到 zip 文件的代码,但我不知道如何将 BufferedImage 转换为 InputStream 以便可以将其保存到 zip 文件中。

如果可能的话,我需要直接从 RAM 中保存 BufferedImage 而不先将其保存到 HDD

byte[] buffer = new byte[1024];
try
{

    FileOutputStream outputStream = new FileOutputStream(PathName + imageData.getFileNumber() + ".zip");
    ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
    ZipEntry imageZipOutput = new ZipEntry(imageData.getFileNumber() + ".png");
    zipOutputStream.putNextEntry(imageZipOutput);

    //the BufferedImage is stored in imageData.getImage();
    //how would I parse the BufferedImage to the InputStream below without saving the png first but straight from RAM
    InputStream in = new InputStream();

    int len;
    while ((len = in.read(buffer)) > 0)
    {
        zipOutputStream.write(buffer, 0, len);
    }

    in.close();
    zipOutputStream.closeEntry();

    zipOutputStream.close();

}
4

1 回答 1

3

用于ImageIO.write(RenderedImage im,String formatName, OutputStream output)写入输出。为 OutputStream 参数传递 a ZipOutputStream

查看页面了解更多信息。

于 2013-04-07T02:28:00.607 回答