开发者

How to save images into a zip file

I am attempting to read in a stream and save the read images to a zip file as this is going to be running over multiple days and would gener开发者_如何学Cate far too many individual files.

I now have an issue where I seem to be unable to save images into a zip file. The worker thread I have built for it is below. I am sure that the image is making it to the ImageIO.write. The result at the end however is a zip file of empty jpgs. I am wondering if perhaps ImageIO is not writing property to the ZipOutputStream.

Thanks for your help.

public class ZipSaveWorker implements Runnable{

    public static ZipOutputStream out=null;
    BufferedImage myImage;
    private static int counter=0;



    public void run() {
        ZipEntry entry=new ZipEntry("video"+counter+".jpg");
        counter++;
        try {
            out.putNextEntry(entry);
            ImageIO.write(myImage, ".jpg", out);

        } catch (IOException ex) {
            Logger.getLogger(ZipSaveWorker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ZipSaveWorker(BufferedImage image)
    {
        if (out==null)
        {
            try {
                out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(new File("images" + File.separator + "video.zip"))));
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ZipSaveWorker.class.getName()).log(Level.SEVERE, null, ex);
            }
            counter=0;
        }

        myImage=image;

    }

    public static void closeStream()
    {
        try {
            out.flush();
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(ZipSaveWorker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


}


The error in your code is in the line:

ImageIO.write(myImage, ".jpg", out);

It should be:

ImageIO.write(myImage, "jpg", out);

I am also not sure if you should call closeEntry() after every image has been written.

Consider what Stephen C writes, this code could result in corrupt zip files if the power was cut or the VM died. Consider making backups of the zip files a few times a week, maybe even a few times a day, to ensure your multiple day runs aren't completely ruined (I assume a run can be resumed).


I am attempting to read in a stream and save the read images to a zip file as this is going to be running over multiple days and would generate far too many individual files.

I'm not convinced by this reasoning. The actual number of files should not really matter that much. You might lose on average 1/2 a (file system) disk block per file, but with terabyte disc drives available for a couple of hundred dollars, that's probably insignificant.

But the more important problem is what happens if your application ... or the power goes off. If you write all of your images straight into a ZIP file, the chances are that you will end up with nothing but a corrupt ZIP file for a multi-day run. I expect that the ZIP file contents will be mostly recoverable, but only using some third party (non-Java) application.

If file system resources (disk space, number of inodes, whatever) are a realistic concern, then maybe you should write a script to run (say) once an hour and ZIP up the files that were written in the last hour and (maybe) put the ZIP file somewhere else.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