1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.tar;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.io.File;
24 import java.io.OutputStream;
25 import java.io.PipedInputStream;
26 import java.io.PipedOutputStream;
27 import java.nio.file.Files;
28 import java.util.Arrays;
29 import java.util.List;
30
31 import org.apache.commons.compress.archivers.ArchiveStreamFactory;
32 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
33 import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
34 import org.apache.commons.compress.compressors.CompressorStreamFactory;
35 import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
36 import org.apache.commons.compress.utils.IOUtils;
37 import org.apache.commons.vfs2.CacheStrategy;
38 import org.apache.commons.vfs2.FileObject;
39 import org.apache.commons.vfs2.cache.SoftRefFilesCache;
40 import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
41 import org.apache.commons.vfs2.provider.local.DefaultLocalFileProvider;
42 import org.junit.jupiter.api.BeforeEach;
43 import org.junit.jupiter.api.Test;
44
45
46 public class LargeTarTest {
47
48 private final static String baseDir = "target/test-classes/test-data/";
49
50 private final static String largeFilePath = baseDir;
51 private final static String largeFileName = "largefile";
52 private DefaultFileSystemManager manager;
53
54
55 protected void createLargeFile(final String path, final String name) throws Exception {
56 final long _1K = 1024;
57 final long _1M = 1024 * _1K;
58
59
60 final long _1G = 1024 * _1M;
61
62
63 final long fileSize = 3 * _1G;
64
65 final File tarGzFile = new File(path + name + ".tar.gz");
66
67 if (!tarGzFile.exists()) {
68 System.out.println(
69 "This test is a bit slow. It needs to write 3GB of data as a compressed file (approx. 3MB) to your hard drive");
70
71 final PipedOutputStream outTarFileStream = new PipedOutputStream();
72 final PipedInputStream inTarFileStream = new PipedInputStream(outTarFileStream);
73
74 final Thread source = new Thread(() -> {
75 final byte[] ba_1k = new byte[(int) _1K];
76 Arrays.fill(ba_1k, (byte) 'a');
77 try {
78 final TarArchiveOutputStream outTarStream = (TarArchiveOutputStream) new ArchiveStreamFactory()
79 .createArchiveOutputStream(ArchiveStreamFactory.TAR, outTarFileStream);
80
81 final TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(name + ".txt");
82 tarArchiveEntry.setSize(fileSize);
83
84 outTarStream.putArchiveEntry(tarArchiveEntry);
85 for (long i = 0; i < fileSize; i += ba_1k.length) {
86 outTarStream.write(ba_1k);
87 }
88 outTarStream.closeArchiveEntry();
89 outTarStream.close();
90 outTarFileStream.close();
91 } catch (final Exception e) {
92 e.printStackTrace();
93 }
94 });
95 source.start();
96
97 final File gzFile = new File(path + name + ".tar.gz");
98
99 final OutputStream outGzipFileStream = Files.newOutputStream(gzFile.toPath());
100
101 final GzipCompressorOutputStream outGzipStream = (GzipCompressorOutputStream) new CompressorStreamFactory()
102 .createCompressorOutputStream(CompressorStreamFactory.GZIP, outGzipFileStream);
103
104 IOUtils.copy(inTarFileStream, outGzipStream);
105 inTarFileStream.close();
106
107 outGzipStream.close();
108 outGzipFileStream.close();
109
110 }
111 }
112
113 protected boolean endsWith(final String testString, final String[] testList) {
114 for (final String string : testList) {
115 if (testString.endsWith(string)) {
116 return true;
117 }
118 }
119 return false;
120 }
121
122
123
124
125
126
127
128
129
130 protected void fileCheck(final String[] expectedFiles, final String tarFile) throws Exception {
131 assertNotNull(manager);
132 final FileObject file = manager.resolveFile(tarFile);
133
134 assertNotNull(file);
135 final List<FileObject> files = Arrays.asList(file.getChildren());
136
137 assertNotNull(files);
138 for (final String expectedFile : expectedFiles) {
139 assertTrue(fileExists(expectedFile, files), () -> "Expected file not found: " + expectedFile);
140 }
141 }
142
143
144
145
146
147
148
149
150 protected boolean fileExists(final String expectedFile, final List<FileObject> files) {
151 for (final FileObject file : files) {
152 if (file.getName().getBaseName().equals(expectedFile)) {
153 return true;
154 }
155 }
156 return false;
157 }
158
159 @BeforeEach
160 public void setUp() throws Exception {
161 manager = new DefaultFileSystemManager();
162
163 manager.setFilesCache(new SoftRefFilesCache());
164 manager.setCacheStrategy(CacheStrategy.ON_RESOLVE);
165
166 manager.addProvider("file", new DefaultLocalFileProvider());
167 manager.addProvider("tgz", new TarFileProvider());
168 manager.addProvider("tar", new TarFileProvider());
169
170 new File(baseDir).mkdir();
171 createLargeFile(largeFilePath, largeFileName);
172 }
173
174 @Test
175 public void testLargeFile() throws Exception {
176 final File realFile = new File(largeFilePath + largeFileName + ".tar.gz");
177
178 final FileObject file = manager.resolveFile("tgz:file://" + realFile.getCanonicalPath() + "!/");
179
180 assertNotNull(file);
181 final List<FileObject> files = Arrays.asList(file.getChildren());
182
183 assertNotNull(files);
184 assertEquals(1, files.size());
185 final FileObject f = files.get(0);
186
187 assertEquals(f.getName().getBaseName(), largeFileName + ".txt", () -> "Expected file not found: " + largeFileName + ".txt");
188 }
189
190 }