This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
The following commit(s) were added to refs/heads/master by this push: new f1e4d77d Use try-with-resources f1e4d77d is described below commit f1e4d77d7239715b9d789416214852a2c7613dc1 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Tue Jan 24 14:57:46 2023 -0500 Use try-with-resources --- .../commons/compress/archivers/JarTestCase.java | 51 ++++++++++------------ 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/src/test/java/org/apache/commons/compress/archivers/JarTestCase.java b/src/test/java/org/apache/commons/compress/archivers/JarTestCase.java index 00742b28..ca059682 100644 --- a/src/test/java/org/apache/commons/compress/archivers/JarTestCase.java +++ b/src/test/java/org/apache/commons/compress/archivers/JarTestCase.java @@ -36,19 +36,16 @@ public final class JarTestCase extends AbstractTestCase { final File file1 = getFile("test1.xml"); final File file2 = getFile("test2.xml"); - final OutputStream out = Files.newOutputStream(output.toPath()); - - final ArchiveOutputStream os = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream("jar", out); - - os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml")); - Files.copy(file1.toPath(), os); - os.closeArchiveEntry(); - - os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml")); - Files.copy(file2.toPath(), os); - os.closeArchiveEntry(); - - os.close(); + try (OutputStream out = Files.newOutputStream(output.toPath()); + ArchiveOutputStream os = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream("jar", out)) { + os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml")); + Files.copy(file1.toPath(), os); + os.closeArchiveEntry(); + + os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml")); + Files.copy(file2.toPath(), os); + os.closeArchiveEntry(); + } } @@ -78,24 +75,22 @@ public final class JarTestCase extends AbstractTestCase { @Test public void testJarUnarchiveAll() throws Exception { final File input = getFile("bla.jar"); - final InputStream is = Files.newInputStream(input.toPath()); - final ArchiveInputStream in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("jar", is); - - ArchiveEntry entry = in.getNextEntry(); - while (entry != null) { - final File archiveEntry = new File(dir, entry.getName()); - archiveEntry.getParentFile().mkdirs(); - if(entry.isDirectory()){ - archiveEntry.mkdir(); + try (InputStream is = Files.newInputStream(input.toPath()); + final ArchiveInputStream in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("jar", is)) { + + ArchiveEntry entry = in.getNextEntry(); + while (entry != null) { + final File archiveEntry = new File(dir, entry.getName()); + archiveEntry.getParentFile().mkdirs(); + if (entry.isDirectory()) { + archiveEntry.mkdir(); + entry = in.getNextEntry(); + continue; + } + Files.copy(in, archiveEntry.toPath()); entry = in.getNextEntry(); - continue; } - Files.copy(in, archiveEntry.toPath()); - entry = in.getNextEntry(); } - - in.close(); - is.close(); } }