Index: src/test/java/org/apache/commons/compress/archivers/JarTestCase.java =================================================================== --- src/test/java/org/apache/commons/compress/archivers/JarTestCase.java (revision 920799) +++ src/test/java/org/apache/commons/compress/archivers/JarTestCase.java (working copy) @@ -24,7 +24,7 @@ import java.io.InputStream; import java.io.OutputStream; -import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.compress.archivers.jar.JarArchiveEntry; import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.compress.AbstractTestCase; @@ -40,11 +40,11 @@ final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("jar", out); - os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml")); + os.putArchiveEntry(new JarArchiveEntry("testdata/test1.xml")); IOUtils.copy(new FileInputStream(file1), os); os.closeArchiveEntry(); - os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml")); + os.putArchiveEntry(new JarArchiveEntry("testdata/test2.xml")); IOUtils.copy(new FileInputStream(file2), os); os.closeArchiveEntry(); @@ -57,21 +57,21 @@ final InputStream is = new FileInputStream(input); final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("jar", is); - ZipArchiveEntry entry = (ZipArchiveEntry)in.getNextEntry(); + JarArchiveEntry entry = (JarArchiveEntry)in.getNextEntry(); File o = new File(dir, entry.getName()); o.getParentFile().mkdirs(); OutputStream out = new FileOutputStream(o); IOUtils.copy(in, out); out.close(); - entry = (ZipArchiveEntry)in.getNextEntry(); + entry = (JarArchiveEntry)in.getNextEntry(); o = new File(dir, entry.getName()); o.getParentFile().mkdirs(); out = new FileOutputStream(o); IOUtils.copy(in, out); out.close(); - entry = (ZipArchiveEntry)in.getNextEntry(); + entry = (JarArchiveEntry)in.getNextEntry(); o = new File(dir, entry.getName()); o.getParentFile().mkdirs(); out = new FileOutputStream(o); Index: src/test/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStreamTest.java =================================================================== --- src/test/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStreamTest.java (revision 920799) +++ src/test/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStreamTest.java (working copy) @@ -23,6 +23,7 @@ import java.io.IOException; import junit.framework.TestCase; +import org.apache.commons.compress.archivers.jar.JarArchiveEntry; import org.apache.commons.compress.archivers.zip.JarMarker; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipExtraField; @@ -30,16 +31,16 @@ public class JarArchiveOutputStreamTest extends TestCase { - public void testJarMarker() throws IOException { + public void testJarMarker() throws Exception { File testArchive = File.createTempFile("jar-aostest", ".jar"); JarArchiveOutputStream out = null; ZipFile zf = null; try { out = new JarArchiveOutputStream(new FileOutputStream(testArchive)); - out.putArchiveEntry(new ZipArchiveEntry("foo/")); + out.putArchiveEntry(new JarArchiveEntry("foo/")); out.closeArchiveEntry(); - out.putArchiveEntry(new ZipArchiveEntry("bar/")); + out.putArchiveEntry(new JarArchiveEntry("bar/")); out.closeArchiveEntry(); out.finish(); out.close(); Index: src/test/java/org/apache/commons/compress/DetectArchiverTestCase.java =================================================================== --- src/test/java/org/apache/commons/compress/DetectArchiverTestCase.java (revision 920799) +++ src/test/java/org/apache/commons/compress/DetectArchiverTestCase.java (working copy) @@ -87,9 +87,10 @@ checkEmptyArchive("cpio"); } - public void testEmptyJarArchive() throws Exception { - checkEmptyArchive("jar"); - } + // jars can not be empty +// public void testEmptyJarArchive() throws Exception { +// checkEmptyArchive("jar"); +// } // empty tar archives just have 512 null bytes // public void testEmptyTarArchive() throws Exception { Index: src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveEntry.java =================================================================== --- src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveEntry.java (revision 920799) +++ src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveEntry.java (working copy) @@ -18,24 +18,19 @@ */ package org.apache.commons.compress.archivers.jar; -import java.security.cert.Certificate; -import java.util.jar.Attributes; +import java.util.Date; import java.util.jar.JarEntry; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import org.apache.commons.compress.archivers.ArchiveEntry; -import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; /** - * + * Adapts {@link JarEntry} to {@link ArchiveEntry}. * @NotThreadSafe */ -public class JarArchiveEntry extends ZipArchiveEntry implements ArchiveEntry { +public class JarArchiveEntry extends JarEntry implements ArchiveEntry { - private Attributes manifestAttributes = null; - private Certificate[] certificates = null; - public JarArchiveEntry(ZipEntry entry) throws ZipException { super(entry); } @@ -44,39 +39,14 @@ super(name); } - public JarArchiveEntry(ZipArchiveEntry entry) throws ZipException { - super(entry); - } - public JarArchiveEntry(JarEntry entry) throws ZipException { super(entry); } - public Attributes getManifestAttributes() { - return manifestAttributes; + /** {@inheritDocs} */ + public Date getLastModifiedDate() { + return new Date(getTime()); } - public Certificate[] getCertificates() { - if (certificates != null) { - Certificate[] certs = new Certificate[certificates.length]; - System.arraycopy(certificates, 0, certs, 0, certs.length); - return certs; - } - return null; - } - - /* (non-Javadoc) - * @see org.apache.commons.compress.archivers.zip.ZipArchiveEntry#equals(java.lang.Object) - */ - public boolean equals(Object obj) { - return super.equals(obj); - } - - /* (non-Javadoc) - * @see org.apache.commons.compress.archivers.zip.ZipArchiveEntry#hashCode() - */ - public int hashCode() { - return super.hashCode(); - } } Index: src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java =================================================================== --- src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java (revision 920799) +++ src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java (working copy) @@ -20,9 +20,12 @@ import java.io.IOException; import java.io.InputStream; +import java.util.jar.JarEntry; +import java.util.jar.JarInputStream; import org.apache.commons.compress.archivers.ArchiveEntry; -import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.compress.archivers.ArchiveException; +import org.apache.commons.compress.archivers.ArchiveInputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; /** @@ -30,14 +33,23 @@ * * @NotThreadSafe */ -public class JarArchiveInputStream extends ZipArchiveInputStream { +public class JarArchiveInputStream extends ArchiveInputStream { - public JarArchiveInputStream( final InputStream inputStream ) { - super(inputStream); + private final JarInputStream in; + + public JarArchiveInputStream(final InputStream inputStream) + throws ArchiveException { + JarInputStream i = null; + try { + i = new JarInputStream(inputStream); + } catch (IOException ex) { + throw new ArchiveException("failed to create stream", ex); + } + in = i; } public JarArchiveEntry getNextJarEntry() throws IOException { - ZipArchiveEntry entry = getNextZipEntry(); + JarEntry entry = in.getNextJarEntry(); return entry == null ? null : new JarArchiveEntry(entry); } @@ -45,6 +57,22 @@ return getNextJarEntry(); } + public int read(byte[] b, int off, int len) throws IOException { + return in.read(b, off, len); + } + + public void close() throws IOException { + in.close(); + } + + public long skip(long value) throws IOException { + return in.skip(value); + } + + public int available() throws IOException { + return in.available(); + } + public static boolean matches(byte[] signature, int length ) { return ZipArchiveInputStream.matches(signature, length); } Index: src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStream.java =================================================================== --- src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStream.java (revision 920799) +++ src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStream.java (working copy) @@ -18,35 +18,119 @@ */ package org.apache.commons.compress.archivers.jar; +import java.io.File; import java.io.IOException; import java.io.OutputStream; +import java.util.jar.JarEntry; +import java.util.jar.JarOutputStream; import org.apache.commons.compress.archivers.ArchiveEntry; -import org.apache.commons.compress.archivers.zip.JarMarker; +import org.apache.commons.compress.archivers.ArchiveException; +import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; -import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; /** - * Subclass that adds a special extra field to the very first entry - * which allows the created archive to be used as an executable jar on - * Solaris. + * Implements an output stream that can read entries from jar files. * * @NotThreadSafe */ -public class JarArchiveOutputStream extends ZipArchiveOutputStream { +public class JarArchiveOutputStream extends ArchiveOutputStream { - private boolean jarMarkerAdded = false; + private final JarOutputStream out; + private boolean unclosedEntry = false; - public JarArchiveOutputStream(final OutputStream out) { - super(out); + public JarArchiveOutputStream(final OutputStream out) + throws ArchiveException { + JarOutputStream o = null; + try { + o = new JarOutputStream(out); + } catch (IOException ex) { + throw new ArchiveException("failed to create stream", ex); + } + this.out = o; } // @throws ClassCastException if entry is not an instance of ZipArchiveEntry public void putArchiveEntry(ArchiveEntry ze) throws IOException { - if (!jarMarkerAdded) { - ((ZipArchiveEntry)ze).addAsFirstExtraField(JarMarker.getInstance()); - jarMarkerAdded = true; + if (unclosedEntry) { + closeArchiveEntry(); } - super.putArchiveEntry(ze); + out.putNextEntry((JarArchiveEntry) ze); + unclosedEntry = true; } + + public void finish() throws IOException { + if (unclosedEntry) { + throw new IOException("This archives contains unclosed entries."); + } + out.finish(); + } + + /** + * Writes all necessary data for this entry. + * @throws IOException on error + */ + public void closeArchiveEntry() throws IOException { + if (!unclosedEntry) { + throw new IOException("No current entry to close"); + } + out.closeEntry(); + unclosedEntry = false; + } + + public void close() throws IOException { + if (unclosedEntry) { + throw new IOException("This archives contains unclosed entries."); + } + out.close(); + } + + /** + * Set the file comment. + * @param comment the comment + */ + public void setComment(String comment) { + out.setComment(comment); + } + + /** + * Sets the compression level for subsequent entries. + * + *
Default is Deflater.DEFAULT_COMPRESSION.
+ * @param level the compression level. + * @throws IllegalArgumentException if an invalid compression + * level is specified. + */ + public void setLevel(int level) { + out.setLevel(level); + } + + /** + * Sets the default compression method for subsequent entries. + * + *Default is DEFLATED.
+ * @param method anint
from java.util.zip.ZipEntry
+ */
+ public void setMethod(int method) {
+ out.setMethod(method);
+ }
+
+ public void write(byte[] b, int offset, int length) throws IOException {
+ out.write(b, offset, length);
+ }
+
+ /**
+ * Flushes this output stream and forces any buffered output bytes
+ * to be written out to the stream.
+ *
+ * @exception IOException if an I/O error occurs.
+ */
+ public void flush() throws IOException {
+ out.flush();
+ }
+
+ public ArchiveEntry createArchiveEntry(File inputFile, String entryName)
+ throws IOException {
+ return new JarArchiveEntry(new ZipArchiveEntry(inputFile, entryName));
+ }
}
Index: src/main/java/org/apache/commons/compress/archivers/jar/package.html
===================================================================
--- src/main/java/org/apache/commons/compress/archivers/jar/package.html (revision 920799)
+++ src/main/java/org/apache/commons/compress/archivers/jar/package.html (working copy)
@@ -19,7 +19,6 @@
-->
Provides stream classes for reading and writing archives using - the ZIP format with some extensions for the special case of JAR - archives.
+ JAR files based on thejava.util.jar
package.