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 org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25
26 import java.io.BufferedInputStream;
27 import java.io.InputStream;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.StandardCopyOption;
31 import java.util.List;
32 import java.util.Random;
33
34 import org.apache.commons.compress.AbstractTest;
35 import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
36 import org.junit.jupiter.api.Test;
37
38 public class BigFilesIT extends AbstractTest {
39
40 private void readFileBiggerThan8GByte(final String name) throws Exception {
41 try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(name)));
42 GzipCompressorInputStream gzin = new GzipCompressorInputStream(in);
43 TarArchiveInputStream tin = new TarArchiveInputStream(gzin)) {
44 final TarArchiveEntry e = tin.getNextTarEntry();
45 assertNotNull(e);
46 assertEquals(8200L * 1024 * 1024, e.getSize());
47
48 long read = 0;
49 final Random r = new Random(System.currentTimeMillis());
50 int readNow;
51 final byte[] buf = new byte[1024 * 1024];
52 while ((readNow = tin.read(buf, 0, buf.length)) > 0) {
53
54
55 for (int i = 0; i < 100; i++) {
56 final int idx = r.nextInt(readNow);
57 assertEquals(0, buf[idx], "testing byte " + (read + idx));
58 }
59 read += readNow;
60 }
61 assertEquals(8200L * 1024 * 1024, read);
62 assertNull(tin.getNextTarEntry());
63 }
64 }
65
66 @Test
67 void testReadFileBiggerThan8GBytePosix() throws Exception {
68 readFileBiggerThan8GByte("8.posix.tar.gz");
69 }
70
71 @Test
72 void testReadFileBiggerThan8GByteStar() throws Exception {
73 readFileBiggerThan8GByte("8.star.tar.gz");
74 }
75
76 @Test
77 void testReadFileHeadersOfArchiveBiggerThan8GByte() throws Exception {
78 try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath("8.posix.tar.gz")));
79 GzipCompressorInputStream gzin = new GzipCompressorInputStream(in);
80 TarArchiveInputStream tin = new TarArchiveInputStream(gzin)) {
81 final TarArchiveEntry e = tin.getNextTarEntry();
82 assertNotNull(e);
83 assertNull(tin.getNextTarEntry());
84 }
85 }
86
87 @Test
88 void testTarFileReadFileHeadersOfArchiveBiggerThan8GByte() throws Exception {
89 final Path file = getPath("8.posix.tar.gz");
90 final Path output = tempResultDir.toPath().resolve("8.posix.tar");
91 try (InputStream in = new BufferedInputStream(Files.newInputStream(file));
92 GzipCompressorInputStream gzin = new GzipCompressorInputStream(in)) {
93 Files.copy(gzin, output, StandardCopyOption.REPLACE_EXISTING);
94 }
95
96 try (TarFile tarFile = new TarFile(output)) {
97 final List<TarArchiveEntry> entries = tarFile.getEntries();
98 assertEquals(1, entries.size());
99 assertNotNull(entries.get(0));
100 }
101 }
102
103 }