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  package org.apache.commons.compress.archivers.zip;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.nio.ByteBuffer;
26  import java.nio.file.Files;
27  
28  import org.apache.commons.compress.AbstractTest;
29  import org.junit.jupiter.api.Test;
30  
31  public class ZipSplitOutputStreamTest extends AbstractTest {
32  
33      @Test
34      public void testCreateSplittedFiles() throws IOException {
35          final File testOutputFile = newTempFile("testCreateSplittedFiles.zip");
36          final int splitSize = 100 * 1024; /* 100 KB */
37          final File fileToTest = getFile("COMPRESS-477/split_zip_created_by_zip/zip_to_compare_created_by_zip.zip");
38          try (ZipSplitOutputStream zipSplitOutputStream = new ZipSplitOutputStream(testOutputFile, splitSize)) {
39              try (InputStream inputStream = Files.newInputStream(fileToTest.toPath())) {
40                  final byte[] buffer = new byte[4096];
41                  int readLen;
42                  while ((readLen = inputStream.read(buffer)) > 0) {
43                      zipSplitOutputStream.write(buffer, 0, readLen);
44                  }
45              }
46          }
47  
48          File zipFile = new File(getTempDirFile().getPath(), "testCreateSplittedFiles.z01");
49          assertEquals(zipFile.length(), splitSize);
50  
51          zipFile = new File(getTempDirFile().getPath(), "testCreateSplittedFiles.z02");
52          assertEquals(zipFile.length(), splitSize);
53  
54          zipFile = new File(getTempDirFile().getPath(), "testCreateSplittedFiles.z03");
55          assertEquals(zipFile.length(), splitSize);
56  
57          zipFile = new File(getTempDirFile().getPath(), "testCreateSplittedFiles.z04");
58          assertEquals(zipFile.length(), splitSize);
59  
60          zipFile = new File(getTempDirFile().getPath(), "testCreateSplittedFiles.z05");
61          assertEquals(zipFile.length(), splitSize);
62  
63          zipFile = new File(getTempDirFile().getPath(), "testCreateSplittedFiles.zip");
64          assertEquals(zipFile.length(), fileToTest.length() + 4 - splitSize * 5);
65      }
66  
67      @Test
68      public void testSplitZipBeginsWithZipSplitSignature() throws IOException {
69          final File tempFile = createTempFile("temp", "zip");
70          try (ZipSplitOutputStream is = new ZipSplitOutputStream(tempFile, 100 * 1024L);
71                  InputStream inputStream = Files.newInputStream(tempFile.toPath())) {
72              final byte[] buffer = new byte[4];
73              inputStream.read(buffer);
74              assertEquals(ByteBuffer.wrap(ZipArchiveOutputStream.DD_SIG).getInt(), ByteBuffer.wrap(buffer).getInt());
75          }
76      }
77  
78      @Test
79      public void testThrowsExceptionIfSplitSizeIsTooLarge() {
80          assertThrows(IllegalArgumentException.class, () -> new ZipSplitOutputStream(createTempFile("temp", "zip"), 4 * 1024 * 1024 * 1024L));
81      }
82  
83      @Test
84      public void testThrowsExceptionIfSplitSizeIsTooSmall() {
85          assertThrows(IllegalArgumentException.class, () -> new ZipSplitOutputStream(createTempFile("temp", "zip"), 64 * 1024 - 1));
86      }
87  
88      @Test
89      public void testThrowsIfUnsplittableSizeLargerThanSplitSize() throws IOException {
90          final long splitSize = 100 * 1024;
91          final ZipSplitOutputStream output = new ZipSplitOutputStream(createTempFile("temp", "zip"), splitSize);
92          assertThrows(IllegalArgumentException.class, () -> output.prepareToWriteUnsplittableContent(splitSize + 1));
93      }
94  }