Why do I have to close the ZipOutputStream in a ce

2019-02-19 12:15发布

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.

2条回答
别忘想泡老子
2楼-- · 2019-02-19 12:53

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.)

查看更多
看我几分像从前
3楼-- · 2019-02-19 12:55

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())))) { ... }
查看更多
登录 后发表回答