在Zip中压缩列表

时间:2022-12-09 21:23:20

I am tring to compress a list of Images into a single zip file.

我想将一个图像列表压缩成一个zip文件。

public void compressZip(List<Image> lstImage) {
    //Abrimos una ventana JFileChooser
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int seleccion = fileChooser.showSaveDialog(laminaComicPrincipal);
    if (seleccion == JFileChooser.APPROVE_OPTION)
    {
       File fichero = fileChooser.getSelectedFile();
       try {
            ZipOutputStream os = new ZipOutputStream(new FileOutputStream(fichero.getAbsolutePath()+"file.zip"));
            int numeroImagen = 0;
            for(Image imagen: lstImage){

                ZipEntry entrada = new ZipEntry(numeroImagen+".jpg");
                os.putNextEntry(entrada);

                ImageInputStream fis = ImageIO.createImageInputStream(imagen);//THis sentences return null
                byte [] buffer = new byte[1024];
                int leido=0;
                while (0 < (leido=fis.read(buffer))){   //FAIL SENTENCES --> fis=null
                   os.write(buffer,0,leido);
                }

                fis.close();
                os.closeEntry();
                numeroImagen++;
            }
            os.close();
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    }

ImageIO.createImageInputStream(image n) returns null. What's the problem?? If I save the Images in HDD first, and use FileInputStream to read the files, will it work? but I prefer not to create temporally files.

ImageIO.createImageInputStream(image n)返回null。有什么问题??如果我先将图像保存在HDD中,并使用FileInputStream来读取文件,它会起作用吗?但我不想创建临时文件。

Thank you for everything.

谢谢你为我做的一切。

1 个解决方案

#1


3  

If you have a look at the JavaDocs you will find your answers

如果您查看JavaDocs,您将找到答案

Returns an ImageInputStream that will take its input from the given Object. The set of ImageInputStreamSpis registered with the IIORegistry class is queried and the first one that is able to take input from the supplied object is used to create the returned ImageInputStream. If no suitable ImageInputStreamSpi exists, null is returned.

返回一个ImageInputStream,它将从给定的Object获取其输入。查询使用IIORegistry类注册的ImageInputStreamSpis集合,并且第一个能够从提供的对象获取输入的集合用于创建返回的ImageInputStream。如果不存在合适的ImageInputStreamSpi,则返回null。

And

input - an Object to be used as an input source, such as a File, readable RandomAccessFile, or InputStream.

input - 要用作输入源的Object,例如File,可读RandomAccessFile或InputStream。

(Emphasis added by me)

(我强调的重点)

So basically, the method is expecting a File or InputStream, not an Image. If you convert your Image to RenderedImage (ie a BufferedImage), then you can just use ImageIO.write to write the image directly to the ZipOutputStream

所以基本上,该方法需要File或InputStream,而不是Image。如果将Image转换为RenderedImage(即BufferedImage),则可以使用ImageIO.write将图像直接写入ZipOutputStream

Then your question becomes How to convert Image to BufferedImage in Java?

然后你的问题变成如何在Java中将图像转换为BufferedImage?

#1


3  

If you have a look at the JavaDocs you will find your answers

如果您查看JavaDocs,您将找到答案

Returns an ImageInputStream that will take its input from the given Object. The set of ImageInputStreamSpis registered with the IIORegistry class is queried and the first one that is able to take input from the supplied object is used to create the returned ImageInputStream. If no suitable ImageInputStreamSpi exists, null is returned.

返回一个ImageInputStream,它将从给定的Object获取其输入。查询使用IIORegistry类注册的ImageInputStreamSpis集合,并且第一个能够从提供的对象获取输入的集合用于创建返回的ImageInputStream。如果不存在合适的ImageInputStreamSpi,则返回null。

And

input - an Object to be used as an input source, such as a File, readable RandomAccessFile, or InputStream.

input - 要用作输入源的Object,例如File,可读RandomAccessFile或InputStream。

(Emphasis added by me)

(我强调的重点)

So basically, the method is expecting a File or InputStream, not an Image. If you convert your Image to RenderedImage (ie a BufferedImage), then you can just use ImageIO.write to write the image directly to the ZipOutputStream

所以基本上,该方法需要File或InputStream,而不是Image。如果将Image转换为RenderedImage(即BufferedImage),则可以使用ImageIO.write将图像直接写入ZipOutputStream

Then your question becomes How to convert Image to BufferedImage in Java?

然后你的问题变成如何在Java中将图像转换为BufferedImage?