summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorralph isenmann2022-01-27 16:13:08 +0100
committerralph isenmann2022-01-27 16:13:08 +0100
commit628faf901555f926ad4d9b7294b0be166a215a38 (patch)
treeac5af6aaeeeba303b68d2c62edf319724be476d3 /src
parentAdd TarArchiveReader and TarArchiveWriter as Util classes. (diff)
downloadmaster-sync-shared-628faf901555f926ad4d9b7294b0be166a215a38.tar.gz
master-sync-shared-628faf901555f926ad4d9b7294b0be166a215a38.tar.xz
master-sync-shared-628faf901555f926ad4d9b7294b0be166a215a38.zip
[Util] Change Tar Dependence to apache commens-compress, fixes #3888
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/openslx/util/TarArchiveUtil.java36
-rw-r--r--src/test/java/org/openslx/util/TarArchiveUtilTest.java45
2 files changed, 66 insertions, 15 deletions
diff --git a/src/main/java/org/openslx/util/TarArchiveUtil.java b/src/main/java/org/openslx/util/TarArchiveUtil.java
index 20af003..d691166 100644
--- a/src/main/java/org/openslx/util/TarArchiveUtil.java
+++ b/src/main/java/org/openslx/util/TarArchiveUtil.java
@@ -1,9 +1,8 @@
package org.openslx.util;
-import org.kamranzafar.jtar.TarEntry;
-import org.kamranzafar.jtar.TarHeader;
-import org.kamranzafar.jtar.TarInputStream;
-import org.kamranzafar.jtar.TarOutputStream;
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
+import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
@@ -12,7 +11,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
-
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
@@ -23,9 +21,8 @@ public class TarArchiveUtil {
public static class TarArchiveReader implements AutoCloseable {
private boolean isCompressed;
- private final TarInputStream tarInputStream;
-
- private TarEntry currentEntry = null;
+ private final TarArchiveInputStream tarInputStream;
+ private TarArchiveEntry currentEntry = null;
public TarArchiveReader(InputStream in) throws IOException {
this(in,true,false);
@@ -43,11 +40,11 @@ public class TarArchiveUtil {
stream = new GZIPInputStream(stream);
}
- this.tarInputStream = new TarInputStream(stream);
+ this.tarInputStream = new TarArchiveInputStream(stream);
}
public boolean hasNextEntry() throws IOException {
- this.currentEntry = this.tarInputStream.getNextEntry();
+ this.currentEntry = this.tarInputStream.getNextTarEntry();
if (this.currentEntry != null) {
return true;
} else {
@@ -56,6 +53,9 @@ public class TarArchiveUtil {
}
public String getEntryName() {
+ if (this.currentEntry == null)
+ return null;
+
return this.currentEntry.getName();
}
@@ -82,7 +82,8 @@ public class TarArchiveUtil {
{
private boolean isBuffered;
private boolean isCompressed;
- private final TarOutputStream tarOutputStream;
+ // private final TarOutputStream tarOutputStream;
+ private final TarArchiveOutputStream tarOutputStream;
public TarArchiveWriter (OutputStream out) throws IOException {
this(out, true, true);
@@ -102,7 +103,7 @@ public class TarArchiveUtil {
stream = new GZIPOutputStream(stream);
}
- this.tarOutputStream = new TarOutputStream(stream);
+ this.tarOutputStream = new TarArchiveOutputStream(stream);
}
public void writeFile(String filename, String data) throws IOException {
@@ -121,13 +122,18 @@ public class TarArchiveUtil {
private void putFile(String filename, byte[] data) throws IOException {
if (data == null)
return;
- tarOutputStream.putNextEntry(new TarEntry(TarHeader.createHeader(filename,
- data.length, Util.unixTime(), false, 0644)));
+
+ TarArchiveEntry entry = new TarArchiveEntry(filename);
+ entry.setSize(data.length);
+ entry.setModTime(System.currentTimeMillis());
+ entry.setMode(0644);
+ tarOutputStream.putArchiveEntry(entry);
tarOutputStream.write(data);
+ tarOutputStream.closeArchiveEntry();
}
@Override
- public void close() throws Exception {
+ public void close() throws IOException {
tarOutputStream.close();
}
}
diff --git a/src/test/java/org/openslx/util/TarArchiveUtilTest.java b/src/test/java/org/openslx/util/TarArchiveUtilTest.java
new file mode 100644
index 0000000..5df5202
--- /dev/null
+++ b/src/test/java/org/openslx/util/TarArchiveUtilTest.java
@@ -0,0 +1,45 @@
+package org.openslx.util;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.openslx.util.TarArchiveUtil.TarArchiveReader;
+import org.openslx.util.TarArchiveUtil.TarArchiveWriter;
+
+public class TarArchiveUtilTest {
+
+ @Test
+ @DisplayName( "Test creating tgz file" )
+ public void testCreateTarGz() throws IOException
+ {
+ // dummy content
+ final String DUMMY_FILENAME = "test";
+ final String DUMMY_FILE_DATA = "Hello World";
+
+ // create targz file with dummy content
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ TarArchiveWriter tarArchiveWriter = new TarArchiveWriter(out);
+
+ tarArchiveWriter.writeFile(DUMMY_FILENAME, DUMMY_FILE_DATA);
+ tarArchiveWriter.close();
+
+
+ // read created targz file,
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ TarArchiveReader tarArchiveReader = new TarArchiveReader(in, true, true);
+
+ assertTrue(tarArchiveReader.hasNextEntry(), "Tar Archive should contain a file");
+ assertEquals(DUMMY_FILENAME, tarArchiveReader.getEntryName());
+
+ String test_string = new String(tarArchiveReader.readCurrentEntry(), StandardCharsets.UTF_8);
+ assertEquals(DUMMY_FILE_DATA, test_string);
+ tarArchiveReader.close();
+ }
+}