diff --git a/src/main/org/apache/tools/zip/ZipEntry.java b/src/main/org/apache/tools/zip/ZipEntry.java index ef2296f3c..351de763d 100644 --- a/src/main/org/apache/tools/zip/ZipEntry.java +++ b/src/main/org/apache/tools/zip/ZipEntry.java @@ -68,7 +68,11 @@ import java.util.zip.ZipException; */ public class ZipEntry extends java.util.zip.ZipEntry { + private static final int PLATFORM_UNIX = 3; + private static final int PLATFORM_FAT = 0; + private int internalAttributes = 0; + private int platform = PLATFORM_FAT; private long externalAttributes = 0; private Vector extraFields = new Vector(); @@ -186,6 +190,32 @@ public class ZipEntry extends java.util.zip.ZipEntry { externalAttributes = value; } + /** + * Sets Unix permissions in a way that is understood by Info-Zip's + * unzip command. + * + * @since Ant 1.6 + */ + public void setUnixMode(int mode) { + setExternalAttributes((mode << 16) + // MS-DOS directory flag + | (isDirectory() ? 0x10 : 0)); + platform = PLATFORM_UNIX; + } + + /** + * Platform specification to put into the "version made + * by" part of the central file header. + * + * @return 0 (MS-DOS FAT) unless {@link #setUnixMode setUnixMode} + * has been called, in which case 3 (Unix) will be returned. + * + * @since Ant 1.6 + */ + public int getPlatform() { + return platform; + } + /** * Replaces all currently attached extra fields with the new array. * diff --git a/src/main/org/apache/tools/zip/ZipOutputStream.java b/src/main/org/apache/tools/zip/ZipOutputStream.java index 4348c5683..e406822df 100644 --- a/src/main/org/apache/tools/zip/ZipOutputStream.java +++ b/src/main/org/apache/tools/zip/ZipOutputStream.java @@ -519,7 +519,7 @@ public class ZipOutputStream extends DeflaterOutputStream { written += 4; // version made by - out.write((new ZipShort(20)).getBytes()); + out.write((new ZipShort((ze.getPlatform() << 8) | 20)).getBytes()); written += 2; // version needed to extract diff --git a/src/testcases/org/apache/tools/zip/ZipEntryTest.java b/src/testcases/org/apache/tools/zip/ZipEntryTest.java index 77e304cf1..9633e06ef 100644 --- a/src/testcases/org/apache/tools/zip/ZipEntryTest.java +++ b/src/testcases/org/apache/tools/zip/ZipEntryTest.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2001 The Apache Software Foundation. All rights + * Copyright (c) 2001-2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -121,4 +121,22 @@ public class ZipEntryTest extends TestCase { } catch (java.util.NoSuchElementException nse) { } } + + public void testUnixMode() { + ZipEntry ze = new ZipEntry("foo"); + assertEquals(0, ze.getPlatform()); + ze.setUnixMode(0755); + assertEquals(3, ze.getPlatform()); + assertEquals(0755, + (ze.getExternalAttributes() >> 16) & 0xFFFF); + assertEquals(0, ze.getExternalAttributes() & 0xFFFF); + + ze = new ZipEntry("foo/"); + ze.setUnixMode(0577); + assertEquals(3, ze.getPlatform()); + assertEquals(0577, + (ze.getExternalAttributes() >> 16) & 0xFFFF); + assertEquals(0x10, ze.getExternalAttributes() & 0xFFFF); + } + }