From 7186e4d604f30af944355f0acdb10ce8db1bd27d Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Wed, 31 May 2023 09:40:20 -0300 Subject: [PATCH 1/4] draft 1 --- libraries-apache-commons-2/README.md | 7 +++ libraries-apache-commons-2/log4j.properties | 1 + libraries-apache-commons-2/pom.xml | 39 +++++++++++++ .../baeldung/commons/untar/TarExtractor.java | 39 +++++++++++++ .../commons/untar/impl/TarExtractorAnt.java | 37 ++++++++++++ .../impl/TarExtractorCommonsCompress.java | 38 ++++++++++++ .../commons/untar/impl/TarExtractorVfs.java | 54 ++++++++++++++++++ .../src/main/resources/logback.xml | 13 +++++ .../com/baeldung/commons/untar/Resources.java | 8 +++ .../untar/TarExtractorAntUnitTest.java | 37 ++++++++++++ .../TarExtractorCommonsCompressUnitTest.java | 37 ++++++++++++ .../untar/TarExtractorVfsUnitTest.java | 37 ++++++++++++ .../src/test/resources/untar/test.tar | Bin 0 -> 5632 bytes .../src/test/resources/untar/test.tar.gz | Bin 0 -> 211 bytes libraries-apache-commons/README.md | 3 +- pom.xml | 2 + 16 files changed, 351 insertions(+), 1 deletion(-) create mode 100644 libraries-apache-commons-2/README.md create mode 100644 libraries-apache-commons-2/log4j.properties create mode 100644 libraries-apache-commons-2/pom.xml create mode 100644 libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/TarExtractor.java create mode 100644 libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorAnt.java create mode 100644 libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorCommonsCompress.java create mode 100644 libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorVfs.java create mode 100644 libraries-apache-commons-2/src/main/resources/logback.xml create mode 100644 libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/Resources.java create mode 100644 libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorAntUnitTest.java create mode 100644 libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorCommonsCompressUnitTest.java create mode 100644 libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorVfsUnitTest.java create mode 100644 libraries-apache-commons-2/src/test/resources/untar/test.tar create mode 100644 libraries-apache-commons-2/src/test/resources/untar/test.tar.gz diff --git a/libraries-apache-commons-2/README.md b/libraries-apache-commons-2/README.md new file mode 100644 index 0000000000..7f91a737d5 --- /dev/null +++ b/libraries-apache-commons-2/README.md @@ -0,0 +1,7 @@ +## Apache Commons + +This module contains articles about Apache Commons libraries. + +### Relevant articles + +- More articles: [[<--prev]](../libraries-apache-commons) \ No newline at end of file diff --git a/libraries-apache-commons-2/log4j.properties b/libraries-apache-commons-2/log4j.properties new file mode 100644 index 0000000000..2173c5d96f --- /dev/null +++ b/libraries-apache-commons-2/log4j.properties @@ -0,0 +1 @@ +log4j.rootLogger=INFO, stdout diff --git a/libraries-apache-commons-2/pom.xml b/libraries-apache-commons-2/pom.xml new file mode 100644 index 0000000000..c8392ac561 --- /dev/null +++ b/libraries-apache-commons-2/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + libraries-apache-commons-2 + libraries-apache-commons-2 + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.apache.commons + commons-compress + ${commons-compress.version} + + + org.apache.ant + ant + ${ant.version} + + + org.apache.commons + commons-vfs2 + ${commons-vfs2.version} + + + + + 1.23.0 + 1.10.13 + 2.9.0 + + + diff --git a/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/TarExtractor.java b/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/TarExtractor.java new file mode 100644 index 0000000000..bd19a872f6 --- /dev/null +++ b/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/TarExtractor.java @@ -0,0 +1,39 @@ +package com.baeldung.commons.untar; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; + +public abstract class TarExtractor { + + private InputStream tarStream; + private boolean gzip; + private Path destination; + + public TarExtractor(InputStream in, boolean gzip, Path destination) throws IOException { + this.tarStream = in; + this.gzip = gzip; + this.destination = destination; + + Files.createDirectories(destination); + } + + public TarExtractor(Path tarFile, Path destination) throws IOException { + this(Files.newInputStream(tarFile), tarFile.endsWith("gz"), destination); + } + + public Path getDestination() { + return destination; + } + + public InputStream getTarStream() { + return tarStream; + } + + public boolean isGzip() { + return gzip; + } + + abstract public void untar() throws IOException; +} diff --git a/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorAnt.java b/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorAnt.java new file mode 100644 index 0000000000..5bb4d74beb --- /dev/null +++ b/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorAnt.java @@ -0,0 +1,37 @@ +package com.baeldung.commons.untar.impl; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.zip.GZIPInputStream; + +import org.apache.tools.tar.TarEntry; +import org.apache.tools.tar.TarInputStream; + +import com.baeldung.commons.untar.TarExtractor; + +public class TarExtractorAnt extends TarExtractor { + + public TarExtractorAnt(InputStream in, boolean gzip, Path destination) throws IOException { + super(in, gzip, destination); + } + + @Override + public void untar() throws IOException { + try (TarInputStream tar = new TarInputStream(new BufferedInputStream( // + isGzip() ? new GZIPInputStream(getTarStream()) : getTarStream()))) { + TarEntry entry; + while ((entry = tar.getNextEntry()) != null) { + Path extractTo = getDestination().resolve(entry.getName()); + if (entry.isDirectory()) { + Files.createDirectories(extractTo); + } else { + Files.copy(tar, extractTo, StandardCopyOption.REPLACE_EXISTING); + } + } + } + } +} \ No newline at end of file diff --git a/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorCommonsCompress.java b/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorCommonsCompress.java new file mode 100644 index 0000000000..0f6d557f61 --- /dev/null +++ b/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorCommonsCompress.java @@ -0,0 +1,38 @@ +package com.baeldung.commons.untar.impl; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; + +import org.apache.commons.compress.archivers.ArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; +import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; + +import com.baeldung.commons.untar.TarExtractor; + +public class TarExtractorCommonsCompress extends TarExtractor { + + public TarExtractorCommonsCompress(InputStream in, boolean gzip, Path destination) throws IOException { + super(in, gzip, destination); + } + + @Override + public void untar() throws IOException { + try (BufferedInputStream inputStream = new BufferedInputStream(getTarStream()); // + TarArchiveInputStream tar = new TarArchiveInputStream( // + isGzip() ? new GzipCompressorInputStream(inputStream) : inputStream)) { + ArchiveEntry entry; + while ((entry = tar.getNextEntry()) != null) { + Path extractTo = getDestination().resolve(entry.getName()); + if (entry.isDirectory()) { + Files.createDirectories(extractTo); + } else { + Files.copy(tar, extractTo, StandardCopyOption.REPLACE_EXISTING); + } + } + } + } +} diff --git a/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorVfs.java b/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorVfs.java new file mode 100644 index 0000000000..add009aae0 --- /dev/null +++ b/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorVfs.java @@ -0,0 +1,54 @@ +package com.baeldung.commons.untar.impl; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.util.Iterator; + +import org.apache.commons.vfs2.FileContent; +import org.apache.commons.vfs2.FileObject; +import org.apache.commons.vfs2.FileSystemManager; +import org.apache.commons.vfs2.FileType; +import org.apache.commons.vfs2.VFS; + +import com.baeldung.commons.untar.TarExtractor; + +public class TarExtractorVfs extends TarExtractor { + + public TarExtractorVfs(InputStream in, boolean gzip, Path destination) throws IOException { + super(in, gzip, destination); + } + + @Override + public void untar() throws IOException { + Path tmpTar = Files.createTempFile("temp", isGzip() ? ".tar.gz" : ".tar"); + Files.copy(getTarStream(), tmpTar, StandardCopyOption.REPLACE_EXISTING); + + FileSystemManager fsManager = VFS.getManager(); + String uri = String.format("%s:file://%s", isGzip() ? "tgz" : "tar", tmpTar.toString()); + FileObject tar = fsManager.resolveFile(uri); + + Iterator contents = tar.iterator(); + while (contents.hasNext()) { + FileObject entry = contents.next(); + + Path extractTo = Paths.get(getDestination().toString(), entry.getName() + .getPath()); + + if (entry.isReadable() && entry.getType() == FileType.FILE) { + Files.createDirectories(extractTo.getParent()); + + try (FileContent fc = entry.getContent(); InputStream stream = fc.getInputStream()) { + Files.copy(stream, extractTo, StandardCopyOption.REPLACE_EXISTING); + } + } else { + Files.createDirectories(extractTo); + } + } + + Files.delete(tmpTar); + } +} diff --git a/libraries-apache-commons-2/src/main/resources/logback.xml b/libraries-apache-commons-2/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/libraries-apache-commons-2/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/Resources.java b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/Resources.java new file mode 100644 index 0000000000..3336a3ec39 --- /dev/null +++ b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/Resources.java @@ -0,0 +1,8 @@ +package com.baeldung.commons.untar; + +import java.io.InputStream; + +public interface Resources { + InputStream TAR_FILE = Resources.class.getResourceAsStream("/untar/test.tar"); + InputStream TAR_GZ_FILE = Resources.class.getResourceAsStream("/untar/test.tar.gz"); +} diff --git a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorAntUnitTest.java b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorAntUnitTest.java new file mode 100644 index 0000000000..01b4866a04 --- /dev/null +++ b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorAntUnitTest.java @@ -0,0 +1,37 @@ +package com.baeldung.commons.untar; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.junit.Test; + +import com.baeldung.commons.untar.impl.TarExtractorAnt; + +public class TarExtractorAntUnitTest { + + @Test + public void givenTarFile_whenUntar_thenExtractedToDestination() throws IOException { + Path destination = Paths.get("/tmp/ant"); + + new TarExtractorAnt(Resources.TAR_FILE, false, destination).untar(); + + assertTrue(Files.list(destination) + .findFirst() + .isPresent()); + } + + @Test + public void givenTarGzFile_whenUntar_thenExtractedToDestination() throws IOException { + Path destination = Paths.get("/tmp/ant-gz"); + + new TarExtractorAnt(Resources.TAR_GZ_FILE, true, destination).untar(); + + assertTrue(Files.list(destination) + .findFirst() + .isPresent()); + } +} diff --git a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorCommonsCompressUnitTest.java b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorCommonsCompressUnitTest.java new file mode 100644 index 0000000000..b43a437a64 --- /dev/null +++ b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorCommonsCompressUnitTest.java @@ -0,0 +1,37 @@ +package com.baeldung.commons.untar; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.junit.jupiter.api.Test; + +import com.baeldung.commons.untar.impl.TarExtractorCommonsCompress; + +public class TarExtractorCommonsCompressUnitTest { + + @Test + public void givenTarFile_whenUntar_thenExtractedToDestination() throws IOException { + Path destination = Paths.get("/tmp/commons-compress"); + + new TarExtractorCommonsCompress(Resources.TAR_FILE, false, destination).untar(); + + assertTrue(Files.list(destination) + .findFirst() + .isPresent()); + } + + @Test + public void givenTarGzFile_whenUntar_thenExtractedToDestination() throws IOException { + Path destination = Paths.get("/tmp/commons-compress-gz"); + + new TarExtractorCommonsCompress(Resources.TAR_GZ_FILE, true, destination).untar(); + + assertTrue(Files.list(destination) + .findFirst() + .isPresent()); + } +} diff --git a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorVfsUnitTest.java b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorVfsUnitTest.java new file mode 100644 index 0000000000..8f513055ae --- /dev/null +++ b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorVfsUnitTest.java @@ -0,0 +1,37 @@ +package com.baeldung.commons.untar; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.junit.Test; + +import com.baeldung.commons.untar.impl.TarExtractorVfs; + +public class TarExtractorVfsUnitTest { + + @Test + public void givenTarFile_whenUntar_thenExtractedToDestination() throws IOException { + Path destination = Paths.get("/tmp/vfs"); + + new TarExtractorVfs(Resources.TAR_FILE, false, destination).untar(); + + assertTrue(Files.list(destination) + .findFirst() + .isPresent()); + } + + @Test + public void givenTarGzFile_whenUntar_thenExtractedToDestination() throws IOException { + Path destination = Paths.get("/tmp/vfs-gz"); + + new TarExtractorVfs(Resources.TAR_GZ_FILE, true, destination).untar(); + + assertTrue(Files.list(destination) + .findFirst() + .isPresent()); + } +} diff --git a/libraries-apache-commons-2/src/test/resources/untar/test.tar b/libraries-apache-commons-2/src/test/resources/untar/test.tar new file mode 100644 index 0000000000000000000000000000000000000000..a0c9e8714fc9c02482ceb12119b972e3b7327f81 GIT binary patch literal 5632 zcmeHJyAHxI49wiG$QOW}=l3Lt0f~vU5}%Kg3fiSGC9;IGbg*P!F23HS)BK!O-va<4 zIIRFMdf0!d(oWFmoV5lqV$^`v1W+3K!dkm8Wlj%*w%l)}T+89eU-k7O^>X#-!#>Ok zY0A@OVg^d!3}Ya3)4y>||2~TTM=XKpe+0{XXOtzMd!r*ijM49X|D6MxS2n-=_&)^% z@_&e^wC@e9_WyJLukW9+uL;ltf6sr2TmEx3|2ay4^8X*P{)r5L5}-uD5o9x*QT3l# c>EAZ}8!!4Fu`Z(j5iH9LVL%uV2KX`X0$IO(CIA2c literal 0 HcmV?d00001 diff --git a/libraries-apache-commons-2/src/test/resources/untar/test.tar.gz b/libraries-apache-commons-2/src/test/resources/untar/test.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..99bc3ec9339c01c9214b6f2db104e7868b742c93 GIT binary patch literal 211 zcmV;^04)C>iwFSng?3~B1MQYg4gw(zMrS>z-~`(8*YkjJVd6@L#N%5qI&O4U%#5LL z(*-1yG>?yPnC{ci769ZyeuWj|;k@&v`;(y}IwFaKfopBLDFARLry z-B_V>|I7R%whe*B_aAYdzb*ei%LyRw{~81i08RjjfKDm?hh6z^s{h)F|7#%r3xXi@ N$pic9;O_ty001@SXGQ=3 literal 0 HcmV?d00001 diff --git a/libraries-apache-commons/README.md b/libraries-apache-commons/README.md index aceea3282a..1c2215d8b0 100644 --- a/libraries-apache-commons/README.md +++ b/libraries-apache-commons/README.md @@ -13,4 +13,5 @@ This module contains articles about Apache Commons libraries. - [Apache Commons BeanUtils](https://www.baeldung.com/apache-commons-beanutils) - [Histograms with Apache Commons Frequency](https://www.baeldung.com/apache-commons-frequency) - [An Introduction to Apache Commons Lang 3](https://www.baeldung.com/java-commons-lang-3) -- [Differences Between the Java WatchService API and the Apache Commons IO Monitor Library](https://www.baeldung.com/java-watchservice-vs-apache-commons-io-monitor-library) \ No newline at end of file +- [Differences Between the Java WatchService API and the Apache Commons IO Monitor Library](https://www.baeldung.com/java-watchservice-vs-apache-commons-io-monitor-library) +- More articles: [[next-->]](../libraries-apache-commons-2) \ No newline at end of file diff --git a/pom.xml b/pom.xml index 1c59d323b8..cf02e2a859 100644 --- a/pom.xml +++ b/pom.xml @@ -862,6 +862,7 @@ libraries-6 libraries-apache-commons + libraries-apache-commons-2 libraries-apache-commons-collections libraries-apache-commons-io libraries-data-2 @@ -1123,6 +1124,7 @@ libraries-5 libraries-6 libraries-apache-commons + libraries-apache-commons-2 libraries-apache-commons-collections libraries-apache-commons-io libraries-data-2 From 95e8e24ddd4c269b5e5b162e6536a799692fad7f Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Fri, 2 Jun 2023 19:37:30 -0300 Subject: [PATCH 2/4] test adjustments --- .../baeldung/commons/untar/impl/TarExtractorVfs.java | 4 +--- .../java/com/baeldung/commons/untar/Resources.java | 10 ++++++++-- .../commons/untar/TarExtractorAntUnitTest.java | 4 ++-- .../untar/TarExtractorCommonsCompressUnitTest.java | 4 ++-- .../commons/untar/TarExtractorVfsUnitTest.java | 4 ++-- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorVfs.java b/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorVfs.java index add009aae0..2932b9c3bc 100644 --- a/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorVfs.java +++ b/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorVfs.java @@ -41,11 +41,9 @@ public class TarExtractorVfs extends TarExtractor { if (entry.isReadable() && entry.getType() == FileType.FILE) { Files.createDirectories(extractTo.getParent()); - try (FileContent fc = entry.getContent(); InputStream stream = fc.getInputStream()) { + try (FileContent content = entry.getContent(); InputStream stream = content.getInputStream()) { Files.copy(stream, extractTo, StandardCopyOption.REPLACE_EXISTING); } - } else { - Files.createDirectories(extractTo); } } diff --git a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/Resources.java b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/Resources.java index 3336a3ec39..5b9b022d7f 100644 --- a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/Resources.java +++ b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/Resources.java @@ -3,6 +3,12 @@ package com.baeldung.commons.untar; import java.io.InputStream; public interface Resources { - InputStream TAR_FILE = Resources.class.getResourceAsStream("/untar/test.tar"); - InputStream TAR_GZ_FILE = Resources.class.getResourceAsStream("/untar/test.tar.gz"); + + static InputStream tarFile() { + return Resources.class.getResourceAsStream("/untar/test.tar"); + } + + static InputStream tarGzFile() { + return Resources.class.getResourceAsStream("/untar/test.tar.gz"); + } } diff --git a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorAntUnitTest.java b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorAntUnitTest.java index 01b4866a04..80b8fda104 100644 --- a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorAntUnitTest.java +++ b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorAntUnitTest.java @@ -17,7 +17,7 @@ public class TarExtractorAntUnitTest { public void givenTarFile_whenUntar_thenExtractedToDestination() throws IOException { Path destination = Paths.get("/tmp/ant"); - new TarExtractorAnt(Resources.TAR_FILE, false, destination).untar(); + new TarExtractorAnt(Resources.tarFile(), false, destination).untar(); assertTrue(Files.list(destination) .findFirst() @@ -28,7 +28,7 @@ public class TarExtractorAntUnitTest { public void givenTarGzFile_whenUntar_thenExtractedToDestination() throws IOException { Path destination = Paths.get("/tmp/ant-gz"); - new TarExtractorAnt(Resources.TAR_GZ_FILE, true, destination).untar(); + new TarExtractorAnt(Resources.tarGzFile(), true, destination).untar(); assertTrue(Files.list(destination) .findFirst() diff --git a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorCommonsCompressUnitTest.java b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorCommonsCompressUnitTest.java index b43a437a64..bea67ea3f5 100644 --- a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorCommonsCompressUnitTest.java +++ b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorCommonsCompressUnitTest.java @@ -17,7 +17,7 @@ public class TarExtractorCommonsCompressUnitTest { public void givenTarFile_whenUntar_thenExtractedToDestination() throws IOException { Path destination = Paths.get("/tmp/commons-compress"); - new TarExtractorCommonsCompress(Resources.TAR_FILE, false, destination).untar(); + new TarExtractorCommonsCompress(Resources.tarFile(), false, destination).untar(); assertTrue(Files.list(destination) .findFirst() @@ -28,7 +28,7 @@ public class TarExtractorCommonsCompressUnitTest { public void givenTarGzFile_whenUntar_thenExtractedToDestination() throws IOException { Path destination = Paths.get("/tmp/commons-compress-gz"); - new TarExtractorCommonsCompress(Resources.TAR_GZ_FILE, true, destination).untar(); + new TarExtractorCommonsCompress(Resources.tarGzFile(), true, destination).untar(); assertTrue(Files.list(destination) .findFirst() diff --git a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorVfsUnitTest.java b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorVfsUnitTest.java index 8f513055ae..9a00fd7e56 100644 --- a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorVfsUnitTest.java +++ b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorVfsUnitTest.java @@ -17,7 +17,7 @@ public class TarExtractorVfsUnitTest { public void givenTarFile_whenUntar_thenExtractedToDestination() throws IOException { Path destination = Paths.get("/tmp/vfs"); - new TarExtractorVfs(Resources.TAR_FILE, false, destination).untar(); + new TarExtractorVfs(Resources.tarFile(), false, destination).untar(); assertTrue(Files.list(destination) .findFirst() @@ -28,7 +28,7 @@ public class TarExtractorVfsUnitTest { public void givenTarGzFile_whenUntar_thenExtractedToDestination() throws IOException { Path destination = Paths.get("/tmp/vfs-gz"); - new TarExtractorVfs(Resources.TAR_GZ_FILE, true, destination).untar(); + new TarExtractorVfs(Resources.tarGzFile(), true, destination).untar(); assertTrue(Files.list(destination) .findFirst() From 80e0f8b2e6760da4423bec12e61570898a1f4c0c Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Wed, 7 Jun 2023 12:20:25 -0300 Subject: [PATCH 3/4] check instanceof Movie --- .../baeldung/equalshashcoderecords/Movie.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/core-java-modules/core-java-records/src/main/java/com/baeldung/equalshashcoderecords/Movie.java b/core-java-modules/core-java-records/src/main/java/com/baeldung/equalshashcoderecords/Movie.java index e94638e8b5..9ee0f1ac95 100644 --- a/core-java-modules/core-java-records/src/main/java/com/baeldung/equalshashcoderecords/Movie.java +++ b/core-java-modules/core-java-records/src/main/java/com/baeldung/equalshashcoderecords/Movie.java @@ -4,20 +4,24 @@ import java.util.Objects; record Movie(String name, Integer yearOfRelease, String distributor) { -@Override -public boolean equals(Object other) { - if (this == other) { - return true; - } - if (other == null) { + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (other == null) { + return false; + } + if (!(other instanceof Movie)) { + return false; + } + + Movie movie = (Movie) other; + if (movie.name.equals(this.name) && movie.yearOfRelease.equals(this.yearOfRelease)) { + return true; + } return false; } - Movie movie = (Movie) other; - if (movie.name.equals(this.name) && movie.yearOfRelease.equals(this.yearOfRelease)) { - return true; - } - return false; -} @Override public int hashCode() { From c697704d9d7c544f51c83caccc76713c24cace7d Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Wed, 7 Jun 2023 12:34:50 -0300 Subject: [PATCH 4/4] removing unrelated code --- libraries-apache-commons-2/README.md | 7 --- libraries-apache-commons-2/log4j.properties | 1 - libraries-apache-commons-2/pom.xml | 39 ------------- .../baeldung/commons/untar/TarExtractor.java | 39 ------------- .../commons/untar/impl/TarExtractorAnt.java | 37 ------------- .../impl/TarExtractorCommonsCompress.java | 38 ------------- .../commons/untar/impl/TarExtractorVfs.java | 52 ------------------ .../src/main/resources/logback.xml | 13 ----- .../com/baeldung/commons/untar/Resources.java | 14 ----- .../untar/TarExtractorAntUnitTest.java | 37 ------------- .../TarExtractorCommonsCompressUnitTest.java | 37 ------------- .../untar/TarExtractorVfsUnitTest.java | 37 ------------- .../src/test/resources/untar/test.tar | Bin 5632 -> 0 bytes .../src/test/resources/untar/test.tar.gz | Bin 211 -> 0 bytes libraries-apache-commons/README.md | 3 +- pom.xml | 2 - 16 files changed, 1 insertion(+), 355 deletions(-) delete mode 100644 libraries-apache-commons-2/README.md delete mode 100644 libraries-apache-commons-2/log4j.properties delete mode 100644 libraries-apache-commons-2/pom.xml delete mode 100644 libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/TarExtractor.java delete mode 100644 libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorAnt.java delete mode 100644 libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorCommonsCompress.java delete mode 100644 libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorVfs.java delete mode 100644 libraries-apache-commons-2/src/main/resources/logback.xml delete mode 100644 libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/Resources.java delete mode 100644 libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorAntUnitTest.java delete mode 100644 libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorCommonsCompressUnitTest.java delete mode 100644 libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorVfsUnitTest.java delete mode 100644 libraries-apache-commons-2/src/test/resources/untar/test.tar delete mode 100644 libraries-apache-commons-2/src/test/resources/untar/test.tar.gz diff --git a/libraries-apache-commons-2/README.md b/libraries-apache-commons-2/README.md deleted file mode 100644 index 7f91a737d5..0000000000 --- a/libraries-apache-commons-2/README.md +++ /dev/null @@ -1,7 +0,0 @@ -## Apache Commons - -This module contains articles about Apache Commons libraries. - -### Relevant articles - -- More articles: [[<--prev]](../libraries-apache-commons) \ No newline at end of file diff --git a/libraries-apache-commons-2/log4j.properties b/libraries-apache-commons-2/log4j.properties deleted file mode 100644 index 2173c5d96f..0000000000 --- a/libraries-apache-commons-2/log4j.properties +++ /dev/null @@ -1 +0,0 @@ -log4j.rootLogger=INFO, stdout diff --git a/libraries-apache-commons-2/pom.xml b/libraries-apache-commons-2/pom.xml deleted file mode 100644 index c8392ac561..0000000000 --- a/libraries-apache-commons-2/pom.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - 4.0.0 - libraries-apache-commons-2 - libraries-apache-commons-2 - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - - - org.apache.commons - commons-compress - ${commons-compress.version} - - - org.apache.ant - ant - ${ant.version} - - - org.apache.commons - commons-vfs2 - ${commons-vfs2.version} - - - - - 1.23.0 - 1.10.13 - 2.9.0 - - - diff --git a/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/TarExtractor.java b/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/TarExtractor.java deleted file mode 100644 index bd19a872f6..0000000000 --- a/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/TarExtractor.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.baeldung.commons.untar; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; - -public abstract class TarExtractor { - - private InputStream tarStream; - private boolean gzip; - private Path destination; - - public TarExtractor(InputStream in, boolean gzip, Path destination) throws IOException { - this.tarStream = in; - this.gzip = gzip; - this.destination = destination; - - Files.createDirectories(destination); - } - - public TarExtractor(Path tarFile, Path destination) throws IOException { - this(Files.newInputStream(tarFile), tarFile.endsWith("gz"), destination); - } - - public Path getDestination() { - return destination; - } - - public InputStream getTarStream() { - return tarStream; - } - - public boolean isGzip() { - return gzip; - } - - abstract public void untar() throws IOException; -} diff --git a/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorAnt.java b/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorAnt.java deleted file mode 100644 index 5bb4d74beb..0000000000 --- a/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorAnt.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.commons.untar.impl; - -import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import java.util.zip.GZIPInputStream; - -import org.apache.tools.tar.TarEntry; -import org.apache.tools.tar.TarInputStream; - -import com.baeldung.commons.untar.TarExtractor; - -public class TarExtractorAnt extends TarExtractor { - - public TarExtractorAnt(InputStream in, boolean gzip, Path destination) throws IOException { - super(in, gzip, destination); - } - - @Override - public void untar() throws IOException { - try (TarInputStream tar = new TarInputStream(new BufferedInputStream( // - isGzip() ? new GZIPInputStream(getTarStream()) : getTarStream()))) { - TarEntry entry; - while ((entry = tar.getNextEntry()) != null) { - Path extractTo = getDestination().resolve(entry.getName()); - if (entry.isDirectory()) { - Files.createDirectories(extractTo); - } else { - Files.copy(tar, extractTo, StandardCopyOption.REPLACE_EXISTING); - } - } - } - } -} \ No newline at end of file diff --git a/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorCommonsCompress.java b/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorCommonsCompress.java deleted file mode 100644 index 0f6d557f61..0000000000 --- a/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorCommonsCompress.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.baeldung.commons.untar.impl; - -import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; - -import org.apache.commons.compress.archivers.ArchiveEntry; -import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; -import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; - -import com.baeldung.commons.untar.TarExtractor; - -public class TarExtractorCommonsCompress extends TarExtractor { - - public TarExtractorCommonsCompress(InputStream in, boolean gzip, Path destination) throws IOException { - super(in, gzip, destination); - } - - @Override - public void untar() throws IOException { - try (BufferedInputStream inputStream = new BufferedInputStream(getTarStream()); // - TarArchiveInputStream tar = new TarArchiveInputStream( // - isGzip() ? new GzipCompressorInputStream(inputStream) : inputStream)) { - ArchiveEntry entry; - while ((entry = tar.getNextEntry()) != null) { - Path extractTo = getDestination().resolve(entry.getName()); - if (entry.isDirectory()) { - Files.createDirectories(extractTo); - } else { - Files.copy(tar, extractTo, StandardCopyOption.REPLACE_EXISTING); - } - } - } - } -} diff --git a/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorVfs.java b/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorVfs.java deleted file mode 100644 index 2932b9c3bc..0000000000 --- a/libraries-apache-commons-2/src/main/java/com/baeldung/commons/untar/impl/TarExtractorVfs.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.baeldung.commons.untar.impl; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; -import java.util.Iterator; - -import org.apache.commons.vfs2.FileContent; -import org.apache.commons.vfs2.FileObject; -import org.apache.commons.vfs2.FileSystemManager; -import org.apache.commons.vfs2.FileType; -import org.apache.commons.vfs2.VFS; - -import com.baeldung.commons.untar.TarExtractor; - -public class TarExtractorVfs extends TarExtractor { - - public TarExtractorVfs(InputStream in, boolean gzip, Path destination) throws IOException { - super(in, gzip, destination); - } - - @Override - public void untar() throws IOException { - Path tmpTar = Files.createTempFile("temp", isGzip() ? ".tar.gz" : ".tar"); - Files.copy(getTarStream(), tmpTar, StandardCopyOption.REPLACE_EXISTING); - - FileSystemManager fsManager = VFS.getManager(); - String uri = String.format("%s:file://%s", isGzip() ? "tgz" : "tar", tmpTar.toString()); - FileObject tar = fsManager.resolveFile(uri); - - Iterator contents = tar.iterator(); - while (contents.hasNext()) { - FileObject entry = contents.next(); - - Path extractTo = Paths.get(getDestination().toString(), entry.getName() - .getPath()); - - if (entry.isReadable() && entry.getType() == FileType.FILE) { - Files.createDirectories(extractTo.getParent()); - - try (FileContent content = entry.getContent(); InputStream stream = content.getInputStream()) { - Files.copy(stream, extractTo, StandardCopyOption.REPLACE_EXISTING); - } - } - } - - Files.delete(tmpTar); - } -} diff --git a/libraries-apache-commons-2/src/main/resources/logback.xml b/libraries-apache-commons-2/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/libraries-apache-commons-2/src/main/resources/logback.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/Resources.java b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/Resources.java deleted file mode 100644 index 5b9b022d7f..0000000000 --- a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/Resources.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.commons.untar; - -import java.io.InputStream; - -public interface Resources { - - static InputStream tarFile() { - return Resources.class.getResourceAsStream("/untar/test.tar"); - } - - static InputStream tarGzFile() { - return Resources.class.getResourceAsStream("/untar/test.tar.gz"); - } -} diff --git a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorAntUnitTest.java b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorAntUnitTest.java deleted file mode 100644 index 80b8fda104..0000000000 --- a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorAntUnitTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.commons.untar; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - -import org.junit.Test; - -import com.baeldung.commons.untar.impl.TarExtractorAnt; - -public class TarExtractorAntUnitTest { - - @Test - public void givenTarFile_whenUntar_thenExtractedToDestination() throws IOException { - Path destination = Paths.get("/tmp/ant"); - - new TarExtractorAnt(Resources.tarFile(), false, destination).untar(); - - assertTrue(Files.list(destination) - .findFirst() - .isPresent()); - } - - @Test - public void givenTarGzFile_whenUntar_thenExtractedToDestination() throws IOException { - Path destination = Paths.get("/tmp/ant-gz"); - - new TarExtractorAnt(Resources.tarGzFile(), true, destination).untar(); - - assertTrue(Files.list(destination) - .findFirst() - .isPresent()); - } -} diff --git a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorCommonsCompressUnitTest.java b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorCommonsCompressUnitTest.java deleted file mode 100644 index bea67ea3f5..0000000000 --- a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorCommonsCompressUnitTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.commons.untar; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - -import org.junit.jupiter.api.Test; - -import com.baeldung.commons.untar.impl.TarExtractorCommonsCompress; - -public class TarExtractorCommonsCompressUnitTest { - - @Test - public void givenTarFile_whenUntar_thenExtractedToDestination() throws IOException { - Path destination = Paths.get("/tmp/commons-compress"); - - new TarExtractorCommonsCompress(Resources.tarFile(), false, destination).untar(); - - assertTrue(Files.list(destination) - .findFirst() - .isPresent()); - } - - @Test - public void givenTarGzFile_whenUntar_thenExtractedToDestination() throws IOException { - Path destination = Paths.get("/tmp/commons-compress-gz"); - - new TarExtractorCommonsCompress(Resources.tarGzFile(), true, destination).untar(); - - assertTrue(Files.list(destination) - .findFirst() - .isPresent()); - } -} diff --git a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorVfsUnitTest.java b/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorVfsUnitTest.java deleted file mode 100644 index 9a00fd7e56..0000000000 --- a/libraries-apache-commons-2/src/test/java/com/baeldung/commons/untar/TarExtractorVfsUnitTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.commons.untar; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - -import org.junit.Test; - -import com.baeldung.commons.untar.impl.TarExtractorVfs; - -public class TarExtractorVfsUnitTest { - - @Test - public void givenTarFile_whenUntar_thenExtractedToDestination() throws IOException { - Path destination = Paths.get("/tmp/vfs"); - - new TarExtractorVfs(Resources.tarFile(), false, destination).untar(); - - assertTrue(Files.list(destination) - .findFirst() - .isPresent()); - } - - @Test - public void givenTarGzFile_whenUntar_thenExtractedToDestination() throws IOException { - Path destination = Paths.get("/tmp/vfs-gz"); - - new TarExtractorVfs(Resources.tarGzFile(), true, destination).untar(); - - assertTrue(Files.list(destination) - .findFirst() - .isPresent()); - } -} diff --git a/libraries-apache-commons-2/src/test/resources/untar/test.tar b/libraries-apache-commons-2/src/test/resources/untar/test.tar deleted file mode 100644 index a0c9e8714fc9c02482ceb12119b972e3b7327f81..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5632 zcmeHJyAHxI49wiG$QOW}=l3Lt0f~vU5}%Kg3fiSGC9;IGbg*P!F23HS)BK!O-va<4 zIIRFMdf0!d(oWFmoV5lqV$^`v1W+3K!dkm8Wlj%*w%l)}T+89eU-k7O^>X#-!#>Ok zY0A@OVg^d!3}Ya3)4y>||2~TTM=XKpe+0{XXOtzMd!r*ijM49X|D6MxS2n-=_&)^% z@_&e^wC@e9_WyJLukW9+uL;ltf6sr2TmEx3|2ay4^8X*P{)r5L5}-uD5o9x*QT3l# c>EAZ}8!!4Fu`Z(j5iH9LVL%uV2KX`X0$IO(CIA2c diff --git a/libraries-apache-commons-2/src/test/resources/untar/test.tar.gz b/libraries-apache-commons-2/src/test/resources/untar/test.tar.gz deleted file mode 100644 index 99bc3ec9339c01c9214b6f2db104e7868b742c93..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 211 zcmV;^04)C>iwFSng?3~B1MQYg4gw(zMrS>z-~`(8*YkjJVd6@L#N%5qI&O4U%#5LL z(*-1yG>?yPnC{ci769ZyeuWj|;k@&v`;(y}IwFaKfopBLDFARLry z-B_V>|I7R%whe*B_aAYdzb*ei%LyRw{~81i08RjjfKDm?hh6z^s{h)F|7#%r3xXi@ N$pic9;O_ty001@SXGQ=3 diff --git a/libraries-apache-commons/README.md b/libraries-apache-commons/README.md index 1c2215d8b0..aceea3282a 100644 --- a/libraries-apache-commons/README.md +++ b/libraries-apache-commons/README.md @@ -13,5 +13,4 @@ This module contains articles about Apache Commons libraries. - [Apache Commons BeanUtils](https://www.baeldung.com/apache-commons-beanutils) - [Histograms with Apache Commons Frequency](https://www.baeldung.com/apache-commons-frequency) - [An Introduction to Apache Commons Lang 3](https://www.baeldung.com/java-commons-lang-3) -- [Differences Between the Java WatchService API and the Apache Commons IO Monitor Library](https://www.baeldung.com/java-watchservice-vs-apache-commons-io-monitor-library) -- More articles: [[next-->]](../libraries-apache-commons-2) \ No newline at end of file +- [Differences Between the Java WatchService API and the Apache Commons IO Monitor Library](https://www.baeldung.com/java-watchservice-vs-apache-commons-io-monitor-library) \ No newline at end of file diff --git a/pom.xml b/pom.xml index 16156c0d91..79b2071507 100644 --- a/pom.xml +++ b/pom.xml @@ -872,7 +872,6 @@ libraries-6 libraries-apache-commons - libraries-apache-commons-2 libraries-apache-commons-collections libraries-apache-commons-io libraries-data-2 @@ -1135,7 +1134,6 @@ libraries-5 libraries-6 libraries-apache-commons - libraries-apache-commons-2 libraries-apache-commons-collections libraries-apache-commons-io libraries-data-2