View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.commons.compress.archivers.tar;
19  
20  import static java.nio.charset.StandardCharsets.UTF_8;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  import java.nio.file.FileSystem;
29  import java.nio.file.Files;
30  import java.nio.file.Path;
31  import java.nio.file.attribute.GroupPrincipal;
32  import java.nio.file.attribute.UserPrincipal;
33  
34  import org.apache.commons.compress.AbstractTest;
35  import org.apache.commons.compress.archivers.ArchiveEntry;
36  import org.apache.commons.compress.archivers.ArchiveException;
37  import org.apache.commons.compress.archivers.ArchiveOutputStream;
38  import org.apache.commons.compress.archivers.ArchiveStreamFactory;
39  import org.junit.jupiter.api.Test;
40  
41  import com.github.marschall.memoryfilesystem.MemoryFileSystemBuilder;
42  
43  public class TarMemoryFileSystemTest {
44  
45      @Test
46      public void testCheckUserInformationInTarEntry() throws IOException, ArchiveException {
47          final String user = "commons";
48          final String group = "compress";
49          try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().addUser(user).addGroup(group).build()) {
50              final Path source = fileSystem.getPath("original-file.txt");
51              Files.write(source, "Test".getBytes(UTF_8));
52              Files.setAttribute(source, "posix:owner", (UserPrincipal) () -> user);
53              Files.setAttribute(source, "posix:group", (GroupPrincipal) () -> group);
54  
55              final Path target = fileSystem.getPath("original-file.tar");
56              try (OutputStream out = Files.newOutputStream(target);
57                      ArchiveOutputStream<ArchiveEntry> tarOut = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream(ArchiveStreamFactory.TAR, out)) {
58                  final TarArchiveEntry entry = new TarArchiveEntry(source);
59                  tarOut.putArchiveEntry(entry);
60  
61                  Files.copy(source, tarOut);
62                  tarOut.closeArchiveEntry();
63              }
64  
65              try (InputStream input = Files.newInputStream(target);
66                      TarArchiveInputStream tarIn = new TarArchiveInputStream(input)) {
67                  final TarArchiveEntry nextTarEntry = tarIn.getNextTarEntry();
68  
69                  assertEquals(user, nextTarEntry.getUserName());
70                  assertEquals(group, nextTarEntry.getGroupName());
71              }
72          }
73      }
74  
75      @Test
76      public void testTarFromMemoryFileSystem() throws IOException, ArchiveException {
77          try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
78              final Path p = fileSystem.getPath("test.txt");
79              Files.write(p, "Test".getBytes(UTF_8));
80  
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  
87                  Files.copy(p, tarOut);
88                  tarOut.closeArchiveEntry();
89                  assertEquals(f.length(), tarOut.getBytesWritten());
90              } finally {
91                  AbstractTest.forceDelete(f);
92              }
93          }
94      }
95  
96      @Test
97      public void testTarToMemoryFileSystem() throws IOException, ArchiveException {
98          try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
99              final Path p = fileSystem.getPath("target.tar");
100 
101             try (OutputStream out = Files.newOutputStream(p);
102                     ArchiveOutputStream<ArchiveEntry> tarOut = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream(ArchiveStreamFactory.TAR, out)) {
103                 final String content = "Test";
104                 final TarArchiveEntry entry = new TarArchiveEntry("test.txt");
105                 entry.setSize(content.length());
106                 tarOut.putArchiveEntry(entry);
107 
108                 tarOut.write("Test".getBytes(UTF_8));
109                 tarOut.closeArchiveEntry();
110 
111                 assertTrue(Files.exists(p));
112                 assertEquals(Files.size(p), tarOut.getBytesWritten());
113             }
114         }
115     }
116 }