You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/12/11 06:34:10 UTC

[commons-compress] branch master updated (0082648e -> 01bfe335)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git


    from 0082648e Javadoc
     new 11cf7943 Use try-with-resources
     new 01bfe335 Remove unused imports

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../compress/archivers/arj/ArjArchiveEntry.java    |   1 +
 .../compress/archivers/sevenz/AES256Options.java   |   1 +
 .../archivers/sevenz/SevenZArchiveEntry.java       |   2 +-
 .../archivers/sevenz/SevenZOutputFile.java         |   1 +
 .../commons/compress/archivers/zip/X000A_NTFS.java |   4 +-
 .../commons/compress/archivers/SevenZTestCase.java |   6 +-
 .../compress/archivers/sevenz/SevenZFileTest.java  |  11 +-
 .../archivers/sevenz/SevenZOutputFileTest.java     |   7 +-
 .../archivers/tar/TarArchiveInputStreamTest.java   |  93 ++---
 .../archivers/tar/TarArchiveOutputStreamTest.java  | 431 ++++++++++-----------
 .../commons/compress/archivers/tar/TarLister.java  |  20 +-
 .../compress/archivers/zip/DataDescriptorTest.java |   2 -
 .../commons/compress/utils/TimeUtilsTest.java      |  22 +-
 13 files changed, 288 insertions(+), 313 deletions(-)


[commons-compress] 02/02: Remove unused imports

Posted by gg...@apache.org.
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

commit 01bfe33566113d07f6ff4fce953222b09846dc99
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Dec 11 01:34:04 2022 -0500

    Remove unused imports
    
    No star imports
    Order imports
---
 .../compress/archivers/arj/ArjArchiveEntry.java    |  1 +
 .../compress/archivers/sevenz/AES256Options.java   |  1 +
 .../archivers/sevenz/SevenZArchiveEntry.java       |  2 +-
 .../archivers/sevenz/SevenZOutputFile.java         |  1 +
 .../commons/compress/archivers/zip/X000A_NTFS.java |  4 ++--
 .../commons/compress/archivers/SevenZTestCase.java |  6 ++++--
 .../compress/archivers/sevenz/SevenZFileTest.java  | 11 ++++++++++-
 .../archivers/sevenz/SevenZOutputFileTest.java     |  7 +++----
 .../compress/archivers/zip/DataDescriptorTest.java |  2 --
 .../commons/compress/utils/TimeUtilsTest.java      | 22 ++++++++++++++--------
 10 files changed, 37 insertions(+), 20 deletions(-)

diff --git a/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveEntry.java b/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveEntry.java
index 165e4855..5c966faf 100644
--- a/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveEntry.java
+++ b/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveEntry.java
@@ -19,6 +19,7 @@ package org.apache.commons.compress.archivers.arj;
 
 import java.io.File;
 import java.util.Date;
+
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.zip.ZipUtil;
 
diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256Options.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256Options.java
index ba23a135..fbfb00b6 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256Options.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256Options.java
@@ -20,6 +20,7 @@ package org.apache.commons.compress.archivers.sevenz;
 import java.security.GeneralSecurityException;
 import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
+
 import javax.crypto.Cipher;
 import javax.crypto.SecretKey;
 import javax.crypto.spec.IvParameterSpec;
diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZArchiveEntry.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZArchiveEntry.java
index 356628dd..70ff3b14 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZArchiveEntry.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZArchiveEntry.java
@@ -17,8 +17,8 @@
  */
 package org.apache.commons.compress.archivers.sevenz;
 
-import java.util.Arrays;
 import java.nio.file.attribute.FileTime;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
 import java.util.Iterator;
diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
index 14080640..89461f49 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
@@ -51,6 +51,7 @@ import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import java.util.stream.StreamSupport;
 import java.util.zip.CRC32;
+
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.utils.CountingOutputStream;
 import org.apache.commons.compress.utils.TimeUtils;
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/X000A_NTFS.java b/src/main/java/org/apache/commons/compress/archivers/zip/X000A_NTFS.java
index 2185edd9..af36e778 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/X000A_NTFS.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/X000A_NTFS.java
@@ -17,13 +17,13 @@
  */
 package org.apache.commons.compress.archivers.zip;
 
-import org.apache.commons.compress.utils.TimeUtils;
-
 import java.nio.file.attribute.FileTime;
 import java.util.Date;
 import java.util.Objects;
 import java.util.zip.ZipException;
 
+import org.apache.commons.compress.utils.TimeUtils;
+
 /**
  * NTFS extra field that was thought to store various attributes but
  * in reality only stores timestamps.
diff --git a/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java b/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java
index 704addc8..c25b4d2d 100644
--- a/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java
+++ b/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java
@@ -17,6 +17,10 @@
  */
 package org.apache.commons.compress.archivers;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -36,8 +40,6 @@ import org.junit.Assume;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
-import static org.junit.Assert.*;
-
 public class SevenZTestCase extends AbstractTestCase {
 
     private File output;
diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
index 94767466..90da9a3f 100644
--- a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
@@ -38,8 +38,17 @@ import java.nio.file.Path;
 import java.nio.file.attribute.FileTime;
 import java.security.NoSuchAlgorithmException;
 import java.time.Instant;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
 import java.util.function.Function;
+
 import javax.crypto.Cipher;
 
 import org.apache.commons.compress.AbstractTestCase;
diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java
index 53b16543..c3199040 100644
--- a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java
@@ -26,9 +26,6 @@ import static org.junit.Assert.fail;
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
 
-import org.apache.commons.compress.utils.TimeUtils;
-import org.junit.jupiter.api.Test;
-
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
@@ -36,8 +33,8 @@ import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.nio.file.attribute.FileTime;
 import java.time.Instant;
-import java.util.Arrays;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.Date;
@@ -47,6 +44,8 @@ import org.apache.commons.compress.AbstractTestCase;
 import org.apache.commons.compress.PasswordRequiredException;
 import org.apache.commons.compress.utils.ByteUtils;
 import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
+import org.apache.commons.compress.utils.TimeUtils;
+import org.junit.jupiter.api.Test;
 import org.tukaani.xz.LZMA2Options;
 
 public class SevenZOutputFileTest extends AbstractTestCase {
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/DataDescriptorTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/DataDescriptorTest.java
index 0b5596a8..9a602efb 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/DataDescriptorTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/DataDescriptorTest.java
@@ -27,12 +27,10 @@ import static org.junit.Assert.assertNotEquals;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.OutputStream;
 import java.nio.file.Files;
 import java.util.Arrays;
 
-import org.apache.commons.compress.utils.IOUtils;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
diff --git a/src/test/java/org/apache/commons/compress/utils/TimeUtilsTest.java b/src/test/java/org/apache/commons/compress/utils/TimeUtilsTest.java
index c65c93b4..d94c26fb 100644
--- a/src/test/java/org/apache/commons/compress/utils/TimeUtilsTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/TimeUtilsTest.java
@@ -17,20 +17,26 @@
  */
 package org.apache.commons.compress.utils;
 
-import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.Arguments;
-import org.junit.jupiter.params.provider.MethodSource;
+import static org.apache.commons.compress.utils.TimeUtils.HUNDRED_NANOS_PER_MILLISECOND;
+import static org.apache.commons.compress.utils.TimeUtils.WINDOWS_EPOCH_OFFSET;
+import static org.apache.commons.compress.utils.TimeUtils.ntfsTimeToDate;
+import static org.apache.commons.compress.utils.TimeUtils.ntfsTimeToFileTime;
+import static org.apache.commons.compress.utils.TimeUtils.toDate;
+import static org.apache.commons.compress.utils.TimeUtils.toFileTime;
+import static org.apache.commons.compress.utils.TimeUtils.toNtfsTime;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
 
 import java.nio.file.attribute.FileTime;
 import java.time.Instant;
 import java.util.Date;
 import java.util.stream.Stream;
 
-import static org.apache.commons.compress.utils.TimeUtils.*;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNull;
+import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
 
 public class TimeUtilsTest {
 


[commons-compress] 01/02: Use try-with-resources

Posted by gg...@apache.org.
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

commit 11cf79432a8ae9c85400def9d28a1b042ed7316d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Dec 11 01:29:20 2022 -0500

    Use try-with-resources
---
 .../archivers/tar/TarArchiveInputStreamTest.java   |  93 ++---
 .../archivers/tar/TarArchiveOutputStreamTest.java  | 431 ++++++++++-----------
 .../commons/compress/archivers/tar/TarLister.java  |  20 +-
 3 files changed, 251 insertions(+), 293 deletions(-)

diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
index 7120ea5c..0c07dd0a 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
@@ -105,22 +105,20 @@ public class TarArchiveInputStreamTest extends AbstractTestCase {
         final String name = "1234567890123456789012345678901234567890123456789"
             + "01234567890123456789012345678901234567890123456789"
             + "01234567890\u00e4";
-        final TarArchiveOutputStream tos =
-            new TarArchiveOutputStream(bos, encoding);
-        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
-        TarArchiveEntry t = new TarArchiveEntry(name);
-        t.setSize(1);
-        tos.putArchiveEntry(t);
-        tos.write(30);
-        tos.closeArchiveEntry();
-        tos.close();
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, encoding)) {
+            tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
+            TarArchiveEntry t = new TarArchiveEntry(name);
+            t.setSize(1);
+            tos.putArchiveEntry(t);
+            tos.write(30);
+            tos.closeArchiveEntry();
+        }
         final byte[] data = bos.toByteArray();
         final ByteArrayInputStream bis = new ByteArrayInputStream(data);
-        final TarArchiveInputStream tis =
-            new TarArchiveInputStream(bis, encoding);
-        t = tis.getNextTarEntry();
-        assertEquals(name, t.getName());
-        tis.close();
+        try (TarArchiveInputStream tis = new TarArchiveInputStream(bis, encoding)) {
+            TarArchiveEntry t = tis.getNextTarEntry();
+            assertEquals(name, t.getName());
+        }
     }
 
     /**
@@ -128,35 +126,31 @@ public class TarArchiveInputStreamTest extends AbstractTestCase {
      */
     @Test
     public void shouldConsumeArchiveCompletely() throws Exception {
-        final InputStream is = TarArchiveInputStreamTest.class
-            .getResourceAsStream("/archive_with_trailer.tar");
-        final TarArchiveInputStream tar = new TarArchiveInputStream(is);
-        while (tar.getNextTarEntry() != null) {
-            // just consume the archive
+        try (InputStream is = TarArchiveInputStreamTest.class.getResourceAsStream("/archive_with_trailer.tar");
+             TarArchiveInputStream tar = new TarArchiveInputStream(is)) {
+            while (tar.getNextTarEntry() != null) {
+                // just consume the archive
+            }
+            final byte[] expected = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n' };
+            final byte[] actual = new byte[expected.length];
+            is.read(actual);
+            assertArrayEquals(expected, actual);
         }
-        final byte[] expected = {
-            'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n'
-        };
-        final byte[] actual = new byte[expected.length];
-        is.read(actual);
-        assertArrayEquals(expected, actual);
-        tar.close();
     }
 
     @Test
     public void readsArchiveCompletely_COMPRESS245() {
-        try (InputStream is = TarArchiveInputStreamTest.class
-                .getResourceAsStream("/COMPRESS-245.tar.gz")) {
+        try (InputStream is = TarArchiveInputStreamTest.class.getResourceAsStream("/COMPRESS-245.tar.gz")) {
             final InputStream gin = new GZIPInputStream(is);
-            final TarArchiveInputStream tar = new TarArchiveInputStream(gin);
-            int count = 0;
-            TarArchiveEntry entry = tar.getNextTarEntry();
-            while (entry != null) {
-                count++;
-                entry = tar.getNextTarEntry();
+            try (TarArchiveInputStream tar = new TarArchiveInputStream(gin)) {
+                int count = 0;
+                TarArchiveEntry entry = tar.getNextTarEntry();
+                while (entry != null) {
+                    count++;
+                    entry = tar.getNextTarEntry();
+                }
+                assertEquals(31, count);
             }
-            assertEquals(31, count);
-            tar.close();
         } catch (final IOException e) {
             fail("COMPRESS-245: " + e.getMessage());
         }
@@ -185,22 +179,21 @@ public class TarArchiveInputStreamTest extends AbstractTestCase {
     @Test
     public void shouldReadBigGid() throws Exception {
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos);
-        tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
-        TarArchiveEntry t = new TarArchiveEntry("name");
-        t.setGroupId(4294967294L);
-        t.setSize(1);
-        tos.putArchiveEntry(t);
-        tos.write(30);
-        tos.closeArchiveEntry();
-        tos.close();
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos)) {
+            tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
+            TarArchiveEntry t = new TarArchiveEntry("name");
+            t.setGroupId(4294967294L);
+            t.setSize(1);
+            tos.putArchiveEntry(t);
+            tos.write(30);
+            tos.closeArchiveEntry();
+        }
         final byte[] data = bos.toByteArray();
         final ByteArrayInputStream bis = new ByteArrayInputStream(data);
-        final TarArchiveInputStream tis =
-            new TarArchiveInputStream(bis);
-        t = tis.getNextTarEntry();
-        assertEquals(4294967294L, t.getLongGroupId());
-        tis.close();
+        try (final TarArchiveInputStream tis = new TarArchiveInputStream(bis)) {
+            TarArchiveEntry t = tis.getNextTarEntry();
+            assertEquals(4294967294L, t.getLongGroupId());
+        }
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java
index 6a46becb..4beeb258 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java
@@ -66,15 +66,15 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
         final TarArchiveEntry sEntry = new TarArchiveEntry(file1, file1.getName());
         tarOut.putArchiveEntry(sEntry);
 
-        final InputStream in = Files.newInputStream(file1.toPath());
-        final byte[] buf = new byte[8192];
+        try (final InputStream in = Files.newInputStream(file1.toPath())) {
+            final byte[] buf = new byte[8192];
 
-        int read = 0;
-        while ((read = in.read(buf)) > 0) {
-            tarOut.write(buf, 0, read);
-        }
+            int read = 0;
+            while ((read = in.read(buf)) > 0) {
+                tarOut.write(buf, 0, read);
+            }
 
-        in.close();
+        }
         tarOut.closeArchiveEntry();
         tarOut.close();
 
@@ -102,8 +102,9 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
         final TarArchiveEntry t = new TarArchiveEntry("foo");
         t.setSize(0100000000000L);
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos);
-        assertThrows(IllegalArgumentException.class, () -> tos.putArchiveEntry(t));
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos)) {
+            assertThrows(IllegalArgumentException.class, () -> tos.putArchiveEntry(t));
+        }
     }
 
     @Test
@@ -117,16 +118,11 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
         // make sure header is written to byte array
         tos.write(new byte[10 * 1024]);
         final byte[] data = bos.toByteArray();
-        assertEquals(0x80,
-            data[TarConstants.NAMELEN
-                + TarConstants.MODELEN
-                + TarConstants.UIDLEN
-                + TarConstants.GIDLEN] & 0x80);
-        final TarArchiveInputStream tin =
-            new TarArchiveInputStream(new ByteArrayInputStream(data));
-        final TarArchiveEntry e = tin.getNextTarEntry();
-        assertEquals(0100000000000L, e.getSize());
-        tin.close();
+        assertEquals(0x80, data[TarConstants.NAMELEN + TarConstants.MODELEN + TarConstants.UIDLEN + TarConstants.GIDLEN] & 0x80);
+        try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+            final TarArchiveEntry e = tin.getNextTarEntry();
+            assertEquals(0100000000000L, e.getSize());
+        }
         // generates IOE because of unclosed entries.
         // However we don't really want to create such large entries.
         closeQuietly(tos);
@@ -150,11 +146,10 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
                     + TarConstants.UIDLEN
                     + TarConstants.GIDLEN, 12,
                     UTF_8));
-        final TarArchiveInputStream tin =
-            new TarArchiveInputStream(new ByteArrayInputStream(data));
-        final TarArchiveEntry e = tin.getNextTarEntry();
-        assertEquals(0100000000000L, e.getSize());
-        tin.close();
+        try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+            final TarArchiveEntry e = tin.getNextTarEntry();
+            assertEquals(0100000000000L, e.getSize());
+        }
         // generates IOE because of unclosed entries.
         // However we don't really want to create such large entries.
         closeQuietly(tos);
