View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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                  // testing all bytes for a value of 0 is going to take
54                  // too long, just pick a few ones randomly
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 }