1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.compress.archivers.tar;
21
22 import static java.nio.charset.StandardCharsets.UTF_8;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.nio.file.FileSystem;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.attribute.GroupPrincipal;
34 import java.nio.file.attribute.UserPrincipal;
35
36 import org.apache.commons.compress.AbstractTest;
37 import org.apache.commons.compress.archivers.ArchiveEntry;
38 import org.apache.commons.compress.archivers.ArchiveException;
39 import org.apache.commons.compress.archivers.ArchiveOutputStream;
40 import org.apache.commons.compress.archivers.ArchiveStreamFactory;
41 import org.junit.jupiter.api.Test;
42
43 import com.github.marschall.memoryfilesystem.MemoryFileSystemBuilder;
44
45 class TarMemoryFileSystemTest {
46
47 @Test
48 void testCheckUserInformationInTarEntry() throws IOException, ArchiveException {
49 final String user = "commons";
50 final String group = "compress";
51 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().addUser(user).addGroup(group).build()) {
52 final Path pathSource = fileSystem.getPath("original-file.txt");
53 Files.write(pathSource, "Test".getBytes(UTF_8));
54 Files.setAttribute(pathSource, "posix:owner", (UserPrincipal) () -> user);
55 Files.setAttribute(pathSource, "posix:group", (GroupPrincipal) () -> group);
56
57 final Path target = fileSystem.getPath("original-file.tar");
58 try (OutputStream out = Files.newOutputStream(target);
59 ArchiveOutputStream<ArchiveEntry> tarOut = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream(ArchiveStreamFactory.TAR, out)) {
60 final TarArchiveEntry entry = new TarArchiveEntry(pathSource);
61 tarOut.putArchiveEntry(entry);
62 tarOut.write(pathSource);
63 tarOut.closeArchiveEntry();
64 }
65
66 try (InputStream input = Files.newInputStream(target);
67 TarArchiveInputStream tarIn = new TarArchiveInputStream(input)) {
68 final TarArchiveEntry nextTarEntry = tarIn.getNextTarEntry();
69
70 assertEquals(user, nextTarEntry.getUserName());
71 assertEquals(group, nextTarEntry.getGroupName());
72 }
73 }
74 }
75
76 @Test
77 void testTarFromMemoryFileSystem() throws IOException, ArchiveException {
78 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
79 final Path p = fileSystem.getPath("test.txt");
80 Files.write(p, "Test".getBytes(UTF_8));
81 final File f = File.createTempFile("commons-compress-memoryfs", ".tar");
82 try (OutputStream out = Files.newOutputStream(f.toPath());
83 ArchiveOutputStream<ArchiveEntry> tarOut = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream(ArchiveStreamFactory.TAR, out)) {
84 final TarArchiveEntry entry = new TarArchiveEntry(p);
85 tarOut.putArchiveEntry(entry);
86 tarOut.write(p);
87 tarOut.closeArchiveEntry();
88 assertEquals(f.length(), tarOut.getBytesWritten());
89 } finally {
90 AbstractTest.forceDelete(f);
91 }
92 }
93 }
94
95 @Test
96 void testTarToMemoryFileSystem() throws IOException, ArchiveException {
97 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
98 final Path p = fileSystem.getPath("target.tar");
99
100 try (OutputStream out = Files.newOutputStream(p);
101 ArchiveOutputStream<ArchiveEntry> tarOut = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream(ArchiveStreamFactory.TAR, out)) {
102 final String content = "Test";
103 final TarArchiveEntry entry = new TarArchiveEntry("test.txt");
104 entry.setSize(content.length());
105 tarOut.putArchiveEntry(entry);
106
107 tarOut.writeUtf8("Test");
108 tarOut.closeArchiveEntry();
109
110 assertTrue(Files.exists(p));
111 assertEquals(Files.size(p), tarOut.getBytesWritten());
112 }
113 }
114 }
115 }