From 0f0876d2bf0ba067525e45a0d225936d155d4542 Mon Sep 17 00:00:00 2001 From: Daniel Santos <dougniel@gmail.com> Date: Fri, 23 Dec 2022 12:53:29 +1100 Subject: [PATCH] =?UTF-8?q?fix(compress):=20=F0=9F=90=9B=20`null`=20appear?= =?UTF-8?q?s=20on=20entry=20paths=20on=20a=20wildcard=20compress?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix #22 --- src/main/java/nc/opt/util/J7zip.java | 2 +- src/test/java/nc/opt/uil/j7zip/Tests.java | 34 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/java/nc/opt/util/J7zip.java b/src/main/java/nc/opt/util/J7zip.java index 4b6916a..7652e68 100644 --- a/src/main/java/nc/opt/util/J7zip.java +++ b/src/main/java/nc/opt/util/J7zip.java @@ -83,7 +83,7 @@ public static void compress(String name, String password, String... files) throw } private static void addToArchiveCompression(SevenZOutputFile out, File file, String dir) throws IOException { - String name = dir + File.separator + file.getName(); + String name = (dir != null ? dir + File.separator : "") + file.getName(); if (file.isFile()) { SevenZArchiveEntry entry = out.createArchiveEntry(file, name); out.putArchiveEntry(entry); diff --git a/src/test/java/nc/opt/uil/j7zip/Tests.java b/src/test/java/nc/opt/uil/j7zip/Tests.java index 4e30ebc..195df69 100644 --- a/src/test/java/nc/opt/uil/j7zip/Tests.java +++ b/src/test/java/nc/opt/uil/j7zip/Tests.java @@ -111,6 +111,40 @@ public void testListArrchiveFiles() throws IOException { Paths.get("target/archive.7z").toFile().delete(); } + @Test + public void testCompressDirectory() throws IOException { + // compress + int exitCode = new CommandLine(new J7zip()).execute(new String[] { "a", "target/archive.7z", "src" }); + restoreStreams(); + + String err = new String(this.err.toByteArray()); + if (!err.isEmpty()) { + System.err.println(err); + throw new AssertionFailedError(err); + } + assertEquals(0, exitCode); + + // list contents of file + setUpStreams(); + exitCode = new CommandLine(new J7zip()).execute(new String[] { "l", "target/archive.7z" }); + restoreStreams(); + + err = new String(this.err.toByteArray()); + if (!err.isEmpty()) { + System.err.println(err); + throw new AssertionFailedError(err); + } + assertEquals(0, exitCode); + + String[] lines = new String(this.out.toByteArray()).split("\n"); + assertNotEquals(0, lines.length); + for (String entry : lines) { + assertTrue(entry.startsWith("src/"), "entry not starts with src/ : " + entry); + } + + Paths.get("target/archive.7z").toFile().delete(); + } + /** * Test with a archive file compressed by 7z tool */