1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }