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  
19  package org.apache.commons.compress.archivers.tar;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertNull;
24  
25  import java.io.BufferedInputStream;
26  import java.io.InputStream;
27  import java.util.Random;
28  
29  import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
30  import org.junit.Test;
31  
32  public class BigFilesIT {
33  
34      @Test
35      public void readFileBiggerThan8GByteStar() throws Exception {
36          readFileBiggerThan8GByte("/8.star.tar.gz");
37      }
38  
39      @Test
40      public void readFileBiggerThan8GBytePosix() throws Exception {
41          readFileBiggerThan8GByte("/8.posix.tar.gz");
42      }
43  
44      @Test
45      public void readFileHeadersOfArchiveBiggerThan8GByte() throws Exception {
46          InputStream in = null;
47          GzipCompressorInputStream gzin = null;
48          TarArchiveInputStream tin = null;
49          try {
50              in = new BufferedInputStream(BigFilesIT.class
51                                           .getResourceAsStream("/8.posix.tar.gz")
52                                           );
53              gzin = new GzipCompressorInputStream(in);
54              tin = new TarArchiveInputStream(gzin);
55              TarArchiveEntry e = tin.getNextTarEntry();
56              assertNotNull(e);
57              assertNull(tin.getNextTarEntry());
58          } finally {
59              if (tin != null) {
60                  tin.close();
61              }
62              if (gzin != null) {
63                  gzin.close();
64              }
65              if (in != null) {
66                  in.close();
67              }
68          }
69      }
70  
71      private void readFileBiggerThan8GByte(String name) throws Exception {
72          InputStream in = null;
73          GzipCompressorInputStream gzin = null;
74          TarArchiveInputStream tin = null;
75          try {
76              in = new BufferedInputStream(BigFilesIT.class
77                                           .getResourceAsStream(name));
78              gzin = new GzipCompressorInputStream(in);
79              tin = new TarArchiveInputStream(gzin);
80              TarArchiveEntry e = tin.getNextTarEntry();
81              assertNotNull(e);
82              assertEquals(8200l * 1024 * 1024, e.getSize());
83  
84              long read = 0;
85              Random r = new Random(System.currentTimeMillis());
86              int readNow;
87              byte[] buf = new byte[1024 * 1024];
88              while ((readNow = tin.read(buf, 0, buf.length)) > 0) {
89                  // testing all bytes for a value of 0 is going to take
90                  // too long, just pick a few ones randomly
91                  for (int i = 0; i < 100; i++) {
92                      int idx = r.nextInt(readNow);
93                      assertEquals("testing byte " + (read + idx), 0, buf[idx]);
94                  }
95                  read += readNow;
96              }
97              assertEquals(8200l * 1024 * 1024, read);
98              assertNull(tin.getNextTarEntry());
99          } finally {
100             if (tin != null) {
101                 tin.close();
102             }
103             if (gzin != null) {
104                 gzin.close();
105             }
106             if (in != null) {
107                 in.close();
108             }
109         }
110     }
111 
112 }