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