@@ -214,43 +209,41 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
 
     private byte[] writePaxHeader(final Map<String, String> m) throws Exception {
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII");
-        tos.writePaxHeaders(new TarArchiveEntry("x"), "foo", m);
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII")) {
+            tos.writePaxHeaders(new TarArchiveEntry("x"), "foo", m);
 
-        // add a dummy entry so data gets written
-        final TarArchiveEntry t = new TarArchiveEntry("foo");
-        t.setSize(10 * 1024);
-        tos.putArchiveEntry(t);
-        tos.write(new byte[10 * 1024]);
-        tos.closeArchiveEntry();
-        tos.close();
+            // add a dummy entry so data gets written
+            final TarArchiveEntry t = new TarArchiveEntry("foo");
+            t.setSize(10 * 1024);
+            tos.putArchiveEntry(t);
+            tos.write(new byte[10 * 1024]);
+            tos.closeArchiveEntry();
+        }
 
         return bos.toByteArray();
     }
 
     @Test
     public void testWriteLongFileNamePosixMode() throws Exception {
-        final String n = "01234567890123456789012345678901234567890123456789"
-            + "01234567890123456789012345678901234567890123456789"
-            + "01234567890123456789012345678901234567890123456789";
-        final TarArchiveEntry t =
-            new TarArchiveEntry(n);
+        // @formatter:off
+        final String n = "01234567890123456789012345678901234567890123456789" 
+                + "01234567890123456789012345678901234567890123456789"
+                + "01234567890123456789012345678901234567890123456789";
+        // @formatter:on
+        final TarArchiveEntry t = new TarArchiveEntry(n);
         t.setSize(10 * 1024);
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII");
-        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
-        tos.putArchiveEntry(t);
-        tos.write(new byte[10 * 1024]);
-        tos.closeArchiveEntry();
-        final byte[] data = bos.toByteArray();
-        assertEquals("160 path=" + n + "\n",
-            new String(data, 512, 160, UTF_8));
-        final TarArchiveInputStream tin =
-            new TarArchiveInputStream(new ByteArrayInputStream(data));
-        final TarArchiveEntry e = tin.getNextTarEntry();
-        assertEquals(n, e.getName());
-        tin.close();
-        tos.close();
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII")) {
+            tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
+            tos.putArchiveEntry(t);
+            tos.write(new byte[10 * 1024]);
+            tos.closeArchiveEntry();
+            final byte[] data = bos.toByteArray();
+            assertEquals("160 path=" + n + "\n", new String(data, 512, 160, UTF_8));
+            try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+                assertEquals(n, tin.getNextTarEntry().getName());
+            }
+        }
     }
 
     @Test
@@ -271,14 +264,13 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
                 + TarConstants.UIDLEN
                 + TarConstants.GIDLEN
                 + TarConstants.SIZELEN]);
