< prev index next >

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

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


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.zip;
  27 
  28 import java.nio.Buffer;

  29 import java.nio.ByteBuffer;
  30 import java.nio.file.attribute.FileTime;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import java.time.DateTimeException;
  34 import java.time.Instant;
  35 import java.time.LocalDateTime;
  36 import java.time.ZoneId;
  37 import java.util.Date;
  38 import java.util.concurrent.TimeUnit;
  39 
  40 import static java.util.zip.ZipConstants.ENDHDR;
  41 
  42 import jdk.internal.misc.Unsafe;
  43 import sun.nio.ch.DirectBuffer;
  44 
  45 class ZipUtils {
  46 
  47     // used to adjust values between Windows and java epoch
  48     private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
  49 
  50     // used to indicate the corresponding windows time is not available
  51     public static final long WINDOWS_TIME_NOT_AVAILABLE = Long.MIN_VALUE;
  52 
  53     // static final ByteBuffer defaultBuf = ByteBuffer.allocateDirect(0);
  54     static final ByteBuffer defaultBuf = ByteBuffer.allocate(0);
  55 
  56     /**
  57      * Converts Windows time (in microseconds, UTC/GMT) time to FileTime.
  58      */
  59     public static final FileTime winTimeToFileTime(long wtime) {
  60         return FileTime.from(wtime / 10 + WINDOWS_EPOCH_IN_MICROSECONDS,
  61                              TimeUnit.MICROSECONDS);
  62     }
  63 


 240     static final long EXTLEN(byte[] b) { return LG(b, 12);} // uncompressed size
 241 
 242     // end of central directory header (END) fields
 243     static final int  ENDSUB(byte[] b) { return SH(b, 8); }  // number of entries on this disk
 244     static final int  ENDTOT(byte[] b) { return SH(b, 10);}  // total number of entries
 245     static final long ENDSIZ(byte[] b) { return LG(b, 12);}  // central directory size
 246     static final long ENDOFF(byte[] b) { return LG(b, 16);}  // central directory offset
 247     static final int  ENDCOM(byte[] b) { return SH(b, 20);}  // size of zip file comment
 248     static final int  ENDCOM(byte[] b, int off) { return SH(b, off + 20);}
 249 
 250     // zip64 end of central directory recoder fields
 251     static final long ZIP64_ENDTOD(byte[] b) { return LL(b, 24);}  // total number of entries on disk
 252     static final long ZIP64_ENDTOT(byte[] b) { return LL(b, 32);}  // total number of entries
 253     static final long ZIP64_ENDSIZ(byte[] b) { return LL(b, 40);}  // central directory size
 254     static final long ZIP64_ENDOFF(byte[] b) { return LL(b, 48);}  // central directory offset
 255     static final long ZIP64_LOCOFF(byte[] b) { return LL(b, 8);}   // zip64 end offset
 256 
 257     // central directory header (CEN) fields
 258     static final long CENSIG(byte[] b, int pos) { return LG(b, pos + 0); }
 259     static final int  CENVEM(byte[] b, int pos) { return SH(b, pos + 4); }

 260     static final int  CENVER(byte[] b, int pos) { return SH(b, pos + 6); }
 261     static final int  CENFLG(byte[] b, int pos) { return SH(b, pos + 8); }
 262     static final int  CENHOW(byte[] b, int pos) { return SH(b, pos + 10);}
 263     static final long CENTIM(byte[] b, int pos) { return LG(b, pos + 12);}
 264     static final long CENCRC(byte[] b, int pos) { return LG(b, pos + 16);}
 265     static final long CENSIZ(byte[] b, int pos) { return LG(b, pos + 20);}
 266     static final long CENLEN(byte[] b, int pos) { return LG(b, pos + 24);}
 267     static final int  CENNAM(byte[] b, int pos) { return SH(b, pos + 28);}
 268     static final int  CENEXT(byte[] b, int pos) { return SH(b, pos + 30);}
 269     static final int  CENCOM(byte[] b, int pos) { return SH(b, pos + 32);}
 270     static final int  CENDSK(byte[] b, int pos) { return SH(b, pos + 34);}
 271     static final int  CENATT(byte[] b, int pos) { return SH(b, pos + 36);}
 272     static final long CENATX(byte[] b, int pos) { return LG(b, pos + 38);}

 273     static final long CENOFF(byte[] b, int pos) { return LG(b, pos + 42);}
 274 
 275     // The END header is followed by a variable length comment of size < 64k.
 276     static final long END_MAXLEN = 0xFFFF + ENDHDR;
 277     static final int READBLOCKSZ = 128;
 278 
 279     /**
 280      * Loads zip native library, if not already laoded
 281      */
 282     static void loadLibrary() {
 283         SecurityManager sm = System.getSecurityManager();
 284         if (sm == null) {
 285             System.loadLibrary("zip");
 286         } else {
 287             PrivilegedAction<Void> pa = () -> { System.loadLibrary("zip"); return null; };
 288             AccessController.doPrivileged(pa);
 289         }
 290     }
 291 
 292     private static final Unsafe unsafe = Unsafe.getUnsafe();


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.zip;
  27 
  28 import static java.util.zip.ZipConstants.ENDHDR;
  29 
  30 import java.nio.ByteBuffer;
  31 import java.nio.file.attribute.FileTime;
  32 import java.security.AccessController;
  33 import java.security.PrivilegedAction;
  34 import java.time.DateTimeException;
  35 import java.time.Instant;
  36 import java.time.LocalDateTime;
  37 import java.time.ZoneId;
  38 import java.util.Date;
  39 import java.util.concurrent.TimeUnit;
  40 


  41 import jdk.internal.misc.Unsafe;

  42 
  43 class ZipUtils {
  44 
  45     // used to adjust values between Windows and java epoch
  46     private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
  47 
  48     // used to indicate the corresponding windows time is not available
  49     public static final long WINDOWS_TIME_NOT_AVAILABLE = Long.MIN_VALUE;
  50 
  51     // static final ByteBuffer defaultBuf = ByteBuffer.allocateDirect(0);
  52     static final ByteBuffer defaultBuf = ByteBuffer.allocate(0);
  53 
  54     /**
  55      * Converts Windows time (in microseconds, UTC/GMT) time to FileTime.
  56      */
  57     public static final FileTime winTimeToFileTime(long wtime) {
  58         return FileTime.from(wtime / 10 + WINDOWS_EPOCH_IN_MICROSECONDS,
  59                              TimeUnit.MICROSECONDS);
  60     }
  61 


 238     static final long EXTLEN(byte[] b) { return LG(b, 12);} // uncompressed size
 239 
 240     // end of central directory header (END) fields
 241     static final int  ENDSUB(byte[] b) { return SH(b, 8); }  // number of entries on this disk
 242     static final int  ENDTOT(byte[] b) { return SH(b, 10);}  // total number of entries
 243     static final long ENDSIZ(byte[] b) { return LG(b, 12);}  // central directory size
 244     static final long ENDOFF(byte[] b) { return LG(b, 16);}  // central directory offset
 245     static final int  ENDCOM(byte[] b) { return SH(b, 20);}  // size of zip file comment
 246     static final int  ENDCOM(byte[] b, int off) { return SH(b, off + 20);}
 247 
 248     // zip64 end of central directory recoder fields
 249     static final long ZIP64_ENDTOD(byte[] b) { return LL(b, 24);}  // total number of entries on disk
 250     static final long ZIP64_ENDTOT(byte[] b) { return LL(b, 32);}  // total number of entries
 251     static final long ZIP64_ENDSIZ(byte[] b) { return LL(b, 40);}  // central directory size
 252     static final long ZIP64_ENDOFF(byte[] b) { return LL(b, 48);}  // central directory offset
 253     static final long ZIP64_LOCOFF(byte[] b) { return LL(b, 8);}   // zip64 end offset
 254 
 255     // central directory header (CEN) fields
 256     static final long CENSIG(byte[] b, int pos) { return LG(b, pos + 0); }
 257     static final int  CENVEM(byte[] b, int pos) { return SH(b, pos + 4); }
 258     static final int  CENVEM_FA(byte[] b, int pos) { return CH(b, pos + 5); }
 259     static final int  CENVER(byte[] b, int pos) { return SH(b, pos + 6); }
 260     static final int  CENFLG(byte[] b, int pos) { return SH(b, pos + 8); }
 261     static final int  CENHOW(byte[] b, int pos) { return SH(b, pos + 10);}
 262     static final long CENTIM(byte[] b, int pos) { return LG(b, pos + 12);}
 263     static final long CENCRC(byte[] b, int pos) { return LG(b, pos + 16);}
 264     static final long CENSIZ(byte[] b, int pos) { return LG(b, pos + 20);}
 265     static final long CENLEN(byte[] b, int pos) { return LG(b, pos + 24);}
 266     static final int  CENNAM(byte[] b, int pos) { return SH(b, pos + 28);}
 267     static final int  CENEXT(byte[] b, int pos) { return SH(b, pos + 30);}
 268     static final int  CENCOM(byte[] b, int pos) { return SH(b, pos + 32);}
 269     static final int  CENDSK(byte[] b, int pos) { return SH(b, pos + 34);}
 270     static final int  CENATT(byte[] b, int pos) { return SH(b, pos + 36);}
 271     static final long CENATX(byte[] b, int pos) { return LG(b, pos + 38);}
 272     static final int  CENATX_PERMS(byte[] b, int pos) { return SH(b, pos + 40);}
 273     static final long CENOFF(byte[] b, int pos) { return LG(b, pos + 42);}
 274 
 275     // The END header is followed by a variable length comment of size < 64k.
 276     static final long END_MAXLEN = 0xFFFF + ENDHDR;
 277     static final int READBLOCKSZ = 128;
 278 
 279     /**
 280      * Loads zip native library, if not already laoded
 281      */
 282     static void loadLibrary() {
 283         SecurityManager sm = System.getSecurityManager();
 284         if (sm == null) {
 285             System.loadLibrary("zip");
 286         } else {
 287             PrivilegedAction<Void> pa = () -> { System.loadLibrary("zip"); return null; };
 288             AccessController.doPrivileged(pa);
 289         }
 290     }
 291 
 292     private static final Unsafe unsafe = Unsafe.getUnsafe();
< prev index next >