BAEL-6403 - Extracting a Tar File in Java (#14240)
* code 1 * sibling module readme * new module * review 1 * review 2
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
protected TarExtractor(InputStream in, boolean gzip, Path destination) throws IOException {
|
||||
this.tarStream = in;
|
||||
this.gzip = gzip;
|
||||
this.destination = destination;
|
||||
|
||||
Files.createDirectories(destination);
|
||||
}
|
||||
|
||||
protected 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;
|
||||
}
|
||||
|
||||
public abstract void untar() throws IOException;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
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 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);
|
||||
FileObject tar = fsManager.resolveFile(uri);
|
||||
|
||||
for (FileObject entry : tar) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
13
libraries-apache-commons-2/src/main/resources/logback.xml
Normal file
13
libraries-apache-commons-2/src/main/resources/logback.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,40 @@
|
||||
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 java.util.stream.Stream;
|
||||
|
||||
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();
|
||||
|
||||
try (Stream<Path> files = Files.list(destination)) {
|
||||
assertTrue(files.findFirst()
|
||||
.isPresent());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTarGzFile_whenUntar_thenExtractedToDestination() throws IOException {
|
||||
Path destination = Paths.get("/tmp/ant-gz");
|
||||
|
||||
new TarExtractorAnt(Resources.tarGzFile(), true, destination).untar();
|
||||
|
||||
try (Stream<Path> files = Files.list(destination)) {
|
||||
assertTrue(files.findFirst()
|
||||
.isPresent());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
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 java.util.stream.Stream;
|
||||
|
||||
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();
|
||||
|
||||
try (Stream<Path> files = Files.list(destination)) {
|
||||
assertTrue(files.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();
|
||||
|
||||
try (Stream<Path> files = Files.list(destination)) {
|
||||
assertTrue(files.findFirst()
|
||||
.isPresent());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
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 java.util.stream.Stream;
|
||||
|
||||
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();
|
||||
|
||||
try (Stream<Path> files = Files.list(destination)) {
|
||||
assertTrue(files.findFirst()
|
||||
.isPresent());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTarGzFile_whenUntar_thenExtractedToDestination() throws IOException {
|
||||
Path destination = Paths.get("/tmp/vfs-gz");
|
||||
|
||||
new TarExtractorVfs(Resources.tarGzFile(), true, destination).untar();
|
||||
|
||||
try (Stream<Path> files = Files.list(destination)) {
|
||||
assertTrue(files.findFirst()
|
||||
.isPresent());
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
libraries-apache-commons-2/src/test/resources/untar/test.tar
Normal file
BIN
libraries-apache-commons-2/src/test/resources/untar/test.tar
Normal file
Binary file not shown.
BIN
libraries-apache-commons-2/src/test/resources/untar/test.tar.gz
Normal file
BIN
libraries-apache-commons-2/src/test/resources/untar/test.tar.gz
Normal file
Binary file not shown.
Reference in New Issue
Block a user