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.apache.commons.compress.AbstractTestCase.getFile;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26
27 import junit.framework.TestCase;
28
29 public class EncryptedArchiveTest extends TestCase {
30
31 public void testReadPasswordEncryptedEntryViaZipFile()
32 throws IOException {
33 File file = getFile("password-encrypted.zip");
34 ZipFile zf = null;
35 try {
36 zf = new ZipFile(file);
37 ZipArchiveEntry zae = zf.getEntry("LICENSE.txt");
38 assertTrue(zae.getGeneralPurposeBit().usesEncryption());
39 assertFalse(zae.getGeneralPurposeBit().usesStrongEncryption());
40 assertFalse(zf.canReadEntryData(zae));
41 try {
42 zf.getInputStream(zae);
43 fail("expected an exception");
44 } catch (UnsupportedZipFeatureException ex) {
45 assertSame(UnsupportedZipFeatureException.Feature.ENCRYPTION,
46 ex.getFeature());
47 }
48 } finally {
49 ZipFile.closeQuietly(zf);
50 }
51 }
52
53 public void testReadPasswordEncryptedEntryViaStream()
54 throws IOException {
55 File file = getFile("password-encrypted.zip");
56 ZipArchiveInputStream zin = null;
57 try {
58 zin = new ZipArchiveInputStream(new FileInputStream(file));
59 ZipArchiveEntry zae = zin.getNextZipEntry();
60 assertEquals("LICENSE.txt", zae.getName());
61 assertTrue(zae.getGeneralPurposeBit().usesEncryption());
62 assertFalse(zae.getGeneralPurposeBit().usesStrongEncryption());
63 assertFalse(zin.canReadEntryData(zae));
64 try {
65 byte[] buf = new byte[1024];
66 zin.read(buf, 0, buf.length);
67 fail("expected an exception");
68 } catch (UnsupportedZipFeatureException ex) {
69 assertSame(UnsupportedZipFeatureException.Feature.ENCRYPTION,
70 ex.getFeature());
71 }
72 } finally {
73 if (zin != null) {
74 zin.close();
75 }
76 }
77 }
78 }