Skip to content
Permalink
Browse files
Use try-with-resources.
  • Loading branch information
garydgregory committed May 5, 2022
1 parent c0602bd commit 505a8756a5a1199a36479226023e928708d66a2b
Showing 1 changed file with 18 additions and 19 deletions.
@@ -325,30 +325,29 @@ public void testDirectoryWithLongNameEndsWithSlash() throws IOException, Archive
// -----------------------
final String fileName = "/" + dirDirectory + "/" + subDir;
final File tarF = new File(rootPath + "/tar" + i + ".tar");
final OutputStream dest = Files.newOutputStream(tarF.toPath());
final TarArchiveOutputStream out = new TarArchiveOutputStream(new BufferedOutputStream(dest));
out.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
out.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

final File file = new File(rootPath, fileName);
final TarArchiveEntry entry = new TarArchiveEntry(file);
entry.setName(fileName);
out.putArchiveEntry(entry);
out.closeArchiveEntry();
out.flush();
out.close();
try (final OutputStream dest = Files.newOutputStream(tarF.toPath())) {
final TarArchiveOutputStream out = new TarArchiveOutputStream(new BufferedOutputStream(dest));
out.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
out.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

final File file = new File(rootPath, fileName);
final TarArchiveEntry entry = new TarArchiveEntry(file);
entry.setName(fileName);
out.putArchiveEntry(entry);
out.closeArchiveEntry();
out.flush();
}

// -----------------------
// untar these tars
// -----------------------
final InputStream is = Files.newInputStream(tarF.toPath());
final TarArchiveInputStream debInputStream = (TarArchiveInputStream) ArchiveStreamFactory.DEFAULT
.createArchiveInputStream("tar", is);
TarArchiveEntry outEntry;
while ((outEntry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
assertTrue(outEntry.getName().endsWith("/"));
try (final InputStream is = Files.newInputStream(tarF.toPath());
final TarArchiveInputStream debInputStream = (TarArchiveInputStream) ArchiveStreamFactory.DEFAULT.createArchiveInputStream("tar", is)) {
TarArchiveEntry outEntry;
while ((outEntry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
assertTrue(outEntry.getName(), outEntry.getName().endsWith("/"));
}
}
debInputStream.close();
}
}

0 comments on commit 505a875

Please sign in to comment.