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