在 try-wtth-resources 块中使用 GZIPOutputStream 对象时是否需要调用 finish()

Do I need to call finish() when using a GZIPOutputStream object inside try-wtth-resources block

在 try-with-resources [autoclosable] 块中使用 GZipOutputStream 时,我是否需要在完成资源后显式调用 finish()

不,离开 try-with-resources 块时调用的 close() 方法会调用 finish(),因此您不需要手动执行此操作。

您可以在其 source code (GZipOutputStream does not override close(), so the close() method from its super class DeflaterOutptuStream is used). Also the JavaDoc of close() states that it "Writes remaining compressed data to the output stream", which is the same as finish() 中看到此操作的文档。