-        final TarArchiveInputStream tin =
-            new TarArchiveInputStream(new ByteArrayInputStream(data));
-        final TarArchiveEntry e = tin.getNextTarEntry();
-        final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
-        cal.set(1969, 11, 31, 23, 59, 59);
-        cal.set(Calendar.MILLISECOND, 0);
-        assertEquals(cal.getTime(), e.getLastModifiedDate());
-        tin.close();
+        try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+            final TarArchiveEntry e = tin.getNextTarEntry();
+            final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
+            cal.set(1969, 11, 31, 23, 59, 59);
+            cal.set(Calendar.MILLISECOND, 0);
+            assertEquals(cal.getTime(), e.getLastModifiedDate());
+        }
         // generates IOE because of unclosed entries.
         // However we don't really want to create such large entries.
         closeQuietly(tos);
@@ -304,14 +296,13 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
                     + TarConstants.GIDLEN
                     + TarConstants.SIZELEN, 12,
                     UTF_8));
-        final TarArchiveInputStream tin =
-            new TarArchiveInputStream(new ByteArrayInputStream(data));
-        final TarArchiveEntry e = tin.getNextTarEntry();
-        final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
-        cal.set(1969, 11, 31, 23, 59, 59);
-        cal.set(Calendar.MILLISECOND, 0);
-        assertEquals(cal.getTime(), e.getLastModifiedDate());
-        tin.close();
+        try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+            final TarArchiveEntry e = tin.getNextTarEntry();
+            final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
+            cal.set(1969, 11, 31, 23, 59, 59);
+            cal.set(Calendar.MILLISECOND, 0);
+            assertEquals(cal.getTime(), e.getLastModifiedDate());
+        }
         // generates IOE because of unclosed entries.
         // However we don't really want to create such large entries.
         closeQuietly(tos);
@@ -322,14 +313,9 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
         final TarArchiveEntry t = new TarArchiveEntry("foo");
         t.setSize(Integer.MAX_VALUE);
         t.setModTime(-1000);
-        final TarArchiveOutputStream tos =
-            new TarArchiveOutputStream(new ByteArrayOutputStream());
-        try {
-            tos.putArchiveEntry(t);
-            fail("Should have generated RuntimeException");
-        } catch (final RuntimeException expected) {
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(new ByteArrayOutputStream())) {
+            assertThrows(RuntimeException.class, () -> tos.putArchiveEntry(t));
         }
-        tos.close();
     }
 
     @Test
@@ -338,20 +324,18 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
         final TarArchiveEntry t = new TarArchiveEntry(n);
         t.setSize(10 * 1024);
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos);
-        tos.setAddPaxHeadersForNonAsciiNames(true);
-        tos.putArchiveEntry(t);
-        tos.write(new byte[10 * 1024]);
-        tos.closeArchiveEntry();
-        tos.close();
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos)) {
+            tos.setAddPaxHeadersForNonAsciiNames(true);
+            tos.putArchiveEntry(t);
+            tos.write(new byte[10 * 1024]);
+            tos.closeArchiveEntry();
+        }
         final byte[] data = bos.toByteArray();
-        assertEquals("11 path=" + n + "\n",
-            new String(data, 512, 11, UTF_8));
-        final TarArchiveInputStream tin =
-            new TarArchiveInputStream(new ByteArrayInputStream(data));
-        final TarArchiveEntry e = tin.getNextTarEntry();
-        assertEquals(n, e.getName());
-        tin.close();
+        assertEquals("11 path=" + n + "\n", new String(data, 512, 11, UTF_8));
+        try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+            final TarArchiveEntry e = tin.getNextTarEntry();
+            assertEquals(n, e.getName());
+        }
     }
 
     @Test
@@ -361,20 +345,18 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
         t.setSize(10 * 1024);
         t.setLinkName(n);
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos);
-        tos.setAddPaxHeadersForNonAsciiNames(true);
-        tos.putArchiveEntry(t);
-        tos.write(new byte[10 * 1024]);
-        tos.closeArchiveEntry();
-        tos.close();
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos)) {
+            tos.setAddPaxHeadersForNonAsciiNames(true);
+            tos.putArchiveEntry(t);
+            tos.write(new byte[10 * 1024]);
+            tos.closeArchiveEntry();
+        }
         final byte[] data = bos.toByteArray();
