Why do I have to close the ZipOutputStream in a certain way in this situation?

孤街醉人 提交于 2019-12-19 04:22:44

问题


I have two examples :

Example 1:

try (ByteArrayOutputStream baous = new ByteArrayOutputStream();     
    FileOutputStream fouscrx = new FileOutputStream(new File(output, "example"))) {
        try (ZipOutputStream zous = new ZipOutputStream(baous)) {
            for (File file: files) {
                try (FileInputStream fis = new FileInputStream(file)) {
                    ZipEntry zipEntry = new ZipEntry(file.getPath().substring(output.getPath().length() + 1));
                    zous.putNextEntry(zipEntry);
                    byte[] bytes = new byte[2048];
                    int length;
                    while ((length = fis.read(bytes)) >= 0) {
                        zous.write(bytes, 0, length);
                    }
                    zous.closeEntry();
                }
            }
        }
        baous.writeTo(fouscrx);
    } catch (FileNotFoundException ex) {} catch (IOException ex) {}

Example 2:

try (ByteArrayOutputStream baous = new ByteArrayOutputStream();
          ZipOutputStream zous = new ZipOutputStream(baous);
       FileOutputStream fouscrx = new FileOutputStream(new File(output, "example"))) {
            for (File file: files) {
                try (FileInputStream fis = new FileInputStream(file)) {
                    ZipEntry zipEntry = new ZipEntry(file.getPath().substring(output.getPath().length() + 1));
                    zous.putNextEntry(zipEntry);
                    byte[] bytes = new byte[2048];
                    int length;
                    while ((length = fis.read(bytes)) >= 0) {
                        zous.write(bytes, 0, length);
                    }
                    zous.closeEntry();
                }
            }
            baous.writeTo(fouscrx);
        } catch (FileNotFoundException ex) {} catch (IOException ex) {}

The second example doesn't work as I would like it to do. What I mean is that the file content is not empty but it' s as if the zip file was corrupted.

I would like you to tell me how come the first example does not work.


回答1:


ZipOutputStream has to do several operations at the end of the stream to finish the zip file, so it's necessary for it to be closed properly. (Generally speaking, pretty much every stream should be closed properly, just as good practice.)




回答2:


Well, it looks like the try-with-resources autoclosing order is important, and the ZipOutputStream has to be closed first when unwinding things. Autoclosing happens in reverse order of their creation in this context.

What happens if you reorder your second example so the ZipOutputStream is after the FileOutputStream? (Though placing the ZipOutputStream in its own try-catch block is clearer code if you ask me. We separate out the related and unrelated streams and handle the auto-closing in an easily readable manner.)

UPDATE

FWIW, this is the sort of idiom I have used in the past when streaming a zip into a buffered output stream:

try (final ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(
                    new FileOutputStream(zipFile.toString())))) { ... }


来源:https://stackoverflow.com/questions/28636416/why-do-i-have-to-close-the-zipoutputstream-in-a-certain-way-in-this-situation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!