├── .gitignore
├── src
├── test
│ ├── resources
│ │ ├── bla.dump
│ │ ├── bla.jar
│ │ ├── bla.pack
│ │ ├── bla.tgz
│ │ ├── bla.zip
│ │ ├── moby.zip
│ │ ├── bla.tar.xz
│ │ ├── bla.z.dump
│ │ ├── test1.xml
│ │ ├── 8.star.tar.gz
│ │ ├── bla.tar.bz2
│ │ ├── bla.txt.bz2
│ │ ├── bla.xml.bz2
│ │ ├── multiple.bz2
│ │ ├── multiple.gz
│ │ ├── multiple.xz
│ │ ├── ordertest.zip
│ │ ├── testAIFF.aif
│ │ ├── 8.posix.tar.gz
│ │ ├── test-winzip.zip
│ │ ├── test2.xml
│ │ ├── COMPRESS-114.tar
│ │ ├── COMPRESS-131.bz2
│ │ ├── COMPRESS-208.zip
│ │ ├── archives
│ │ │ ├── SunOS.zip
│ │ │ ├── FreeBSD.zip
│ │ │ ├── SunOS_.cpio
│ │ │ ├── FreeBSD_bin.cpio
│ │ │ ├── FreeBSD_hpbin.cpio
│ │ │ ├── files.txt
│ │ │ ├── SunOS.ar
│ │ │ ├── FreeBSD.ar
│ │ │ ├── SunOS_odc.cpio
│ │ │ ├── FreeBSD_crc.cpio
│ │ │ ├── SunOS_-c.cpio
│ │ │ ├── SunOS_crc.cpio
│ │ │ └── FreeBSD_newc.cpio
│ │ ├── preepoch-star.tar
│ │ ├── utf8-7zip-test.zip
│ │ ├── testCompress209.doc
│ │ ├── utf8-winzip-test.zip
│ │ ├── zip64support.tar.bz2
│ │ ├── longpath
│ │ │ ├── minotaur-0.jar
│ │ │ ├── minotaur-M.jar
│ │ │ ├── minotaur-c.zip
│ │ │ ├── minotaur-z.zip
│ │ │ ├── minotaur.jar
│ │ │ ├── minotaur.zip
│ │ │ ├── minotaur.ar
│ │ │ ├── files.txt
│ │ │ └── minotaur.cpio
│ │ ├── password-encrypted.zip
│ │ ├── simple-aix-native-tar.tar
│ │ ├── apache-maven-2.2.1.zip.001
│ │ ├── longfile_bsd.ar
│ │ ├── OSX_ArchiveWithNestedArchive.zip
│ │ ├── test4.xml
│ │ ├── longfile_gnu.ar
│ │ ├── test.txt
│ │ ├── test3.xml
│ │ ├── test with spaces.txt
│ │ └── bla.ar
│ └── java
│ │ └── org
│ │ └── apache
│ │ └── commons
│ │ └── compress
│ │ ├── archivers
│ │ ├── memory
│ │ │ ├── MemoryArchiveEntry.java
│ │ │ ├── MemoryArchiveTestCase.java
│ │ │ └── MemoryArchiveInputStream.java
│ │ ├── tar
│ │ │ ├── SparseFilesTest.java
│ │ │ └── BigFilesIT.java
│ │ ├── dump
│ │ │ ├── DumpArchiveEntryTest.java
│ │ │ ├── DumpArchiveUtilTest.java
│ │ │ └── DumpArchiveInputStreamTest.java
│ │ ├── cpio
│ │ │ ├── CpioArchiveInputStreamTest.java
│ │ │ ├── CpioUtilTest.java
│ │ │ └── CpioArchiveOutputStreamTest.java
│ │ ├── ar
│ │ │ ├── ArArchiveInputStreamTest.java
│ │ │ └── ArArchiveOutputStreamTest.java
│ │ ├── jar
│ │ │ └── JarArchiveOutputStreamTest.java
│ │ ├── zip
│ │ │ ├── ZipShortTest.java
│ │ │ ├── ZipArchiveInputStreamTest.java
│ │ │ ├── ZipLongTest.java
│ │ │ ├── ZipUtilTest.java
│ │ │ └── EncryptedArchiveTest.java
│ │ └── ExceptionMessageTest.java
│ │ ├── ChainingTestCase.java
│ │ ├── utils
│ │ ├── CharsetsTest.java
│ │ └── CountingStreamTest.java
│ │ ├── DetectCompressorTestCase.java
│ │ ├── ArchiveUtilsTest.java
│ │ └── compressors
│ │ └── XZUtilsTestCase.java
├── site
│ ├── resources
│ │ └── images
│ │ │ ├── compress-logo-white.png
│ │ │ └── compress-logo-white.xcf
│ ├── site.xml
│ └── xdoc
│ │ └── conventions.xml
└── main
│ ├── java
│ └── org
│ │ └── apache
│ │ └── commons
│ │ └── compress
│ │ ├── utils
│ │ ├── package.html
│ │ ├── CountingOutputStream.java
│ │ ├── CountingInputStream.java
│ │ └── IOUtils.java
│ │ ├── compressors
│ │ ├── package.html
│ │ ├── bzip2
│ │ │ ├── package.html
│ │ │ └── BZip2Constants.java
│ │ ├── CompressorOutputStream.java
│ │ ├── gzip
│ │ │ ├── package.html
│ │ │ └── GzipCompressorOutputStream.java
│ │ ├── pack200
│ │ │ ├── Pack200Strategy.java
│ │ │ ├── InMemoryCachingStreamBridge.java
│ │ │ ├── TempFileCachingStreamBridge.java
│ │ │ └── StreamBridge.java
│ │ ├── CompressorException.java
│ │ └── CompressorInputStream.java
│ │ ├── archivers
│ │ ├── ar
│ │ │ └── package.html
│ │ ├── cpio
│ │ │ └── package.html
│ │ ├── package.html
│ │ ├── zip
│ │ │ ├── package.html
│ │ │ ├── Zip64Mode.java
│ │ │ ├── Zip64RequiredException.java
│ │ │ ├── UnixStat.java
│ │ │ ├── ZipConstants.java
│ │ │ ├── UnicodePathExtraField.java
│ │ │ ├── UnicodeCommentExtraField.java
│ │ │ ├── ZipExtraField.java
│ │ │ └── FallbackZipEncoding.java
│ │ ├── jar
│ │ │ ├── package.html
│ │ │ ├── JarArchiveOutputStream.java
│ │ │ ├── JarArchiveInputStream.java
│ │ │ └── JarArchiveEntry.java
│ │ ├── dump
│ │ │ ├── ShortFileException.java
│ │ │ ├── UnrecognizedFormatException.java
│ │ │ ├── DumpArchiveException.java
│ │ │ ├── UnsupportedCompressionAlgorithmException.java
│ │ │ ├── InvalidFormatException.java
│ │ │ ├── Dirent.java
│ │ │ ├── package.html
│ │ │ └── DumpArchiveConstants.java
│ │ ├── tar
│ │ │ ├── package.html
│ │ │ └── TarArchiveSparseEntry.java
│ │ ├── ArchiveEntry.java
│ │ ├── ArchiveException.java
│ │ └── Lister.java
│ │ └── changes
│ │ ├── package.html
│ │ ├── Change.java
│ │ └── ChangeSetResults.java
│ └── assembly
│ ├── src.xml
│ └── bin.xml
├── .gitattributes
├── NOTICE.txt
├── .classpath
├── README.txt
├── .project
├── RELEASE-NOTES.txt
├── PROPOSAL.txt
├── pmd-ruleset.xml
├── META-INF
└── MANIFEST.MF
└── doap_compress.rdf
/.gitignore:
--------------------------------------------------------------------------------
1 | target
2 | .project
3 | .classpath
4 | .settings
5 | *~
6 | /bin
7 |
--------------------------------------------------------------------------------
/src/test/resources/bla.dump:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/puppetlabs/commons-compress/trunk/src/test/resources/bla.dump
--------------------------------------------------------------------------------
/src/test/resources/bla.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/puppetlabs/commons-compress/trunk/src/test/resources/bla.jar
--------------------------------------------------------------------------------
/src/test/resources/bla.pack:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/puppetlabs/commons-compress/trunk/src/test/resources/bla.pack
--------------------------------------------------------------------------------
/src/test/resources/bla.tgz:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/puppetlabs/commons-compress/trunk/src/test/resources/bla.tgz
--------------------------------------------------------------------------------
/src/test/resources/bla.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/puppetlabs/commons-compress/trunk/src/test/resources/bla.zip
--------------------------------------------------------------------------------
/src/test/resources/moby.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/puppetlabs/commons-compress/trunk/src/test/resources/moby.zip
--------------------------------------------------------------------------------
/src/test/resources/bla.tar.xz:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/puppetlabs/commons-compress/trunk/src/test/resources/bla.tar.xz
--------------------------------------------------------------------------------
/src/test/resources/bla.z.dump:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/puppetlabs/commons-compress/trunk/src/test/resources/bla.z.dump
--------------------------------------------------------------------------------
/src/test/resources/test1.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Contains utilities used internally by the compress library. Provides a unified API and factories for dealing with
22 | compressed streams. Provides stream classes for reading and writing archives using
22 | the AR format. Provides stream classes for reading and writing archives using
22 | the CPIO format. Provides a unified API and factories for dealing with archives
22 | in different formats. Provides stream classes for reading and writing archives using
22 | the ZIP format. Provides stream classes for compressing and decompressing
22 | streams using the BZip2 algorithm. Provides stream classes for reading and writing archives using
22 | the ZIP format with some extensions for the special case of JAR
23 | archives. EXPERIMENTAL support for changesets that are applied to
22 | archives. This API is considered unstable and may be modified or even
25 | removed in future releases. Provides stream classes for compressing and decompressing
22 | streams using the GZip algorithm. The classes in this package are wrappers around {@link
25 | java.util.zip.GZIPInputStream java.util.zip.GZIPInputStream} and
26 | {@link java.util.zip.GZIPOutputStream
27 | java.util.zip.GZIPOutputStream}. Provides stream classes for reading and writing archives using
22 | the TAR format. There are many different format dialects that call themselves
25 | TAR. The classes of this package can read and write archives in
26 | the traditional pre-POSIX ustar format and support GNU
27 | specific extensions for long filenames that GNU tar itself by
28 | now refers to as oldgnu. This will cause a {@link Zip64RequiredException} to be
38 | * thrown if {@link ZipArchiveOutputStream} detects it needs Zip64
39 | * support.
27 | * The C structure for a sparse entry is:
28 | * This package provides stream classes for reading archives
22 | using the Unix DUMP format. This format is similar to (and
23 | contemporary with) TAR but reads the raw filesystem directly.
24 | This means that writers are filesystem-specific even though the
25 | created archives are filesystem-agnostic.
26 | Unlike other formats DUMP offers clean support for sparse files,
29 | extended attributes, and other file metadata. In addition DUMP
30 | supports incremental dump files can capture (most) file deletion.
31 | It also provides a native form of compression and will soon support
32 | native encryption as well.
33 | In practice TAR archives are used for both distribution
36 | and backups. DUMP archives are used exclusively for backups.
37 | Like any 30+-year-old application there are a number of variants.
40 | For pragmatic reasons we will only support archives with the
41 | 'new' tape header and inode formats. Other restrictions:
42 |
43 |
29 | * struct posix_header {
30 | * struct sparse sp[21]; // TarConstants.SPARSELEN_GNU_SPARSE - offset 0
31 | * char isextended; // TarConstants.ISEXTENDEDLEN_GNU_SPARSE - offset 504
32 | * };
33 | *
34 | * Whereas, "struct sparse" is:
35 | *
36 | * struct sparse {
37 | * char offset[12]; // offset 0
38 | * char numbytes[12]; // offset 12
39 | * };
40 | *
41 | */
42 |
43 | public class TarArchiveSparseEntry implements TarConstants {
44 | /** If an extension sparse header follows. */
45 | private boolean isExtended;
46 |
47 | /**
48 | * Construct an entry from an archive's header bytes. File is set
49 | * to null.
50 | *
51 | * @param headerBuf The header bytes from a tar archive entry.
52 | * @throws IOException on unknown format
53 | */
54 | public TarArchiveSparseEntry(byte[] headerBuf) throws IOException {
55 | int offset = 0;
56 | offset += SPARSELEN_GNU_SPARSE;
57 | isExtended = TarUtils.parseBoolean(headerBuf, offset);
58 | }
59 |
60 | public boolean isExtended() {
61 | return isExtended;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/commons/compress/ChainingTestCase.java:
--------------------------------------------------------------------------------
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 | */
18 |
19 | package org.apache.commons.compress;
20 |
21 | import java.io.File;
22 | import java.io.FileInputStream;
23 |
24 | import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
25 | import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
26 | import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
27 | import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
28 |
29 |
30 | public class ChainingTestCase extends AbstractTestCase {
31 |
32 | public void testTarGzip() throws Exception {
33 | File file = new File("src/test/resources/bla.tgz");
34 | final TarArchiveInputStream is = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(file)));
35 | final TarArchiveEntry entry = (TarArchiveEntry)is.getNextEntry();
36 | assertNotNull(entry);
37 | assertEquals("test1.xml", entry.getName());
38 | is.close();
39 | }
40 |
41 | public void testTarBzip2() throws Exception {
42 | File file = new File("src/test/resources/bla.tar.bz2");
43 | final TarArchiveInputStream is = new TarArchiveInputStream(new BZip2CompressorInputStream(new FileInputStream(file)));
44 | final TarArchiveEntry entry = (TarArchiveEntry)is.getNextEntry();
45 | assertNotNull(entry);
46 | assertEquals("test1.xml", entry.getName());
47 | is.close();
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/commons/compress/archivers/cpio/CpioUtilTest.java:
--------------------------------------------------------------------------------
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 | * http://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.cpio;
20 |
21 | import static org.junit.Assert.assertArrayEquals;
22 | import static org.junit.Assert.assertEquals;
23 |
24 | import org.junit.Test;
25 |
26 | public class CpioUtilTest {
27 |
28 | @Test
29 | public void oldBinMagic2ByteArrayNotSwapped() {
30 | assertArrayEquals(new byte[] { (byte) 0xc7, 0x71 },
31 | CpioUtil.long2byteArray(CpioConstants.MAGIC_OLD_BINARY,
32 | 2, false));
33 | }
34 |
35 | @Test
36 | public void oldBinMagic2ByteArraySwapped() {
37 | assertArrayEquals(new byte[] { 0x71, (byte) 0xc7, },
38 | CpioUtil.long2byteArray(CpioConstants.MAGIC_OLD_BINARY,
39 | 2, true));
40 | }
41 |
42 | @Test
43 | public void oldBinMagicFromByteArrayNotSwapped() {
44 | assertEquals(CpioConstants.MAGIC_OLD_BINARY,
45 | CpioUtil.byteArray2long(new byte[] { (byte) 0xc7, 0x71 },
46 | false));
47 | }
48 |
49 | @Test
50 | public void oldBinMagicFromByteArraySwapped() {
51 | assertEquals(CpioConstants.MAGIC_OLD_BINARY,
52 | CpioUtil.byteArray2long(new byte[] { 0x71, (byte) 0xc7 },
53 | true));
54 | }
55 |
56 | }
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/compress/archivers/dump/Dirent.java:
--------------------------------------------------------------------------------
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 | * http://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.dump;
20 |
21 | /**
22 | * Directory entry.
23 | */
24 | class Dirent {
25 | private int ino;
26 | private int parentIno;
27 | private int type;
28 | private String name;
29 |
30 | /**
31 | * Constructor
32 | *
33 | * @param ino
34 | * @param parentIno
35 | * @param type
36 | * @param name
37 | */
38 | Dirent(int ino, int parentIno, int type, String name) {
39 | this.ino = ino;
40 | this.parentIno = parentIno;
41 | this.type = type;
42 | this.name = name;
43 | }
44 |
45 | /**
46 | * Get ino.
47 | * @return the i-node
48 | */
49 | int getIno() {
50 | return ino;
51 | }
52 |
53 | /**
54 | * Get ino of parent directory.
55 | * @return the parent i-node
56 | */
57 | int getParentIno() {
58 | return parentIno;
59 | }
60 |
61 | /**
62 | * Get entry type.
63 | * @return the entry type
64 | */
65 | int getType() {
66 | return type;
67 | }
68 |
69 | /**
70 | * Get name of directory entry.
71 | * @return the directory name
72 | */
73 | String getName() {
74 | return name;
75 | }
76 |
77 | /**
78 | * @see java.lang.Object#toString()
79 | */
80 | @Override
81 | public String toString() {
82 | return String.format("[%d]: %s", Integer.valueOf(ino), name);
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/compress/compressors/pack200/StreamBridge.java:
--------------------------------------------------------------------------------
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 | * http://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 |
20 | package org.apache.commons.compress.compressors.pack200;
21 |
22 | import java.io.FilterOutputStream;
23 | import java.io.IOException;
24 | import java.io.InputStream;
25 | import java.io.OutputStream;
26 |
27 | /**
28 | * Provides an InputStream to read all data written to this
29 | * OutputStream.
30 | *
31 | * @ThreadSafe
32 | * @since 1.3
33 | */
34 | abstract class StreamBridge extends FilterOutputStream {
35 | private InputStream input;
36 | private final Object INPUT_LOCK = new Object();
37 |
38 | protected StreamBridge(OutputStream out) {
39 | super(out);
40 | }
41 |
42 | protected StreamBridge() {
43 | this(null);
44 | }
45 |
46 | /**
47 | * Provides the input view.
48 | */
49 | InputStream getInput() throws IOException {
50 | synchronized (INPUT_LOCK) {
51 | if (input == null) {
52 | input = getInputView();
53 | }
54 | }
55 | return input;
56 | }
57 |
58 | /**
59 | * Creates the input view.
60 | */
61 | abstract InputStream getInputView() throws IOException;
62 |
63 | /**
64 | * Closes input and output and releases all associated resources.
65 | */
66 | void stop() throws IOException {
67 | close();
68 | synchronized (INPUT_LOCK) {
69 | if (input != null) {
70 | input.close();
71 | input = null;
72 | }
73 | }
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStreamTest.java:
--------------------------------------------------------------------------------
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 | */
18 |
19 | package org.apache.commons.compress.archivers.ar;
20 |
21 | import java.io.BufferedInputStream;
22 | import java.io.FileInputStream;
23 |
24 | import org.apache.commons.compress.AbstractTestCase;
25 | import org.apache.commons.compress.archivers.ArchiveEntry;
26 | import org.apache.commons.compress.utils.ArchiveUtils;
27 |
28 | public class ArArchiveInputStreamTest extends AbstractTestCase {
29 |
30 | public void testReadLongNamesGNU() throws Exception {
31 | checkLongNameEntry("longfile_gnu.ar");
32 | }
33 |
34 | public void testReadLongNamesBSD() throws Exception {
35 | checkLongNameEntry("longfile_bsd.ar");
36 | }
37 |
38 | private void checkLongNameEntry(String archive) throws Exception {
39 | FileInputStream fis = new FileInputStream(getFile(archive));
40 | ArArchiveInputStream s = null;
41 | try {
42 | s = new ArArchiveInputStream(new BufferedInputStream(fis));
43 | ArchiveEntry e = s.getNextEntry();
44 | assertEquals("this_is_a_long_file_name.txt", e.getName());
45 | assertEquals(14, e.getSize());
46 | byte[] hello = new byte[14];
47 | s.read(hello);
48 | assertEquals("Hello, world!\n", ArchiveUtils.toAsciiString(hello));
49 | assertNull(s.getNextEntry());
50 | } finally {
51 | if (s != null) {
52 | s.close();
53 | }
54 | fis.close();
55 | }
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/compress/utils/CountingOutputStream.java:
--------------------------------------------------------------------------------
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 | * http://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.utils;
20 |
21 | import java.io.FilterOutputStream;
22 | import java.io.IOException;
23 | import java.io.OutputStream;
24 |
25 | /**
26 | * Stream that tracks the number of bytes read.
27 | * @since 1.3
28 | * @NotThreadSafe
29 | */
30 | public class CountingOutputStream extends FilterOutputStream {
31 | private long bytesWritten = 0;
32 |
33 | public CountingOutputStream(final OutputStream out) {
34 | super(out);
35 | }
36 |
37 | @Override
38 | public void write(int b) throws IOException {
39 | out.write(b);
40 | count(1);
41 | }
42 | @Override
43 | public void write(byte[] b) throws IOException {
44 | write(b, 0, b.length);
45 | }
46 | @Override
47 | public void write(byte[] b, int off, int len) throws IOException {
48 | out.write(b, off, len);
49 | count(len);
50 | }
51 |
52 | /**
53 | * Increments the counter of already written bytes.
54 | * Doesn't increment if the EOF has been hit (written == -1)
55 | *
56 | * @param written the number of bytes written
57 | */
58 | protected void count(long written) {
59 | if (written != -1) {
60 | bytesWritten += written;
61 | }
62 | }
63 |
64 | /**
65 | * Returns the current number of bytes written to this stream.
66 | * @return the number of written bytes
67 | */
68 | public long getBytesWritten() {
69 | return bytesWritten;
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/compress/compressors/CompressorInputStream.java:
--------------------------------------------------------------------------------
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 | * http://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.compressors;
20 |
21 | import java.io.InputStream;
22 |
23 | public abstract class CompressorInputStream extends InputStream {
24 | private long bytesRead = 0;
25 |
26 | /**
27 | * Increments the counter of already read bytes.
28 | * Doesn't increment if the EOF has been hit (read == -1)
29 | *
30 | * @param read the number of bytes read
31 | *
32 | * @since 1.1
33 | */
34 | protected void count(int read) {
35 | count((long) read);
36 | }
37 |
38 | /**
39 | * Increments the counter of already read bytes.
40 | * Doesn't increment if the EOF has been hit (read == -1)
41 | *
42 | * @param read the number of bytes read
43 | */
44 | protected void count(long read) {
45 | if(read != -1) {
46 | bytesRead = bytesRead + read;
47 | }
48 | }
49 |
50 | /**
51 | * Returns the current number of bytes read from this stream.
52 | * @return the number of read bytes
53 | * @deprecated this method may yield wrong results for large
54 | * archives, use #getBytesRead instead
55 | */
56 | @Deprecated
57 | public int getCount() {
58 | return (int) bytesRead;
59 | }
60 |
61 | /**
62 | * Returns the current number of bytes read from this stream.
63 | * @return the number of read bytes
64 | *
65 | * @since 1.1
66 | */
67 | public long getBytesRead() {
68 | return bytesRead;
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java:
--------------------------------------------------------------------------------
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 | * http://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.jar;
20 |
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 |
24 | import org.apache.commons.compress.archivers.ArchiveEntry;
25 | import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
26 | import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
27 |
28 | /**
29 | * Implements an input stream that can read entries from jar files.
30 | *
31 | * @NotThreadSafe
32 | */
33 | public class JarArchiveInputStream extends ZipArchiveInputStream {
34 |
35 | public JarArchiveInputStream( final InputStream inputStream ) {
36 | super(inputStream);
37 | }
38 |
39 | public JarArchiveEntry getNextJarEntry() throws IOException {
40 | ZipArchiveEntry entry = getNextZipEntry();
41 | return entry == null ? null : new JarArchiveEntry(entry);
42 | }
43 |
44 | @Override
45 | public ArchiveEntry getNextEntry() throws IOException {
46 | return getNextJarEntry();
47 | }
48 |
49 | /**
50 | * Checks if the signature matches what is expected for a jar file
51 | * (in this case it is the same as for a zip file).
52 | *
53 | * @param signature
54 | * the bytes to check
55 | * @param length
56 | * the number of bytes to check
57 | * @return true, if this stream is a jar archive stream, false otherwise
58 | */
59 | public static boolean matches(byte[] signature, int length ) {
60 | return ZipArchiveInputStream.matches(signature, length);
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/commons/compress/utils/CharsetsTest.java:
--------------------------------------------------------------------------------
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 |
18 | package org.apache.commons.compress.utils;
19 |
20 | import java.nio.charset.Charset;
21 |
22 | import junit.framework.Assert;
23 |
24 | import org.junit.Test;
25 |
26 | /**
27 | * Sanity checks for {@link Charsets}.
28 | *
29 | * @version $Id$
30 | */
31 | public class CharsetsTest {
32 |
33 | @Test
34 | public void testToCharset() {
35 | Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null));
36 | Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null));
37 | Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset()));
38 | Assert.assertEquals(Charset.forName("UTF-8"), Charsets.toCharset(Charset.forName("UTF-8")));
39 | }
40 |
41 | @Test
42 | public void testIso8859_1() {
43 | Assert.assertEquals("ISO-8859-1", Charsets.ISO_8859_1.name());
44 | }
45 |
46 | @Test
47 | public void testUsAscii() {
48 | Assert.assertEquals("US-ASCII", Charsets.US_ASCII.name());
49 | }
50 |
51 | @Test
52 | public void testUtf16() {
53 | Assert.assertEquals("UTF-16", Charsets.UTF_16.name());
54 | }
55 |
56 | @Test
57 | public void testUtf16Be() {
58 | Assert.assertEquals("UTF-16BE", Charsets.UTF_16BE.name());
59 | }
60 |
61 | @Test
62 | public void testUtf16Le() {
63 | Assert.assertEquals("UTF-16LE", Charsets.UTF_16LE.name());
64 | }
65 |
66 | @Test
67 | public void testUtf8() {
68 | Assert.assertEquals("UTF-8", Charsets.UTF_8.name());
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/src/test/resources/longpath/files.txt:
--------------------------------------------------------------------------------
1 | # Licensed to the Apache Software Foundation (ASF) under one or more
2 | # contributor license agreements. See the NOTICE file distributed with
3 | # this work for additional information regarding copyright ownership.
4 | # The ASF licenses this file to You under the Apache License, Version 2.0
5 | # (the "License"); you may not use this file except in compliance with
6 | # the License. You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 | #
16 | #
17 | # File prefixes correspond to the following OSes:
18 | # cygwin - CYGWIN_NT-5.1 1.7.1(0.218/5/3)
19 | # hudson - SunOS 5.10
20 | # minotaur - FreeBSD 8.0-STABLE
21 | # vmgump - Linux 2.6.24-27-server
22 | # winXP_ant - Ant 1.7.1 on Windows/XP
23 | #
24 | compress-test/
25 | compress-test/dir1/
26 | compress-test/dir1/dir2/
27 | compress-test/dir1/dir2/dir3/
28 | compress-test/dir1/dir2/dir3/dir4/
29 | compress-test/dir1/dir2/dir3/dir4/dir5/
30 | compress-test/dir1/dir2/dir3/dir4/dir5/dir6/
31 | compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/
32 | compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/
33 | compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/
34 | compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/
35 | compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file12345678901234567890.txt
36 | compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file1234567890123456789012345678901234567890123456789012345678901234567890.txt
37 | compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file123456789012345678901234567890123456789012345678901234567890.txt
38 | compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file.txt
39 | compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file123456789012345678901234567890.txt
40 | compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file1234567890.txt
41 | compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file1234567890123456789012345678901234567890.txt
42 | compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file12345678901234567890123456789012345678901234567890.txt
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/compress/utils/CountingInputStream.java:
--------------------------------------------------------------------------------
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 | * http://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.utils;
20 |
21 | import java.io.FilterInputStream;
22 | import java.io.IOException;
23 | import java.io.InputStream;
24 |
25 | /**
26 | * Stream that tracks the number of bytes read.
27 | * @since 1.3
28 | * @NotThreadSafe
29 | */
30 | public class CountingInputStream extends FilterInputStream {
31 | private long bytesRead;
32 |
33 | public CountingInputStream(final InputStream in) {
34 | super(in);
35 | }
36 |
37 | @Override
38 | public int read() throws IOException {
39 | int r = in.read();
40 | if (r >= 0) {
41 | count(1);
42 | }
43 | return r;
44 | }
45 | @Override
46 | public int read(byte[] b) throws IOException {
47 | return read(b, 0, b.length);
48 | }
49 | @Override
50 | public int read(byte[] b, int off, int len) throws IOException {
51 | int r = in.read(b, off, len);
52 | if (r >= 0) {
53 | count(r);
54 | }
55 | return r;
56 | }
57 | /**
58 | * Increments the counter of already read bytes.
59 | * Doesn't increment if the EOF has been hit (read == -1)
60 | *
61 | * @param read the number of bytes read
62 | */
63 | protected final void count(long read) {
64 | if (read != -1) {
65 | bytesRead += read;
66 | }
67 | }
68 |
69 | /**
70 | * Returns the current number of bytes read from this stream.
71 | * @return the number of read bytes
72 | */
73 | public long getBytesRead() {
74 | return bytesRead;
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveEntry.java:
--------------------------------------------------------------------------------
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 | * http://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.jar;
20 |
21 | import java.security.cert.Certificate;
22 | import java.util.jar.Attributes;
23 | import java.util.jar.JarEntry;
24 | import java.util.zip.ZipEntry;
25 | import java.util.zip.ZipException;
26 |
27 | import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
28 |
29 | /**
30 | *
31 | * @NotThreadSafe
32 | */
33 | public class JarArchiveEntry extends ZipArchiveEntry {
34 |
35 | private Attributes manifestAttributes = null;
36 | private Certificate[] certificates = null;
37 |
38 | public JarArchiveEntry(ZipEntry entry) throws ZipException {
39 | super(entry);
40 | }
41 |
42 | public JarArchiveEntry(String name) {
43 | super(name);
44 | }
45 |
46 | public JarArchiveEntry(ZipArchiveEntry entry) throws ZipException {
47 | super(entry);
48 | }
49 |
50 | public JarArchiveEntry(JarEntry entry) throws ZipException {
51 | super(entry);
52 |
53 | }
54 |
55 | public Attributes getManifestAttributes() {
56 | return manifestAttributes;
57 | }
58 |
59 | public Certificate[] getCertificates() {
60 | if (certificates != null) {
61 | Certificate[] certs = new Certificate[certificates.length];
62 | System.arraycopy(certificates, 0, certs, 0, certs.length);
63 | return certs;
64 | }
65 | return null;
66 | }
67 |
68 | @Override
69 | public boolean equals(Object o) {
70 | return super.equals(o);
71 | }
72 |
73 | @Override
74 | public int hashCode() {
75 | return super.hashCode();
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/compress/archivers/dump/package.html:
--------------------------------------------------------------------------------
1 |
2 |
20 |
21 |
44 |
51 |
As of Apache Commons Compress 1.3 support for the dump format is 54 | read-only.
55 | 56 | 57 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/compress/utils/IOUtils.java: -------------------------------------------------------------------------------- 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 | * http://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.utils; 20 | 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | import java.io.OutputStream; 24 | 25 | /** 26 | * Utility functions 27 | * @Immutable 28 | */ 29 | public final class IOUtils { 30 | 31 | /** Private constructor to prevent instantiation of this utility class. */ 32 | private IOUtils(){ 33 | } 34 | 35 | /** 36 | * Copies the content of a InputStream into an OutputStream. 37 | * Uses a default buffer size of 8024 bytes. 38 | * 39 | * @param input 40 | * the InputStream to copy 41 | * @param output 42 | * the target Stream 43 | * @throws IOException 44 | * if an error occurs 45 | */ 46 | public static long copy(final InputStream input, final OutputStream output) throws IOException { 47 | return copy(input, output, 8024); 48 | } 49 | 50 | /** 51 | * Copies the content of a InputStream into an OutputStream 52 | * 53 | * @param input 54 | * the InputStream to copy 55 | * @param output 56 | * the target Stream 57 | * @param buffersize 58 | * the buffer size to use 59 | * @throws IOException 60 | * if an error occurs 61 | */ 62 | public static long copy(final InputStream input, final OutputStream output, int buffersize) throws IOException { 63 | final byte[] buffer = new byte[buffersize]; 64 | int n = 0; 65 | long count=0; 66 | while (-1 != (n = input.read(buffer))) { 67 | output.write(buffer, 0, n); 68 | count += n; 69 | } 70 | return count; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/site/xdoc/conventions.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 |26 | The developers of this component decided to follow the recommended standards 27 | but not to include Checkstyle (or similar tools) into Commons Compress. 28 |
29 |32 | Commons Compress does not aim to be threadsafe at the moment. But the developers 33 | agreed to document multithreading behaviour in the javadocs. 34 |
35 |36 | We use some of the annotations from 37 | JCIP 38 | as JavaDoc tags. The used tags are: 39 | 40 |
The name of the archive must be given as a command line argument.
30 | *The optional second argument defines the archive type, in case the format is not recognised.
31 | * 32 | * @since 1.1 33 | */ 34 | public final class Lister { 35 | private static final ArchiveStreamFactory factory = new ArchiveStreamFactory(); 36 | 37 | public static void main(String[] args) throws Exception { 38 | if (args.length == 0) { 39 | usage(); 40 | return; 41 | } 42 | System.out.println("Analysing "+args[0]); 43 | File f = new File(args[0]); 44 | if (!f.isFile()) { 45 | System.err.println(f + " doesn't exist or is a directory"); 46 | } 47 | InputStream fis = new BufferedInputStream(new FileInputStream(f)); 48 | ArchiveInputStream ais; 49 | if (args.length > 1) { 50 | ais = factory.createArchiveInputStream(args[1], fis); 51 | } else { 52 | ais = factory.createArchiveInputStream(fis); 53 | } 54 | System.out.println("Created "+ais.toString()); 55 | ArchiveEntry ae; 56 | while((ae=ais.getNextEntry()) != null){ 57 | System.out.println(ae.getName()); 58 | } 59 | ais.close(); 60 | fis.close(); 61 | } 62 | 63 | private static void usage() { 64 | System.out.println("Parameters: archive-name [archive-type]"); 65 | } 66 | 67 | } -------------------------------------------------------------------------------- /src/test/resources/longpath/minotaur.cpio: -------------------------------------------------------------------------------- 1 | 0707077777771502240407750032710032710000030000001137125042400001600000000000compress-test 0707077777771502250407750032710032710000030000001137125043100002300000000000compress-test/dir1 0707077777771536250407750032710032710000030000001137125043600003000000000000compress-test/dir1/dir2 0707077777771664760407750032710032710000030000001137125044100003500000000000compress-test/dir1/dir2/dir3 0707077777771725040407750032710032710000030000001137125044500004200000000000compress-test/dir1/dir2/dir3/dir4 0707077777771734130407750032710032710000030000001137125045100004700000000000compress-test/dir1/dir2/dir3/dir4/dir5 0707077777771734160407750032710032710000030000001137125045500005400000000000compress-test/dir1/dir2/dir3/dir4/dir5/dir6 0707077777771734520407750032710032710000030000001137125046200006100000000000compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7 0707077777771734550407750032710032710000030000001137125047100006600000000000compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8 0707077777771735160407750032710032710000030000001137125051200007300000000000compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9 0707077777771735210407750032710032710000020000001137125064200010100000000000compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10 0707077777771735361006640032710032710000010000001137125060300013600000000000compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file12345678901234567890.txt 0707077777771735641006640032710032710000010000001137125064200022000000000000compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file1234567890123456789012345678901234567890123456789012345678901234567890.txt 0707077777771735541006640032710032710000010000001137125063300020600000000000compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file123456789012345678901234567890123456789012345678901234567890.txt 0707077777771735261006640032710032710000010000001137125054100011200000000000compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file.txt 0707077777771735461006640032710032710000010000001137125061100015000000000000compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file123456789012345678901234567890.txt 0707077777771735331006640032710032710000010000001137125056200012400000000000compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file1234567890.txt 0707077777771735471006640032710032710000010000001137125061700016200000000000compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file1234567890123456789012345678901234567890.txt 0707077777771735511006640032710032710000010000001137125062400017400000000000compress-test/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/file12345678901234567890123456789012345678901234567890.txt 0707070000000000000000000000000000000000010000000000000000000001300000000000TRAILER!!! -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/compress/DetectCompressorTestCase.java: -------------------------------------------------------------------------------- 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 | * http://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; 20 | 21 | import java.io.BufferedInputStream; 22 | import java.io.File; 23 | import java.io.FileInputStream; 24 | import java.io.FileNotFoundException; 25 | import java.net.URL; 26 | 27 | import junit.framework.TestCase; 28 | 29 | import org.apache.commons.compress.compressors.CompressorException; 30 | import org.apache.commons.compress.compressors.CompressorInputStream; 31 | import org.apache.commons.compress.compressors.CompressorStreamFactory; 32 | import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; 33 | import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; 34 | 35 | public final class DetectCompressorTestCase extends TestCase { 36 | 37 | public DetectCompressorTestCase(String name) { 38 | super(name); 39 | } 40 | 41 | final ClassLoader classLoader = getClass().getClassLoader(); 42 | final CompressorStreamFactory factory = new CompressorStreamFactory(); 43 | 44 | public void testDetection() throws Exception { 45 | 46 | final CompressorInputStream bzip2 = getStreamFor("bla.txt.bz2"); 47 | assertNotNull(bzip2); 48 | assertTrue(bzip2 instanceof BZip2CompressorInputStream); 49 | 50 | final CompressorInputStream gzip = getStreamFor("bla.tgz"); 51 | assertNotNull(gzip); 52 | assertTrue(gzip instanceof GzipCompressorInputStream); 53 | 54 | } 55 | 56 | private CompressorInputStream getStreamFor(String resource) 57 | throws CompressorException, FileNotFoundException { 58 | 59 | final URL rsc = classLoader.getResource(resource); 60 | assertNotNull("Could not find resource "+resource,rsc); 61 | return factory.createCompressorInputStream( 62 | new BufferedInputStream(new FileInputStream( 63 | new File(rsc.getFile())))); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStreamTest.java: -------------------------------------------------------------------------------- 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 | * http://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.cpio; 20 | 21 | import java.io.File; 22 | import java.io.FileInputStream; 23 | import java.io.FileOutputStream; 24 | import java.io.InputStream; 25 | 26 | import org.apache.commons.compress.AbstractTestCase; 27 | import org.apache.commons.compress.utils.IOUtils; 28 | 29 | public class CpioArchiveOutputStreamTest extends AbstractTestCase { 30 | 31 | public void testWriteOldBinary() throws Exception { 32 | final File f = getFile("test1.xml"); 33 | final File output = new File(dir, "test.cpio"); 34 | final FileOutputStream out = new FileOutputStream(output); 35 | InputStream in = null; 36 | try { 37 | final CpioArchiveOutputStream os = 38 | new CpioArchiveOutputStream(out, CpioConstants 39 | .FORMAT_OLD_BINARY); 40 | os.putArchiveEntry(new CpioArchiveEntry(CpioConstants 41 | .FORMAT_OLD_BINARY, 42 | f, "test1.xml")); 43 | IOUtils.copy(in = new FileInputStream(f), os); 44 | in.close(); 45 | in = null; 46 | os.closeArchiveEntry(); 47 | os.close(); 48 | } finally { 49 | if (in != null) { 50 | in.close(); 51 | } 52 | out.close(); 53 | } 54 | 55 | try { 56 | in = new CpioArchiveInputStream(new FileInputStream(output)); 57 | CpioArchiveEntry e = ((CpioArchiveInputStream) in) 58 | .getNextCPIOEntry(); 59 | assertEquals("test1.xml", e.getName()); 60 | assertNull(((CpioArchiveInputStream) in).getNextEntry()); 61 | } finally { 62 | if (in != null) { 63 | in.close(); 64 | } 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveConstants.java: -------------------------------------------------------------------------------- 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 | * http://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.dump; 20 | 21 | /** 22 | * Various constants associated with dump archives. 23 | */ 24 | public final class DumpArchiveConstants { 25 | public static final int TP_SIZE = 1024; 26 | public static final int NTREC = 10; 27 | public static final int HIGH_DENSITY_NTREC = 32; 28 | public static final int OFS_MAGIC = 60011; 29 | public static final int NFS_MAGIC = 60012; 30 | public static final int FS_UFS2_MAGIC = 0x19540119; 31 | public static final int CHECKSUM = 84446; 32 | public static final int LBLSIZE = 16; 33 | public static final int NAMELEN = 64; 34 | 35 | /* do not instantiate */ 36 | private DumpArchiveConstants() { 37 | } 38 | 39 | /** 40 | * The type of tape segment. 41 | */ 42 | public enum SEGMENT_TYPE { 43 | TAPE(1), 44 | INODE(2), 45 | BITS(3), 46 | ADDR(4), 47 | END(5), 48 | CLRI(6); 49 | 50 | int code; 51 | 52 | private SEGMENT_TYPE(int code) { 53 | this.code = code; 54 | } 55 | 56 | public static SEGMENT_TYPE find(int code) { 57 | for (SEGMENT_TYPE t : values()) { 58 | if (t.code == code) { 59 | return t; 60 | } 61 | } 62 | 63 | return null; 64 | } 65 | } 66 | 67 | /** 68 | * The type of compression. 69 | */ 70 | public enum COMPRESSION_TYPE { 71 | ZLIB(0), 72 | BZLIB(1), 73 | LZO(2); 74 | 75 | int code; 76 | 77 | private COMPRESSION_TYPE(int code) { 78 | this.code = code; 79 | } 80 | 81 | public static COMPRESSION_TYPE find(int code) { 82 | for (COMPRESSION_TYPE t : values()) { 83 | if (t.code == code) { 84 | return t; 85 | } 86 | } 87 | 88 | return null; 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStreamTest.java: -------------------------------------------------------------------------------- 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 | * http://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.jar; 20 | 21 | import java.io.File; 22 | import java.io.FileOutputStream; 23 | import java.io.IOException; 24 | 25 | import junit.framework.TestCase; 26 | 27 | import org.apache.commons.compress.AbstractTestCase; 28 | import org.apache.commons.compress.archivers.zip.JarMarker; 29 | import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; 30 | import org.apache.commons.compress.archivers.zip.ZipExtraField; 31 | import org.apache.commons.compress.archivers.zip.ZipFile; 32 | 33 | public class JarArchiveOutputStreamTest extends TestCase { 34 | 35 | public void testJarMarker() throws IOException { 36 | File testArchive = File.createTempFile("jar-aostest", ".jar"); 37 | testArchive.deleteOnExit(); 38 | JarArchiveOutputStream out = null; 39 | ZipFile zf = null; 40 | try { 41 | 42 | out = new JarArchiveOutputStream(new FileOutputStream(testArchive)); 43 | out.putArchiveEntry(new ZipArchiveEntry("foo/")); 44 | out.closeArchiveEntry(); 45 | out.putArchiveEntry(new ZipArchiveEntry("bar/")); 46 | out.closeArchiveEntry(); 47 | out.finish(); 48 | out.close(); 49 | out = null; 50 | 51 | zf = new ZipFile(testArchive); 52 | ZipArchiveEntry ze = zf.getEntry("foo/"); 53 | assertNotNull(ze); 54 | ZipExtraField[] fes = ze.getExtraFields(); 55 | assertEquals(1, fes.length); 56 | assertTrue(fes[0] instanceof JarMarker); 57 | 58 | ze = zf.getEntry("bar/"); 59 | assertNotNull(ze); 60 | fes = ze.getExtraFields(); 61 | assertEquals(0, fes.length); 62 | } finally { 63 | if (out != null) { 64 | try { 65 | out.close(); 66 | } catch (IOException e) { /* swallow */ } 67 | } 68 | ZipFile.closeQuietly(zf); 69 | AbstractTestCase.tryHardToDelete(testArchive); 70 | } 71 | } 72 | 73 | } -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/compress/utils/CountingStreamTest.java: -------------------------------------------------------------------------------- 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 | * http://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.utils; 20 | 21 | import static org.junit.Assert.assertArrayEquals; 22 | import static org.junit.Assert.assertEquals; 23 | 24 | import java.io.ByteArrayInputStream; 25 | import java.io.ByteArrayOutputStream; 26 | 27 | import org.junit.Test; 28 | 29 | public class CountingStreamTest { 30 | 31 | @Test 32 | public void output() throws Exception { 33 | // I don't like "test all at once" tests either, but the class 34 | // is so trivial 35 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); 36 | CountingOutputStream o = new CountingOutputStream(bos); 37 | o.write(1); 38 | assertEquals(1, o.getBytesWritten()); 39 | o.write(new byte[] { 2, 3 }); 40 | assertEquals(3, o.getBytesWritten()); 41 | o.write(new byte[] { 2, 3, 4, 5, }, 2, 1); 42 | assertEquals(4, o.getBytesWritten()); 43 | o.count(-1); 44 | assertEquals(4, o.getBytesWritten()); 45 | o.count(-2); 46 | assertEquals(2, o.getBytesWritten()); 47 | o.close(); 48 | assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bos.toByteArray()); 49 | } 50 | 51 | @Test 52 | public void input() throws Exception { 53 | // I don't like "test all at once" tests either, but the class 54 | // is so trivial 55 | ByteArrayInputStream bis = 56 | new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }); 57 | CountingInputStream i = new CountingInputStream(bis); 58 | assertEquals(1, i.read()); 59 | assertEquals(1, i.getBytesRead()); 60 | byte[] b = new byte[2]; 61 | i.read(b); 62 | assertEquals(3, i.getBytesRead()); 63 | assertArrayEquals(new byte[] { 2, 3 }, b); 64 | b = new byte[3]; 65 | i.read(b, 1, 1); 66 | assertArrayEquals(new byte[] { 0, 4, 0 }, b); 67 | assertEquals(4, i.getBytesRead()); 68 | i.count(-1); 69 | assertEquals(4, i.getBytesRead()); 70 | i.count(-2); 71 | assertEquals(2, i.getBytesRead()); 72 | i.close(); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java: -------------------------------------------------------------------------------- 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 | */ 18 | 19 | package org.apache.commons.compress; 20 | 21 | import org.apache.commons.compress.utils.ArchiveUtils; 22 | 23 | public class ArchiveUtilsTest extends AbstractTestCase { 24 | 25 | private static final int bytesToTest = 50; 26 | private static final byte[] byteTest = new byte[bytesToTest]; 27 | static { 28 | for(int i=0; i < byteTest.length ;) { 29 | byteTest[i]=(byte) i; 30 | byteTest[i+1]=(byte) -i; 31 | i += 2; 32 | } 33 | } 34 | public void testCompareBA(){ 35 | byte[] buffer1 = {1,2,3}; 36 | byte[] buffer2 = {1,2,3,0}; 37 | byte[] buffer3 = {1,2,3}; 38 | assertTrue(ArchiveUtils.isEqual(buffer1, buffer2, true)); 39 | assertFalse(ArchiveUtils.isEqual(buffer1, buffer2, false)); 40 | assertFalse(ArchiveUtils.isEqual(buffer1, buffer2)); 41 | assertTrue(ArchiveUtils.isEqual(buffer2, buffer1, true)); 42 | assertFalse(ArchiveUtils.isEqual(buffer2, buffer1, false)); 43 | assertFalse(ArchiveUtils.isEqual(buffer2, buffer1)); 44 | assertTrue(ArchiveUtils.isEqual(buffer1, buffer3)); 45 | assertTrue(ArchiveUtils.isEqual(buffer3, buffer1)); 46 | } 47 | 48 | public void testCompareAscii(){ 49 | byte[] buffer1 = {'a','b','c'}; 50 | byte[] buffer2 = {'d','e','f',0}; 51 | assertTrue(ArchiveUtils.matchAsciiBuffer("abc", buffer1)); 52 | assertFalse(ArchiveUtils.matchAsciiBuffer("abc\0", buffer1)); 53 | assertTrue(ArchiveUtils.matchAsciiBuffer("def\0", buffer2)); 54 | assertFalse(ArchiveUtils.matchAsciiBuffer("def", buffer2)); 55 | } 56 | 57 | public void testAsciiConversions() { 58 | asciiToByteAndBackOK(""); 59 | asciiToByteAndBackOK("abcd"); 60 | asciiToByteAndBackFail("\u8025"); 61 | } 62 | 63 | private void asciiToByteAndBackOK(String inputString) { 64 | assertEquals(inputString, ArchiveUtils.toAsciiString(ArchiveUtils.toAsciiBytes(inputString))); 65 | } 66 | 67 | private void asciiToByteAndBackFail(String inputString) { 68 | assertFalse(inputString.equals(ArchiveUtils.toAsciiString(ArchiveUtils.toAsciiBytes(inputString)))); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/compress/archivers/zip/UnicodePathExtraField.java: -------------------------------------------------------------------------------- 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 | */ 18 | 19 | package org.apache.commons.compress.archivers.zip; 20 | 21 | /** 22 | * Info-ZIP Unicode Path Extra Field (0x7075): 23 | * 24 | *Stores the UTF-8 version of the file name field as stored in the 25 | * local header and central directory header.
26 | * 27 | *
28 | * Value Size Description
29 | * ----- ---- -----------
30 | * (UPath) 0x7075 Short tag for this extra block type ("up")
31 | * TSize Short total data size for this block
32 | * Version 1 byte version of this extra field, currently 1
33 | * NameCRC32 4 bytes File Name Field CRC32 Checksum
34 | * UnicodeName Variable UTF-8 version of the entry File Name
35 | *
36 | * @NotThreadSafe super-class is not thread-safe
37 | */
38 | public class UnicodePathExtraField extends AbstractUnicodeExtraField {
39 |
40 | public static final ZipShort UPATH_ID = new ZipShort(0x7075);
41 |
42 | public UnicodePathExtraField () {
43 | }
44 |
45 | /**
46 | * Assemble as unicode path extension from the name given as
47 | * text as well as the encoded bytes actually written to the archive.
48 | *
49 | * @param text The file name
50 | * @param bytes the bytes actually written to the archive
51 | * @param off The offset of the encoded filename in bytes.
52 | * @param len The length of the encoded filename or comment in
53 | * bytes.
54 | */
55 | public UnicodePathExtraField(String text, byte[] bytes, int off, int len) {
56 | super(text, bytes, off, len);
57 | }
58 |
59 | /**
60 | * Assemble as unicode path extension from the name given as
61 | * text as well as the encoded bytes actually written to the archive.
62 | *
63 | * @param name The file name
64 | * @param bytes the bytes actually written to the archive
65 | */
66 | public UnicodePathExtraField(String name, byte[] bytes) {
67 | super(name, bytes);
68 | }
69 |
70 | /** {@inheritDoc} */
71 | public ZipShort getHeaderId() {
72 | return UPATH_ID;
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/commons/compress/archivers/zip/ZipShortTest.java:
--------------------------------------------------------------------------------
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 | */
18 |
19 | package org.apache.commons.compress.archivers.zip;
20 |
21 | import junit.framework.TestCase;
22 |
23 | /**
24 | * JUnit 3 testcases for org.apache.commons.compress.archivers.zip.ZipShort.
25 | *
26 | */
27 | public class ZipShortTest extends TestCase {
28 |
29 | public ZipShortTest(String name) {
30 | super(name);
31 | }
32 |
33 | /**
34 | * Test conversion to bytes.
35 | */
36 | public void testToBytes() {
37 | ZipShort zs = new ZipShort(0x1234);
38 | byte[] result = zs.getBytes();
39 | assertEquals("length getBytes", 2, result.length);
40 | assertEquals("first byte getBytes", 0x34, result[0]);
41 | assertEquals("second byte getBytes", 0x12, result[1]);
42 | }
43 |
44 | /**
45 | * Test conversion from bytes.
46 | */
47 | public void testFromBytes() {
48 | byte[] val = new byte[] {0x34, 0x12};
49 | ZipShort zs = new ZipShort(val);
50 | assertEquals("value from bytes", 0x1234, zs.getValue());
51 | }
52 |
53 | /**
54 | * Test the contract of the equals method.
55 | */
56 | public void testEquals() {
57 | ZipShort zs = new ZipShort(0x1234);
58 | ZipShort zs2 = new ZipShort(0x1234);
59 | ZipShort zs3 = new ZipShort(0x5678);
60 |
61 | assertTrue("reflexive", zs.equals(zs));
62 |
63 | assertTrue("works", zs.equals(zs2));
64 | assertTrue("works, part two", !zs.equals(zs3));
65 |
66 | assertTrue("symmetric", zs2.equals(zs));
67 |
68 | assertTrue("null handling", !zs.equals(null));
69 | assertTrue("non ZipShort handling", !zs.equals(new Integer(0x1234)));
70 | }
71 |
72 | /**
73 | * Test sign handling.
74 | */
75 | public void testSign() {
76 | ZipShort zs = new ZipShort(new byte[] {(byte)0xFF, (byte)0xFF});
77 | assertEquals(0x0000FFFF, zs.getValue());
78 | }
79 |
80 | public void testClone() {
81 | ZipShort s1 = new ZipShort(42);
82 | ZipShort s2 = (ZipShort) s1.clone();
83 | assertNotSame(s1, s2);
84 | assertEquals(s1, s2);
85 | assertEquals(s1.getValue(), s2.getValue());
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/commons/compress/compressors/XZUtilsTestCase.java:
--------------------------------------------------------------------------------
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 | * http://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.compressors;
20 |
21 | import junit.framework.TestCase;
22 |
23 | import org.apache.commons.compress.compressors.xz.XZUtils;
24 |
25 | public class XZUtilsTestCase extends TestCase {
26 |
27 | public void testIsCompressedFilename() {
28 | assertFalse(XZUtils.isCompressedFilename(""));
29 | assertFalse(XZUtils.isCompressedFilename(".xz"));
30 |
31 | assertTrue(XZUtils.isCompressedFilename("x.txz"));
32 | assertTrue(XZUtils.isCompressedFilename("x.xz"));
33 | assertTrue(XZUtils.isCompressedFilename("x-xz"));
34 |
35 | assertFalse(XZUtils.isCompressedFilename("xxgz"));
36 | assertFalse(XZUtils.isCompressedFilename("xzz"));
37 | assertFalse(XZUtils.isCompressedFilename("xaz"));
38 |
39 | assertFalse(XZUtils.isCompressedFilename("x.txz "));
40 | assertFalse(XZUtils.isCompressedFilename("x.txz\n"));
41 | assertFalse(XZUtils.isCompressedFilename("x.txz.y"));
42 | }
43 |
44 | public void testGetUncompressedFilename() {
45 | assertEquals("", XZUtils.getUncompressedFilename(""));
46 | assertEquals(".xz", XZUtils.getUncompressedFilename(".xz"));
47 |
48 | assertEquals("x.tar", XZUtils.getUncompressedFilename("x.txz"));
49 | assertEquals("x", XZUtils.getUncompressedFilename("x.xz"));
50 | assertEquals("x", XZUtils.getUncompressedFilename("x-xz"));
51 |
52 | assertEquals("x.txz ", XZUtils.getUncompressedFilename("x.txz "));
53 | assertEquals("x.txz\n", XZUtils.getUncompressedFilename("x.txz\n"));
54 | assertEquals("x.txz.y", XZUtils.getUncompressedFilename("x.txz.y"));
55 | }
56 |
57 | public void testGetCompressedFilename() {
58 | assertEquals(".xz", XZUtils.getCompressedFilename(""));
59 | assertEquals("x.xz", XZUtils.getCompressedFilename("x"));
60 |
61 | assertEquals("x.txz", XZUtils.getCompressedFilename("x.tar"));
62 |
63 | assertEquals("x.wmf .xz", XZUtils.getCompressedFilename("x.wmf "));
64 | assertEquals("x.wmf\n.xz", XZUtils.getCompressedFilename("x.wmf\n"));
65 | assertEquals("x.wmf.y.xz", XZUtils.getCompressedFilename("x.wmf.y"));
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/compress/archivers/zip/UnicodeCommentExtraField.java:
--------------------------------------------------------------------------------
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 | */
18 |
19 | package org.apache.commons.compress.archivers.zip;
20 |
21 | /**
22 | * Info-ZIP Unicode Comment Extra Field (0x6375):
23 | *
24 | * Stores the UTF-8 version of the file comment as stored in the 25 | * central directory header.
26 | * 27 | *
28 | * Value Size Description
29 | * ----- ---- -----------
30 | * (UCom) 0x6375 Short tag for this extra block type ("uc")
31 | * TSize Short total data size for this block
32 | * Version 1 byte version of this extra field, currently 1
33 | * ComCRC32 4 bytes Comment Field CRC32 Checksum
34 | * UnicodeCom Variable UTF-8 version of the entry comment
35 | *
36 | * @NotThreadSafe super-class is not thread-safe
37 | */
38 | public class UnicodeCommentExtraField extends AbstractUnicodeExtraField {
39 |
40 | public static final ZipShort UCOM_ID = new ZipShort(0x6375);
41 |
42 | public UnicodeCommentExtraField () {
43 | }
44 |
45 | /**
46 | * Assemble as unicode comment extension from the name given as
47 | * text as well as the encoded bytes actually written to the archive.
48 | *
49 | * @param text The file name
50 | * @param bytes the bytes actually written to the archive
51 | * @param off The offset of the encoded comment in bytes.
52 | * @param len The length of the encoded comment or comment in
53 | * bytes.
54 | */
55 | public UnicodeCommentExtraField(String text, byte[] bytes, int off,
56 | int len) {
57 | super(text, bytes, off, len);
58 | }
59 |
60 | /**
61 | * Assemble as unicode comment extension from the comment given as
62 | * text as well as the bytes actually written to the archive.
63 | *
64 | * @param comment The file comment
65 | * @param bytes the bytes actually written to the archive
66 | */
67 | public UnicodeCommentExtraField(String comment, byte[] bytes) {
68 | super(comment, bytes);
69 | }
70 |
71 | /** {@inheritDoc} */
72 | public ZipShort getHeaderId() {
73 | return UCOM_ID;
74 | }
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/commons/compress/archivers/tar/BigFilesIT.java:
--------------------------------------------------------------------------------
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 | */
18 |
19 | package org.apache.commons.compress.archivers.tar;
20 |
21 | import static org.junit.Assert.assertEquals;
22 | import static org.junit.Assert.assertNotNull;
23 | import static org.junit.Assert.assertNull;
24 |
25 | import java.util.Random;
26 |
27 | import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
28 | import org.junit.Test;
29 |
30 | public class BigFilesIT {
31 |
32 | @Test
33 | public void readFileBiggerThan8GByteStar() throws Exception {
34 | readFileBiggerThan8GByte("/8.star.tar.gz");
35 | }
36 |
37 | @Test
38 | public void readFileBiggerThan8GBytePosix() throws Exception {
39 | readFileBiggerThan8GByte("/8.posix.tar.gz");
40 | }
41 |
42 | private void readFileBiggerThan8GByte(String name) throws Exception {
43 | GzipCompressorInputStream in = null;
44 | TarArchiveInputStream tin = null;
45 | try {
46 | in =
47 | new GzipCompressorInputStream(BigFilesIT.class
48 | .getResourceAsStream(name));
49 | tin = new TarArchiveInputStream(in);
50 | TarArchiveEntry e = tin.getNextTarEntry();
51 | assertNotNull(e);
52 | assertEquals(8200l * 1024 * 1024, e.getSize());
53 |
54 | long read = 0;
55 | Random r = new Random(System.currentTimeMillis());
56 | int readNow;
57 | byte[] buf = new byte[1024 * 1024];
58 | while ((readNow = tin.read(buf, 0, buf.length)) > 0) {
59 | // testing all bytes for a value of 0 is going to take
60 | // too long, just pick a few ones randomly
61 | for (int i = 0; i < 100; i++) {
62 | int idx = r.nextInt(readNow);
63 | assertEquals("testing byte " + (read + idx), 0, buf[idx]);
64 | }
65 | read += readNow;
66 | }
67 | assertEquals(8200l * 1024 * 1024, read);
68 | assertNull(tin.getNextTarEntry());
69 | } finally {
70 | if (tin != null) {
71 | tin.close();
72 | }
73 | if (in != null) {
74 | in.close();
75 | }
76 | }
77 | }
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java:
--------------------------------------------------------------------------------
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 | */
18 |
19 | package org.apache.commons.compress.archivers.zip;
20 |
21 | import static org.junit.Assert.assertEquals;
22 | import static org.junit.Assert.assertTrue;
23 |
24 | import java.io.BufferedInputStream;
25 | import java.io.File;
26 | import java.io.FileInputStream;
27 | import java.net.URI;
28 | import java.net.URL;
29 |
30 | import org.junit.Ignore;
31 | import org.junit.Test;
32 |
33 | public class ZipArchiveInputStreamTest {
34 |
35 | /**
36 | * @see "https://issues.apache.org/jira/browse/COMPRESS-176"
37 | */
38 | @Test
39 | public void winzipBackSlashWorkaround() throws Exception {
40 | URL zip = getClass().getResource("/test-winzip.zip");
41 | ZipArchiveInputStream in = null;
42 | try {
43 | in = new ZipArchiveInputStream(new FileInputStream(new File(new URI(zip.toString()))));
44 | ZipArchiveEntry zae = in.getNextZipEntry();
45 | zae = in.getNextZipEntry();
46 | zae = in.getNextZipEntry();
47 | assertEquals("\u00e4/", zae.getName());
48 | } finally {
49 | if (in != null) {
50 | in.close();
51 | }
52 | }
53 | }
54 |
55 | /**
56 | * @see "https://issues.apache.org/jira/browse/COMPRESS-189"
57 | */
58 | @Test
59 | @Ignore
60 | public void properUseOfInflater() throws Exception {
61 | URL zip = getClass().getResource("/COMPRESS-189.zip");
62 | ZipFile zf = null;
63 | ZipArchiveInputStream in = null;
64 | try {
65 | zf = new ZipFile(new File(new URI(zip.toString())));
66 | ZipArchiveEntry zae = zf.getEntry("USD0558682-20080101.ZIP");
67 | in = new ZipArchiveInputStream(new BufferedInputStream(zf.getInputStream(zae)));
68 | ZipArchiveEntry innerEntry;
69 | while ((innerEntry = in.getNextZipEntry()) != null) {
70 | if (innerEntry.getName().endsWith("XML")) {
71 | assertTrue(0 < in.read());
72 | }
73 | }
74 | } finally {
75 | if (zf != null) {
76 | zf.close();
77 | }
78 | if (in != null) {
79 | in.close();
80 | }
81 | }
82 | }
83 |
84 | }
85 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/compress/changes/Change.java:
--------------------------------------------------------------------------------
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 | * http://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.changes;
20 |
21 | import java.io.InputStream;
22 |
23 | import org.apache.commons.compress.archivers.ArchiveEntry;
24 |
25 | /**
26 | * Change holds meta information about a change.
27 | *
28 | * @Immutable
29 | */
30 | class Change {
31 | private final String targetFile; // entry name to delete
32 | private final ArchiveEntry entry; // new entry to add
33 | private final InputStream input; // source for new entry
34 | private final boolean replaceMode; // change should replaceMode existing entries
35 |
36 | // Type of change
37 | private final int type;
38 | // Possible type values
39 | static final int TYPE_DELETE = 1;
40 | static final int TYPE_ADD = 2;
41 | static final int TYPE_MOVE = 3; // NOT USED
42 | static final int TYPE_DELETE_DIR = 4;
43 |
44 | /**
45 | * Constructor. Takes the filename of the file to be deleted
46 | * from the stream as argument.
47 | * @param pFilename the filename of the file to delete
48 | */
49 | Change(final String pFilename, int type) {
50 | if(pFilename == null) {
51 | throw new NullPointerException();
52 | }
53 | this.targetFile = pFilename;
54 | this.type = type;
55 | this.input = null;
56 | this.entry = null;
57 | this.replaceMode = true;
58 | }
59 |
60 | /**
61 | * Construct a change which adds an entry.
62 | *
63 | * @param pEntry the entry details
64 | * @param pInput the InputStream for the entry data
65 | */
66 | Change(final ArchiveEntry pEntry, final InputStream pInput, boolean replace) {
67 | if(pEntry == null || pInput == null) {
68 | throw new NullPointerException();
69 | }
70 | this.entry = pEntry;
71 | this.input = pInput;
72 | type = TYPE_ADD;
73 | targetFile = null;
74 | this.replaceMode = replace;
75 | }
76 |
77 | ArchiveEntry getEntry() {
78 | return entry;
79 | }
80 |
81 | InputStream getInput() {
82 | return input;
83 | }
84 |
85 | String targetFile() {
86 | return targetFile;
87 | }
88 |
89 | int type() {
90 | return type;
91 | }
92 |
93 | boolean isReplaceMode() {
94 | return replaceMode;
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/commons/compress/archivers/zip/ZipLongTest.java:
--------------------------------------------------------------------------------
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 | */
18 |
19 | package org.apache.commons.compress.archivers.zip;
20 |
21 | import junit.framework.TestCase;
22 |
23 | /**
24 | * JUnit 3 testcases for org.apache.commons.compress.archivers.zip.ZipLong.
25 | *
26 | */
27 | public class ZipLongTest extends TestCase {
28 |
29 | public ZipLongTest(String name) {
30 | super(name);
31 | }
32 |
33 | /**
34 | * Test conversion to bytes.
35 | */
36 | public void testToBytes() {
37 | ZipLong zl = new ZipLong(0x12345678);
38 | byte[] result = zl.getBytes();
39 | assertEquals("length getBytes", 4, result.length);
40 | assertEquals("first byte getBytes", 0x78, result[0]);
41 | assertEquals("second byte getBytes", 0x56, result[1]);
42 | assertEquals("third byte getBytes", 0x34, result[2]);
43 | assertEquals("fourth byte getBytes", 0x12, result[3]);
44 | }
45 |
46 | /**
47 | * Test conversion from bytes.
48 | */
49 | public void testFromBytes() {
50 | byte[] val = new byte[] {0x78, 0x56, 0x34, 0x12};
51 | ZipLong zl = new ZipLong(val);
52 | assertEquals("value from bytes", 0x12345678, zl.getValue());
53 | }
54 |
55 | /**
56 | * Test the contract of the equals method.
57 | */
58 | public void testEquals() {
59 | ZipLong zl = new ZipLong(0x12345678);
60 | ZipLong zl2 = new ZipLong(0x12345678);
61 | ZipLong zl3 = new ZipLong(0x87654321);
62 |
63 | assertTrue("reflexive", zl.equals(zl));
64 |
65 | assertTrue("works", zl.equals(zl2));
66 | assertTrue("works, part two", !zl.equals(zl3));
67 |
68 | assertTrue("symmetric", zl2.equals(zl));
69 |
70 | assertTrue("null handling", !zl.equals(null));
71 | assertTrue("non ZipLong handling", !zl.equals(new Integer(0x1234)));
72 | }
73 |
74 | /**
75 | * Test sign handling.
76 | */
77 | public void testSign() {
78 | ZipLong zl = new ZipLong(new byte[] {(byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF});
79 | assertEquals(0x00000000FFFFFFFFl, zl.getValue());
80 | }
81 |
82 | public void testClone() {
83 | ZipLong s1 = new ZipLong(42);
84 | ZipLong s2 = (ZipLong) s1.clone();
85 | assertNotSame(s1, s2);
86 | assertEquals(s1, s2);
87 | assertEquals(s1.getValue(), s2.getValue());
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStreamTest.java:
--------------------------------------------------------------------------------
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 | */
18 |
19 | package org.apache.commons.compress.archivers.ar;
20 |
21 | import java.io.ByteArrayOutputStream;
22 | import java.io.File;
23 | import java.io.FileOutputStream;
24 | import java.io.IOException;
25 | import java.util.ArrayList;
26 | import java.util.List;
27 |
28 | import org.apache.commons.compress.AbstractTestCase;
29 |
30 | public class ArArchiveOutputStreamTest extends AbstractTestCase {
31 |
32 | public void testLongFileNamesCauseExceptionByDefault() {
33 | ArArchiveOutputStream os = null;
34 | try {
35 | os = new ArArchiveOutputStream(new ByteArrayOutputStream());
36 | ArArchiveEntry ae = new ArArchiveEntry("this_is_a_long_name.txt",
37 | 0);
38 | os.putArchiveEntry(ae);
39 | fail("Expected an exception");
40 | } catch (IOException ex) {
41 | assertTrue(ex.getMessage().startsWith("filename too long"));
42 | } finally {
43 | closeQuietly(os);
44 | }
45 | }
46 |
47 | public void testLongFileNamesWorkUsingBSDDialect() throws Exception {
48 | FileOutputStream fos = null;
49 | ArArchiveOutputStream os = null;
50 | File[] df = createTempDirAndFile();
51 | try {
52 | fos = new FileOutputStream(df[1]);
53 | os = new ArArchiveOutputStream(fos);
54 | os.setLongFileMode(ArArchiveOutputStream.LONGFILE_BSD);
55 | ArArchiveEntry ae = new ArArchiveEntry("this_is_a_long_name.txt",
56 | 14);
57 | os.putArchiveEntry(ae);
58 | os.write(new byte[] {
59 | 'H', 'e', 'l', 'l', 'o', ',', ' ',
60 | 'w', 'o', 'r', 'l', 'd', '!', '\n'
61 | });
62 | os.closeArchiveEntry();
63 | os.close();
64 | os = null;
65 | fos = null;
66 |
67 | ListExtra fields usually appear twice per file, once in the local 27 | * file data and once in the central directory. Usually they are the 28 | * same, but they don't have to be. {@link 29 | * java.util.zip.ZipOutputStream java.util.zip.ZipOutputStream} will 30 | * only use the local file data in both places.
31 | * 32 | */ 33 | public interface ZipExtraField { 34 | /** 35 | * The Header-ID. 36 | * 37 | * @return The HeaderId value 38 | */ 39 | ZipShort getHeaderId(); 40 | 41 | /** 42 | * Length of the extra field in the local file data - without 43 | * Header-ID or length specifier. 44 | * @return the length of the field in the local file data 45 | */ 46 | ZipShort getLocalFileDataLength(); 47 | 48 | /** 49 | * Length of the extra field in the central directory - without 50 | * Header-ID or length specifier. 51 | * @return the length of the field in the central directory 52 | */ 53 | ZipShort getCentralDirectoryLength(); 54 | 55 | /** 56 | * The actual data to put into local file data - without Header-ID 57 | * or length specifier. 58 | * @return the data 59 | */ 60 | byte[] getLocalFileDataData(); 61 | 62 | /** 63 | * The actual data to put into central directory - without Header-ID or 64 | * length specifier. 65 | * @return the data 66 | */ 67 | byte[] getCentralDirectoryData(); 68 | 69 | /** 70 | * Populate data from this array as if it was in local file data. 71 | * 72 | * @param buffer the buffer to read data from 73 | * @param offset offset into buffer to read data 74 | * @param length the length of data 75 | * @exception ZipException on error 76 | */ 77 | void parseFromLocalFileData(byte[] buffer, int offset, int length) 78 | throws ZipException; 79 | 80 | /** 81 | * Populate data from this array as if it was in central directory data. 82 | * 83 | * @param buffer the buffer to read data from 84 | * @param offset offset into buffer to read data 85 | * @param length the length of data 86 | * @exception ZipException on error 87 | */ 88 | void parseFromCentralDirectoryData(byte[] buffer, int offset, int length) 89 | throws ZipException; 90 | } 91 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/compress/changes/ChangeSetResults.java: -------------------------------------------------------------------------------- 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 | * http://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.changes; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | /** 25 | * Stores the results of an performed ChangeSet operation. 26 | */ 27 | public class ChangeSetResults { 28 | private final ListThis implementation is not suitable for encodings other than 29 | * utf-8, because java.io encodes unmappable character as question 30 | * marks leading to unreadable ZIP entries on some operating 31 | * systems.
32 | * 33 | *Furthermore this implementation is unable to tell whether a 34 | * given name can be safely encoded or not.
35 | * 36 | *This implementation acts as a last resort implementation, when 37 | * neither {@link Simple8BitZipEnoding} nor {@link NioZipEncoding} is 38 | * available.
39 | * 40 | *The methods of this class are reentrant.
41 | * @Immutable 42 | */ 43 | class FallbackZipEncoding implements ZipEncoding { 44 | private final String charset; 45 | 46 | /** 47 | * Construct a fallback zip encoding, which uses the platform's 48 | * default charset. 49 | */ 50 | public FallbackZipEncoding() { 51 | this.charset = null; 52 | } 53 | 54 | /** 55 | * Construct a fallback zip encoding, which uses the given charset. 56 | * 57 | * @param charset The name of the charset or {@code null} for 58 | * the platform's default character set. 59 | */ 60 | public FallbackZipEncoding(String charset) { 61 | this.charset = charset; 62 | } 63 | 64 | /** 65 | * @see 66 | * org.apache.commons.compress.archivers.zip.ZipEncoding#canEncode(java.lang.String) 67 | */ 68 | public boolean canEncode(String name) { 69 | return true; 70 | } 71 | 72 | /** 73 | * @see 74 | * org.apache.commons.compress.archivers.zip.ZipEncoding#encode(java.lang.String) 75 | */ 76 | public ByteBuffer encode(String name) throws IOException { 77 | if (this.charset == null) { // i.e. use default charset, see no-args constructor 78 | return ByteBuffer.wrap(name.getBytes()); 79 | } else { 80 | return ByteBuffer.wrap(name.getBytes(this.charset)); 81 | } 82 | } 83 | 84 | /** 85 | * @see 86 | * org.apache.commons.compress.archivers.zip.ZipEncoding#decode(byte[]) 87 | */ 88 | public String decode(byte[] data) throws IOException { 89 | if (this.charset == null) { // i.e. use default charset, see no-args constructor 90 | return new String(data); 91 | } else { 92 | return new String(data,this.charset); 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java: -------------------------------------------------------------------------------- 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 | */ 18 | 19 | package org.apache.commons.compress.archivers.zip; 20 | 21 | import java.io.File; 22 | import java.io.FileInputStream; 23 | import java.io.IOException; 24 | import java.net.URI; 25 | import java.net.URISyntaxException; 26 | import java.net.URL; 27 | 28 | import junit.framework.TestCase; 29 | 30 | public class EncryptedArchiveTest extends TestCase { 31 | 32 | public void testReadPasswordEncryptedEntryViaZipFile() 33 | throws IOException, URISyntaxException { 34 | URL zip = getClass().getResource("/password-encrypted.zip"); 35 | File file = new File(new URI(zip.toString())); 36 | ZipFile zf = null; 37 | try { 38 | zf = new ZipFile(file); 39 | ZipArchiveEntry zae = zf.getEntry("LICENSE.txt"); 40 | assertTrue(zae.getGeneralPurposeBit().usesEncryption()); 41 | assertFalse(zae.getGeneralPurposeBit().usesStrongEncryption()); 42 | assertFalse(zf.canReadEntryData(zae)); 43 | try { 44 | zf.getInputStream(zae); 45 | fail("expected an exception"); 46 | } catch (UnsupportedZipFeatureException ex) { 47 | assertSame(UnsupportedZipFeatureException.Feature.ENCRYPTION, 48 | ex.getFeature()); 49 | } 50 | } finally { 51 | ZipFile.closeQuietly(zf); 52 | } 53 | } 54 | 55 | public void testReadPasswordEncryptedEntryViaStream() 56 | throws IOException, URISyntaxException { 57 | URL zip = getClass().getResource("/password-encrypted.zip"); 58 | File file = new File(new URI(zip.toString())); 59 | ZipArchiveInputStream zin = null; 60 | try { 61 | zin = new ZipArchiveInputStream(new FileInputStream(file)); 62 | ZipArchiveEntry zae = zin.getNextZipEntry(); 63 | assertEquals("LICENSE.txt", zae.getName()); 64 | assertTrue(zae.getGeneralPurposeBit().usesEncryption()); 65 | assertFalse(zae.getGeneralPurposeBit().usesStrongEncryption()); 66 | assertFalse(zin.canReadEntryData(zae)); 67 | try { 68 | byte[] buf = new byte[1024]; 69 | zin.read(buf, 0, buf.length); 70 | fail("expected an exception"); 71 | } catch (UnsupportedZipFeatureException ex) { 72 | assertSame(UnsupportedZipFeatureException.Feature.ENCRYPTION, 73 | ex.getFeature()); 74 | } 75 | } finally { 76 | if (zin != null) { 77 | zin.close(); 78 | } 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /doap_compress.rdf: -------------------------------------------------------------------------------- 1 | 2 | 18 |