I am tring to compress a list of Images into a single zip file.
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.
Thank you for everything.