Author: bodewig Date: Wed Feb 18 15:52:42 2009 New Revision: 745545 URL: http://svn.apache.org/viewvc?rev=745545&view=rev Log: Add a JarMarker to the very first entry in a jar. SANDBOX-289
Added: commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/jar/ commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStreamTest.java (with props) Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStream.java commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java (contents, props changed) commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntryTest.java (contents, props changed) Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStream.java URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStream.java?rev=745545&r1=745544&r2=745545&view=diff ============================================================================== --- commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStream.java (original) +++ commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStream.java Wed Feb 18 15:52:42 2009 @@ -22,17 +22,28 @@ import java.io.OutputStream; import org.apache.commons.compress.archivers.ArchiveEntry; +import org.apache.commons.compress.archivers.zip.JarMarker; 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. + */ public class JarArchiveOutputStream extends ZipArchiveOutputStream { - public JarArchiveOutputStream( final OutputStream out ) { + private boolean jarMarkerAdded = false; + + public JarArchiveOutputStream(final OutputStream out) { super(out); } - public void putArchiveEntry(ArchiveEntry entry) throws IOException { - // TODO special jar stuff - super.putArchiveEntry((ZipArchiveEntry) entry); + public void putNextEntry(ZipArchiveEntry ze) throws IOException { + if (!jarMarkerAdded) { + ze.addAsFirstExtraField(JarMarker.getInstance()); + jarMarkerAdded = true; + } + super.putNextEntry(ze); } } Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java?rev=745545&r1=745544&r2=745545&view=diff ============================================================================== --- commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java (original) +++ commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java Wed Feb 18 15:52:42 2009 @@ -216,6 +216,9 @@ /** * Adds an extra fields - replacing an already present extra field * of the same type. + * + * <p>If no extra field of the same type exists, the field will be + * added as last field.</p> * @param ze an extra field * @since 1.1 */ @@ -228,6 +231,25 @@ } /** + * Adds an extra fields - replacing an already present extra field + * of the same type. + * + * <p>The new extra field will be the first one.</p> + * @param ze an extra field + * @since 1.1 + */ + public void addAsFirstExtraField(ZipExtraField ze) { + LinkedHashMap copy = extraFields; + extraFields = new LinkedHashMap(); + extraFields.put(ze.getHeaderId(), ze); + if (copy != null) { + copy.remove(ze.getHeaderId()); + extraFields.putAll(copy); + } + setExtra(); + } + + /** * Remove an extra fields. * @param type the type of extra field to remove * @since 1.1 Propchange: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Wed Feb 18 15:52:42 2009 @@ -0,0 +1 @@ +/ant/core/trunk/src/main/org/apache/tools/zip/ZipEntry.java:745537 Added: commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStreamTest.java URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStreamTest.java?rev=745545&view=auto ============================================================================== --- commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStreamTest.java (added) +++ commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStreamTest.java Wed Feb 18 15:52:42 2009 @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.compress.archivers.jar; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import junit.framework.TestCase; + +import org.apache.commons.compress.archivers.zip.JarMarker; +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.compress.archivers.zip.ZipExtraField; +import org.apache.commons.compress.archivers.zip.ZipFile; + +public class JarArchiveOutputStreamTest extends TestCase { + + public void testJarMarker() throws IOException { + 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.closeEntry(); + out.putArchiveEntry(new ZipArchiveEntry("bar/")); + out.closeEntry(); + out.close(); + out = null; + + zf = new ZipFile(testArchive); + ZipArchiveEntry ze = zf.getEntry("foo/"); + assertNotNull(ze); + ZipExtraField[] fes = ze.getExtraFields(); + assertEquals(1, fes.length); + assertTrue(fes[0] instanceof JarMarker); + + ze = zf.getEntry("bar/"); + assertNotNull(ze); + fes = ze.getExtraFields(); + assertEquals(0, fes.length); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { /* swallow */ } + } + ZipFile.closeQuietly(zf); + if (testArchive.exists()) { + testArchive.delete(); + } + } + } + +} \ No newline at end of file Propchange: commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntryTest.java URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntryTest.java?rev=745545&r1=745544&r2=745545&view=diff ============================================================================== --- commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntryTest.java (original) +++ commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntryTest.java Wed Feb 18 15:52:42 2009 @@ -85,6 +85,46 @@ } } + /** + * test handling of extra fields + * + * @since 1.1 + */ + public void testAddAsFirstExtraField() { + AsiExtraField a = new AsiExtraField(); + a.setDirectory(true); + a.setMode(0755); + UnrecognizedExtraField u = new UnrecognizedExtraField(); + u.setHeaderId(new ZipShort(1)); + u.setLocalFileDataData(new byte[0]); + + ZipArchiveEntry ze = new ZipArchiveEntry("test/"); + ze.setExtraFields(new ZipExtraField[] {a, u}); + byte[] data1 = ze.getExtra(); + + UnrecognizedExtraField u2 = new UnrecognizedExtraField(); + u2.setHeaderId(new ZipShort(1)); + u2.setLocalFileDataData(new byte[] {1}); + + ze.addAsFirstExtraField(u2); + byte[] data2 = ze.getExtra(); + ZipExtraField[] result = ze.getExtraFields(); + assertEquals("second pass", 2, result.length); + assertSame(u2, result[0]); + assertSame(a, result[1]); + assertEquals("length second pass", data1.length + 1, data2.length); + + UnrecognizedExtraField u3 = new UnrecognizedExtraField(); + u3.setHeaderId(new ZipShort(2)); + u3.setLocalFileDataData(new byte[] {1}); + ze.addAsFirstExtraField(u3); + result = ze.getExtraFields(); + assertEquals("third pass", 3, result.length); + assertSame(u3, result[0]); + assertSame(u2, result[1]); + assertSame(a, result[2]); + } + public void testUnixMode() { ZipArchiveEntry ze = new ZipArchiveEntry("foo"); assertEquals(0, ze.getPlatform()); Propchange: commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntryTest.java ------------------------------------------------------------------------------ --- svn:mergeinfo (added) +++ svn:mergeinfo Wed Feb 18 15:52:42 2009 @@ -0,0 +1 @@ +/ant/core/trunk/src/tests/junit/org/apache/tools/zip/ZipEntryTest.java:745537