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

Print this page




  37 import java.nio.file.Path;
  38 import java.nio.file.Files;
  39 
  40 import java.util.ArrayDeque;
  41 import java.util.ArrayList;
  42 import java.util.Arrays;
  43 import java.util.Deque;
  44 import java.util.Enumeration;
  45 import java.util.HashMap;
  46 import java.util.Iterator;
  47 import java.util.Map;
  48 import java.util.Objects;
  49 import java.util.NoSuchElementException;
  50 import java.util.Spliterator;
  51 import java.util.Spliterators;
  52 import java.util.WeakHashMap;
  53 import java.util.stream.Stream;
  54 import java.util.stream.StreamSupport;
  55 import jdk.internal.misc.JavaUtilZipFileAccess;
  56 import jdk.internal.misc.SharedSecrets;

  57 
  58 import static java.util.zip.ZipConstants.*;
  59 import static java.util.zip.ZipConstants64.*;
  60 import static java.util.zip.ZipUtils.*;
  61 
  62 /**
  63  * This class is used to read entries from a zip file.
  64  *
  65  * <p> Unless otherwise noted, passing a {@code null} argument to a constructor
  66  * or method in this class will cause a {@link NullPointerException} to be
  67  * thrown.
  68  *
  69  * @author      David Connelly
  70  */
  71 public
  72 class ZipFile implements ZipConstants, Closeable {
  73 
  74     private final String name;     // zip file name
  75     private volatile boolean closeRequested;
  76     private Source zsrc;


 193     public ZipFile(File file, int mode, Charset charset) throws IOException
 194     {
 195         if (((mode & OPEN_READ) == 0) ||
 196             ((mode & ~(OPEN_READ | OPEN_DELETE)) != 0)) {
 197             throw new IllegalArgumentException("Illegal mode: 0x"+
 198                                                Integer.toHexString(mode));
 199         }
 200         String name = file.getPath();
 201         SecurityManager sm = System.getSecurityManager();
 202         if (sm != null) {
 203             sm.checkRead(name);
 204             if ((mode & OPEN_DELETE) != 0) {
 205                 sm.checkDelete(name);
 206             }
 207         }
 208         Objects.requireNonNull(charset, "charset");
 209         this.zc = ZipCoder.get(charset);
 210         this.name = name;
 211         long t0 = System.nanoTime();
 212         this.zsrc = Source.get(file, (mode & OPEN_DELETE) != 0);
 213         sun.misc.PerfCounter.getZipFileOpenTime().addElapsedTimeFrom(t0);
 214         sun.misc.PerfCounter.getZipFileCount().increment();
 215     }
 216 
 217     /**
 218      * Opens a zip file for reading.
 219      *
 220      * <p>First, if there is a security manager, its {@code checkRead}
 221      * method is called with the {@code name} argument as its argument
 222      * to ensure the read is allowed.
 223      *
 224      * @param name the name of the zip file
 225      * @param charset
 226      *        the {@linkplain java.nio.charset.Charset charset} to
 227      *        be used to decode the ZIP entry name and comment that are not
 228      *        encoded by using UTF-8 encoding (indicated by entry's general
 229      *        purpose flag).
 230      *
 231      * @throws ZipException if a ZIP format error has occurred
 232      * @throws IOException if an I/O error has occurred
 233      * @throws SecurityException
 234      *         if a security manager exists and its {@code checkRead}




  37 import java.nio.file.Path;
  38 import java.nio.file.Files;
  39 
  40 import java.util.ArrayDeque;
  41 import java.util.ArrayList;
  42 import java.util.Arrays;
  43 import java.util.Deque;
  44 import java.util.Enumeration;
  45 import java.util.HashMap;
  46 import java.util.Iterator;
  47 import java.util.Map;
  48 import java.util.Objects;
  49 import java.util.NoSuchElementException;
  50 import java.util.Spliterator;
  51 import java.util.Spliterators;
  52 import java.util.WeakHashMap;
  53 import java.util.stream.Stream;
  54 import java.util.stream.StreamSupport;
  55 import jdk.internal.misc.JavaUtilZipFileAccess;
  56 import jdk.internal.misc.SharedSecrets;
  57 import jdk.internal.perf.PerfCounter;
  58 
  59 import static java.util.zip.ZipConstants.*;
  60 import static java.util.zip.ZipConstants64.*;
  61 import static java.util.zip.ZipUtils.*;
  62 
  63 /**
  64  * This class is used to read entries from a zip file.
  65  *
  66  * <p> Unless otherwise noted, passing a {@code null} argument to a constructor
  67  * or method in this class will cause a {@link NullPointerException} to be
  68  * thrown.
  69  *
  70  * @author      David Connelly
  71  */
  72 public
  73 class ZipFile implements ZipConstants, Closeable {
  74 
  75     private final String name;     // zip file name
  76     private volatile boolean closeRequested;
  77     private Source zsrc;


 194     public ZipFile(File file, int mode, Charset charset) throws IOException
 195     {
 196         if (((mode & OPEN_READ) == 0) ||
 197             ((mode & ~(OPEN_READ | OPEN_DELETE)) != 0)) {
 198             throw new IllegalArgumentException("Illegal mode: 0x"+
 199                                                Integer.toHexString(mode));
 200         }
 201         String name = file.getPath();
 202         SecurityManager sm = System.getSecurityManager();
 203         if (sm != null) {
 204             sm.checkRead(name);
 205             if ((mode & OPEN_DELETE) != 0) {
 206                 sm.checkDelete(name);
 207             }
 208         }
 209         Objects.requireNonNull(charset, "charset");
 210         this.zc = ZipCoder.get(charset);
 211         this.name = name;
 212         long t0 = System.nanoTime();
 213         this.zsrc = Source.get(file, (mode & OPEN_DELETE) != 0);
 214         PerfCounter.getZipFileOpenTime().addElapsedTimeFrom(t0);
 215         PerfCounter.getZipFileCount().increment();
 216     }
 217 
 218     /**
 219      * Opens a zip file for reading.
 220      *
 221      * <p>First, if there is a security manager, its {@code checkRead}
 222      * method is called with the {@code name} argument as its argument
 223      * to ensure the read is allowed.
 224      *
 225      * @param name the name of the zip file
 226      * @param charset
 227      *        the {@linkplain java.nio.charset.Charset charset} to
 228      *        be used to decode the ZIP entry name and comment that are not
 229      *        encoded by using UTF-8 encoding (indicated by entry's general
 230      *        purpose flag).
 231      *
 232      * @throws ZipException if a ZIP format error has occurred
 233      * @throws IOException if an I/O error has occurred
 234      * @throws SecurityException
 235      *         if a security manager exists and its {@code checkRead}