< prev index next >

src/java.base/share/classes/java/util/zip/ZipEntry.java

Print this page
rev 51866 : 6194856: Zip Files lose ALL ownership and permissions of the files

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -23,19 +23,23 @@
  * questions.
  */
 
 package java.util.zip;
 
+import static java.util.zip.ZipConstants64.*;
 import static java.util.zip.ZipUtils.*;
+
 import java.nio.file.attribute.FileTime;
-import java.util.Objects;
-import java.util.concurrent.TimeUnit;
+import java.nio.file.attribute.PosixFilePermission;
+import java.nio.file.attribute.PosixFilePermissions;
 import java.time.LocalDateTime;
-import java.time.ZonedDateTime;
 import java.time.ZoneId;
-
-import static java.util.zip.ZipConstants64.*;
+import java.time.ZonedDateTime;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
 
 /**
  * This class is used to represent a ZIP file entry.
  *
  * @author      David Connelly

@@ -54,10 +58,11 @@
     long crc = -1;      // crc-32 of entry data
     long size = -1;     // uncompressed size of entry data
     long csize = -1;    // compressed size of entry data
     int method = -1;    // compression method
     int flag = 0;       // general purpose flag
+    int posixPerms = -1; // posix permissions
     byte[] extra;       // optional extra field data for entry
     String comment;     // optional comment string for entry
 
     /**
      * Compression method for uncompressed entries.

@@ -654,10 +659,47 @@
     public boolean isDirectory() {
         return name.endsWith("/");
     }
 
     /**
+     * Returns the set of Posix file permissions that are associated with
+     * this ZipEntry. This information might not be present all times,
+     * e.g. it is platform dependend. Also, when reading a zip file via
+     * {@link ZipInputStream}, the posix permissions will be null
+     * because data is stored in the CEN of the zip file which is not
+     * evaluated then. If you rely on Posix permissions, you need to access
+     * the zip file via {@link ZipFile}.
+     *
+     * @return The set of Posix File permissions, in case they are associated.
+     *
+     * @since 12
+     */
+    public Optional<Set<PosixFilePermission>> getPosixPermissions() {
+        if (posixPerms == -1) {
+            return Optional.empty();
+        }
+        return Optional.of(PosixFilePermissions.fromFlags(posixPerms));
+    }
+
+    /**
+     * Sets the set of Posix file permissions for this ZipEntry.
+     *
+     * @param permissions A set of PosixFilePermissions. If the value is null,
+     *                    no permission information will be stored in the zip
+     *                    file.
+     *
+     * @since 12
+     */
+    public void setPosixPermissions(Set<PosixFilePermission> permissions) {
+        if (permissions == null) {
+            posixPerms = -1;
+            return;
+        }
+        posixPerms = PosixFilePermissions.toFlags(permissions);
+    }
+
+    /**
      * Returns a string representation of the ZIP entry.
      */
     public String toString() {
         return getName();
     }
< prev index next >