-        assertEquals("15 linkpath=" + n + "\n",
-            new String(data, 512, 15, UTF_8));
-        final TarArchiveInputStream tin =
-            new TarArchiveInputStream(new ByteArrayInputStream(data));
-        final TarArchiveEntry e = tin.getNextTarEntry();
-        assertEquals(n, e.getLinkName());
-        tin.close();
+        assertEquals("15 linkpath=" + n + "\n", new String(data, 512, 15, UTF_8));
+        try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+            final TarArchiveEntry e = tin.getNextTarEntry();
+            assertEquals(n, e.getLinkName());
+        }
     }
 
     /**
@@ -394,24 +376,21 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
     }
 
     private void testRoundtripWith67CharFileName(final int mode) throws Exception {
-        final String n = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
-            + "AAAAAAA";
+        final String n = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
         assertEquals(67, n.length());
         final TarArchiveEntry t = new TarArchiveEntry(n);
         t.setSize(10 * 1024);
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII");
-        tos.setLongFileMode(mode);
-        tos.putArchiveEntry(t);
-        tos.write(new byte[10 * 1024]);
-        tos.closeArchiveEntry();
-        tos.close();
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII")) {
+            tos.setLongFileMode(mode);
+            tos.putArchiveEntry(t);
+            tos.write(new byte[10 * 1024]);
+            tos.closeArchiveEntry();
+        }
         final byte[] data = bos.toByteArray();
-        final TarArchiveInputStream tin =
-            new TarArchiveInputStream(new ByteArrayInputStream(data));
-        final TarArchiveEntry e = tin.getNextTarEntry();
-        assertEquals(n, e.getName());
-        tin.close();
+        try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+            assertEquals(n, tin.getNextTarEntry().getName());
+        }
     }
 
     @Test
@@ -423,11 +402,11 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
         try {
             final TarArchiveEntry t = new TarArchiveEntry(n);
             final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-            final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII");
-            tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_ERROR);
-            tos.putArchiveEntry(t);
-            tos.closeArchiveEntry();
-            tos.close();
+            try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII")) {
+                tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_ERROR);
+                tos.putArchiveEntry(t);
+                tos.closeArchiveEntry();
+            }
 
             fail("Truncated name didn't throw an exception");
         } catch (final RuntimeException e) {
@@ -442,18 +421,17 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
             + "01234567890123456789012345678901234567890123456789/";
         final TarArchiveEntry t = new TarArchiveEntry(n);
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII");
-        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_TRUNCATE);
-        tos.putArchiveEntry(t);
-        tos.closeArchiveEntry();
-        tos.close();
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII")) {
+            tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_TRUNCATE);
+            tos.putArchiveEntry(t);
+            tos.closeArchiveEntry();
+        }
         final byte[] data = bos.toByteArray();
-        final TarArchiveInputStream tin =
-            new TarArchiveInputStream(new ByteArrayInputStream(data));
-        final TarArchiveEntry e = tin.getNextTarEntry();
-        assertEquals("Entry name", n.substring(0, TarConstants.NAMELEN) + "/", e.getName());
-        assertTrue("The entry is not a directory", e.isDirectory());
-        tin.close();
+        try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+            final TarArchiveEntry e = tin.getNextTarEntry();
+            assertEquals("Entry name", n.substring(0, TarConstants.NAMELEN) + "/", e.getName());
+            assertTrue("The entry is not a directory", e.isDirectory());
+        }
     }
 
     /**
@@ -478,18 +456,17 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
             + "01234567890123456789012345678901234567890123456789/";
         final TarArchiveEntry t = new TarArchiveEntry(n);
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII");
-        tos.setLongFileMode(mode);
-        tos.putArchiveEntry(t);
-        tos.closeArchiveEntry();
-        tos.close();
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII")) {
+            tos.setLongFileMode(mode);
+            tos.putArchiveEntry(t);
+            tos.closeArchiveEntry();
+        }
         final byte[] data = bos.toByteArray();
-        final TarArchiveInputStream tin =
-            new TarArchiveInputStream(new ByteArrayInputStream(data));
-        final TarArchiveEntry e = tin.getNextTarEntry();
-        assertEquals(n, e.getName());
-        assertTrue(e.isDirectory());
-        tin.close();
+        try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+            final TarArchiveEntry e = tin.getNextTarEntry();
+            assertEquals(n, e.getName());
+            assertTrue(e.isDirectory());
+        }
     }
 
     /**
@@ -500,18 +477,17 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
         final String n = "f\u00f6\u00f6/";
         final TarArchiveEntry t = new TarArchiveEntry(n);
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos);
-        tos.setAddPaxHeadersForNonAsciiNames(true);
-        tos.putArchiveEntry(t);
-        tos.closeArchiveEntry();
-        tos.close();
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos)) {
+            tos.setAddPaxHeadersForNonAsciiNames(true);
+            tos.putArchiveEntry(t);
+            tos.closeArchiveEntry();
+        }
         final byte[] data = bos.toByteArray();
-        final TarArchiveInputStream tin =
-            new TarArchiveInputStream(new ByteArrayInputStream(data));
-        final TarArchiveEntry e = tin.getNextTarEntry();
-        assertEquals(n, e.getName());
-        assertTrue(e.isDirectory());
-        tin.close();
+        try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+            final TarArchiveEntry e = tin.getNextTarEntry();
+            assertEquals(n, e.getName());
+            assertTrue(e.isDirectory());
+        }
     }
 
     /**
@@ -522,18 +498,17 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
         final String n = "f\u00f6\u00f6\u00dc";
         final TarArchiveEntry t = new TarArchiveEntry(n);
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos);
-        tos.setAddPaxHeadersForNonAsciiNames(true);
-        tos.putArchiveEntry(t);
-        tos.closeArchiveEntry();
-        tos.close();
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos)) {
+            tos.setAddPaxHeadersForNonAsciiNames(true);
+            tos.putArchiveEntry(t);
+            tos.closeArchiveEntry();
+        }
         final byte[] data = bos.toByteArray();
-        final TarArchiveInputStream tin =
-            new TarArchiveInputStream(new ByteArrayInputStream(data));
-        final TarArchiveEntry e = tin.getNextTarEntry();
-        assertEquals(n, e.getName());
-        assertFalse(e.isDirectory());
-        tin.close();
+        try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+            final TarArchiveEntry e = tin.getNextTarEntry();
+            assertEquals(n, e.getName());
+            assertFalse(e.isDirectory());
+        }
     }
 
     /**
@@ -549,11 +524,11 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
 
         try {
             final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-            final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII");
-            tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_ERROR);
-            tos.putArchiveEntry(entry);
-            tos.closeArchiveEntry();
-            tos.close();
+            try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII")) {
+                tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_ERROR);
+                tos.putArchiveEntry(entry);
+                tos.closeArchiveEntry();
+            }
 
             fail("Truncated link name didn't throw an exception");
         } catch (final RuntimeException e) {
@@ -570,17 +545,17 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
         entry.setLinkName(linkname);
 
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII");
-        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_TRUNCATE);
-        tos.putArchiveEntry(entry);
-        tos.closeArchiveEntry();
-        tos.close();
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII")) {
+            tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_TRUNCATE);
+            tos.putArchiveEntry(entry);
+            tos.closeArchiveEntry();
+        }
 
         final byte[] data = bos.toByteArray();
-        final TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data));
-        final TarArchiveEntry e = tin.getNextTarEntry();
-        assertEquals("Link name", linkname.substring(0, TarConstants.NAMELEN), e.getLinkName());
-        tin.close();
+        try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+            final TarArchiveEntry e = tin.getNextTarEntry();
+            assertEquals("Link name", linkname.substring(0, TarConstants.NAMELEN), e.getLinkName());
+        }
     }
 
     /**
@@ -610,19 +585,19 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
         entry.setLinkName(linkname);
 
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII");
-        tos.setLongFileMode(mode);
-        tos.putArchiveEntry(entry);
-        tos.closeArchiveEntry();
-        tos.close();
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII")) {
+            tos.setLongFileMode(mode);
+            tos.putArchiveEntry(entry);
+            tos.closeArchiveEntry();
+        }
 
         final byte[] data = bos.toByteArray();
-        final TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data));
-        final TarArchiveEntry e = tin.getNextTarEntry();
-        assertEquals("Entry name", "test", e.getName());
-        assertEquals("Link name", linkname, e.getLinkName());
-        assertTrue("The entry is not a symbolic link", e.isSymbolicLink());
-        tin.close();
+        try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+            final TarArchiveEntry e = tin.getNextTarEntry();
+            assertEquals("Entry name", "test", e.getName());
+            assertEquals("Link name", linkname, e.getLinkName());
+            assertTrue("The entry is not a symbolic link", e.isSymbolicLink());
+        }
     }
 
     @SuppressWarnings("deprecation")
@@ -709,29 +684,28 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
 
 	@Test
 	public void testPutGlobalPaxHeaderEntry() throws IOException {
+        final String x = "If at first you don't succeed, give up";
 		final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-		final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos);
-		final int pid = 73;
-		final int globCount = 1;
-		final byte lfPaxGlobalExtendedHeader = TarConstants.LF_PAX_GLOBAL_EXTENDED_HEADER;
-		final TarArchiveEntry globalHeader = new TarArchiveEntry("/tmp/GlobalHead." + pid + "." + globCount,
-				lfPaxGlobalExtendedHeader);
-		globalHeader.addPaxHeader("SCHILLY.xattr.user.org.apache.weasels", "global-weasels");
-		tos.putArchiveEntry(globalHeader);
-		TarArchiveEntry entry = new TarArchiveEntry("message");
-		final String x = "If at first you don't succeed, give up";
-		entry.setSize(x.length());
-		tos.putArchiveEntry(entry);
-		tos.write(x.getBytes());
-		tos.closeArchiveEntry();
-		entry = new TarArchiveEntry("counter-message");
-		final String y = "Nothing succeeds like excess";
-		entry.setSize(y.length());
-		entry.addPaxHeader("SCHILLY.xattr.user.org.apache.weasels.species", "unknown");
-		tos.putArchiveEntry(entry);
-		tos.write(y.getBytes());
-		tos.closeArchiveEntry();
-		tos.close();
+        try (TarArchiveOutputStream tos = new TarArchiveOutputStream(bos)) {
+            final int pid = 73;
+            final int globCount = 1;
+            final byte lfPaxGlobalExtendedHeader = TarConstants.LF_PAX_GLOBAL_EXTENDED_HEADER;
+            final TarArchiveEntry globalHeader = new TarArchiveEntry("/tmp/GlobalHead." + pid + "." + globCount, lfPaxGlobalExtendedHeader);
+            globalHeader.addPaxHeader("SCHILLY.xattr.user.org.apache.weasels", "global-weasels");
+            tos.putArchiveEntry(globalHeader);
+            TarArchiveEntry entry = new TarArchiveEntry("message");
+            entry.setSize(x.length());
+            tos.putArchiveEntry(entry);
+            tos.write(x.getBytes());
+            tos.closeArchiveEntry();
+            entry = new TarArchiveEntry("counter-message");
+            final String y = "Nothing succeeds like excess";
+            entry.setSize(y.length());
+            entry.addPaxHeader("SCHILLY.xattr.user.org.apache.weasels.species", "unknown");
+            tos.putArchiveEntry(entry);
+            tos.write(y.getBytes());
+            tos.closeArchiveEntry();
+        }
 		final TarArchiveInputStream in = new TarArchiveInputStream(new ByteArrayInputStream(bos.toByteArray()));
 		TarArchiveEntry entryIn = in.getNextTarEntry();
 		assertNotNull(entryIn);
@@ -775,14 +749,12 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
         // do I still have the correct modification date?
         // let a second elapse so we don't get the current time
         Thread.sleep(1000);
-        final TarArchiveInputStream tarIn = new TarArchiveInputStream(
-            new ByteArrayInputStream(archive2));
-        final ArchiveEntry nextEntry = tarIn.getNextEntry();
-        assertEquals(longFileName, nextEntry.getName());
-        // tar archive stores modification time to second granularity only (floored)
-        assertEquals(modificationDate.getTime() / 1000,
-            nextEntry.getLastModifiedDate().getTime() / 1000);
-        tarIn.close();
+        try (TarArchiveInputStream tarIn = new TarArchiveInputStream(new ByteArrayInputStream(archive2))) {
+            final ArchiveEntry nextEntry = tarIn.getNextEntry();
+            assertEquals(longFileName, nextEntry.getName());
+            // tar archive stores modification time to second granularity only (floored)
+            assertEquals(modificationDate.getTime() / 1000, nextEntry.getLastModifiedDate().getTime() / 1000);
+        }
     }
 
     @Test
@@ -795,19 +767,18 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase {
         assertThrows(IllegalArgumentException.class, () -> tos.putArchiveEntry(t));
     }
 
-    private static byte[] createTarArchiveContainingOneDirectory(final String fname,
-        final Date modificationDate) throws IOException {
+    private static byte[] createTarArchiveContainingOneDirectory(final String fname, final Date modificationDate) throws IOException {
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        final TarArchiveOutputStream tarOut = new TarArchiveOutputStream(baos, 1024);
-        tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
-        final TarArchiveEntry tarEntry = new TarArchiveEntry("d");
-        tarEntry.setModTime(modificationDate);
-        tarEntry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE);
-        tarEntry.setModTime(modificationDate.getTime());
-        tarEntry.setName(fname);
-        tarOut.putArchiveEntry(tarEntry);
-        tarOut.closeArchiveEntry();
-        tarOut.close();
+        try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(baos, 1024)) {
+            tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
+            final TarArchiveEntry tarEntry = new TarArchiveEntry("d");
+            tarEntry.setModTime(modificationDate);
+            tarEntry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE);
+            tarEntry.setModTime(modificationDate.getTime());
+            tarEntry.setName(fname);
+            tarOut.putArchiveEntry(tarEntry);
+            tarOut.closeArchiveEntry();
+        }
 
         return baos.toByteArray();
     }
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarLister.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarLister.java
index 3bfb5c2f..3a25475f 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarLister.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarLister.java
@@ -43,20 +43,14 @@ public final class TarLister {
         if (!f.isFile()) {
             System.err.println(f + " doesn't exist or is a directory");
         }
-        final InputStream fis = new BufferedInputStream(Files.newInputStream(f.toPath()));
-        final TarArchiveInputStream ais;
-        if (args.length > 1) {
-            ais = new TarArchiveInputStream(fis, args[1]);
-        } else {
-            ais = new TarArchiveInputStream(fis);
-        }
-        System.out.println("Created " + ais);
-        TarArchiveEntry ae;
-        while((ae=ais.getNextTarEntry()) != null){
-            log(ae);
+        try (InputStream fis = new BufferedInputStream(Files.newInputStream(f.toPath()));
+                TarArchiveInputStream ais = args.length > 1 ? new TarArchiveInputStream(fis, args[1]) : new TarArchiveInputStream(fis)) {
+            System.out.println("Created " + ais);
+            TarArchiveEntry ae;
+            while ((ae = ais.getNextTarEntry()) != null) {
+                log(ae);
+            }
         }
-        ais.close();
-        fis.close();
     }
 
     private static void usage() {