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