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.assertArrayEquals;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.ByteArrayOutputStream;
27  import java.io.DataOutput;
28  import java.io.DataOutputStream;
29  import java.io.IOException;
30  import java.util.zip.Deflater;
31  import java.util.zip.ZipEntry;
32  
33  import org.junit.jupiter.api.Test;
34  
35  class StreamCompressorTest {
36  
37      @Test
38      void testCreateDataOutputCompressor() throws IOException {
39          final DataOutput dataOutputStream = new DataOutputStream(new ByteArrayOutputStream());
40          try (StreamCompressor streamCompressor = StreamCompressor.create(dataOutputStream, new Deflater(9))) {
41              assertNotNull(streamCompressor);
42          }
43      }
44  
45      @Test
46      void testDeflatedEntries() throws Exception {
47          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
48          try (StreamCompressor sc = StreamCompressor.create(baos)) {
49              sc.deflate(new ByteArrayInputStream("AAAAAABBBBBB".getBytes()), ZipEntry.DEFLATED);
50              assertEquals(12, sc.getBytesRead());
51              assertEquals(8, sc.getBytesWrittenForLastEntry());
52              assertEquals(3299542, sc.getCrc32());
53  
54              final byte[] actuals = baos.toByteArray();
55              final byte[] expected = { 115, 116, 4, 1, 39, 48, 0, 0 };
56              // Note that this test really asserts stuff about the java Deflater, which might be a little bit brittle
57              assertArrayEquals(expected, actuals);
58          }
59      }
60  
61      @Test
62      void testStoredEntries() throws Exception {
63          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
64          try (StreamCompressor sc = StreamCompressor.create(baos)) {
65              sc.deflate(new ByteArrayInputStream("A".getBytes()), ZipEntry.STORED);
66              sc.deflate(new ByteArrayInputStream("BAD".getBytes()), ZipEntry.STORED);
67              assertEquals(3, sc.getBytesRead());
68              assertEquals(3, sc.getBytesWrittenForLastEntry());
69              assertEquals(344750961, sc.getCrc32());
70              sc.deflate(new ByteArrayInputStream("CAFE".getBytes()), ZipEntry.STORED);
71              assertEquals("ABADCAFE", baos.toString());
72          }
73      }
74  }