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.io.Closeable;
29 import java.io.InputStream;
30 import java.io.IOException;
31 import java.io.EOFException;
32 import java.io.File;
33 import java.io.RandomAccessFile;
34 import java.io.UncheckedIOException;
35 import java.lang.ref.Cleaner.Cleanable;
36 import java.nio.charset.Charset;
37 import java.nio.charset.StandardCharsets;
38 import java.nio.file.attribute.BasicFileAttributes;
39 import java.nio.file.Files;
40
41 import java.util.ArrayDeque;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Collections;
45 import java.util.Deque;
46 import java.util.Enumeration;
47 import java.util.HashMap;
48 import java.util.Iterator;
49 import java.util.Objects;
50 import java.util.NoSuchElementException;
51 import java.util.Set;
52 import java.util.Spliterator;
53 import java.util.Spliterators;
54 import java.util.WeakHashMap;
55
56 import java.util.function.Consumer;
57 import java.util.function.Function;
58 import java.util.function.IntFunction;
59 import java.util.jar.JarEntry;
60 import java.util.stream.Stream;
61 import java.util.stream.StreamSupport;
62 import jdk.internal.misc.JavaUtilZipFileAccess;
63 import jdk.internal.misc.SharedSecrets;
64 import jdk.internal.misc.VM;
65 import jdk.internal.perf.PerfCounter;
66 import jdk.internal.ref.CleanerFactory;
67 import jdk.internal.vm.annotation.Stable;
68
69 import static java.util.zip.ZipConstants64.*;
70 import static java.util.zip.ZipUtils.*;
71
72 /**
73 * This class is used to read entries from a zip file.
74 *
75 * <p> Unless otherwise noted, passing a {@code null} argument to a constructor
76 * or method in this class will cause a {@link NullPointerException} to be
77 * thrown.
78 *
79 * @apiNote
80 * To release resources used by this {@code ZipFile}, the {@link #close()} method
81 * should be called explicitly or by try-with-resources. Subclasses are responsible
82 * for the cleanup of resources acquired by the subclass. Subclasses that override
83 * {@link #finalize()} in order to perform cleanup should be modified to use alternative
84 * cleanup mechanisms such as {@link java.lang.ref.Cleaner} and remove the overriding
85 * {@code finalize} method.
86 *
87 * @implSpec
88 * If this {@code ZipFile} has been subclassed and the {@code close} method has
156 * method is called with the {@code name} argument as its argument to
157 * ensure the read is allowed.
158 *
159 * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
160 * decode the entry names and comments
161 *
162 * @param file the ZIP file to be opened for reading
163 * @param mode the mode in which the file is to be opened
164 * @throws ZipException if a ZIP format error has occurred
165 * @throws IOException if an I/O error has occurred
166 * @throws SecurityException if a security manager exists and
167 * its {@code checkRead} method
168 * doesn't allow read access to the file,
169 * or its {@code checkDelete} method doesn't allow deleting
170 * the file when the {@code OPEN_DELETE} flag is set.
171 * @throws IllegalArgumentException if the {@code mode} argument is invalid
172 * @see SecurityManager#checkRead(java.lang.String)
173 * @since 1.3
174 */
175 public ZipFile(File file, int mode) throws IOException {
176 this(file, mode, StandardCharsets.UTF_8);
177 }
178
179 /**
180 * Opens a ZIP file for reading given the specified File object.
181 *
182 * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
183 * decode the entry names and comments.
184 *
185 * @param file the ZIP file to be opened for reading
186 * @throws ZipException if a ZIP format error has occurred
187 * @throws IOException if an I/O error has occurred
188 */
189 public ZipFile(File file) throws ZipException, IOException {
190 this(file, OPEN_READ);
191 }
192
193 /**
194 * Opens a new {@code ZipFile} to read from the specified
195 * {@code File} object in the specified mode. The mode argument
196 * must be either {@code OPEN_READ} or {@code OPEN_READ | OPEN_DELETE}.
1077 }
1078
1079 /**
1080 * Returns the names of all non-directory entries that begin with
1081 * "META-INF/" (case ignored). This method is used in JarFile, via
1082 * SharedSecrets, as an optimization when looking up manifest and
1083 * signature file entries. Returns null if no entries were found.
1084 */
1085 private String[] getMetaInfEntryNames() {
1086 synchronized (this) {
1087 ensureOpen();
1088 Source zsrc = res.zsrc;
1089 if (zsrc.metanames == null) {
1090 return null;
1091 }
1092 String[] names = new String[zsrc.metanames.length];
1093 byte[] cen = zsrc.cen;
1094 for (int i = 0; i < names.length; i++) {
1095 int pos = zsrc.metanames[i];
1096 names[i] = new String(cen, pos + CENHDR, CENNAM(cen, pos),
1097 StandardCharsets.UTF_8);
1098 }
1099 return names;
1100 }
1101 }
1102
1103 private static boolean isWindows;
1104 static {
1105 SharedSecrets.setJavaUtilZipFileAccess(
1106 new JavaUtilZipFileAccess() {
1107 @Override
1108 public boolean startsWithLocHeader(ZipFile zip) {
1109 return zip.res.zsrc.startsWithLoc;
1110 }
1111 @Override
1112 public String[] getMetaInfEntryNames(ZipFile zip) {
1113 return zip.getMetaInfEntryNames();
1114 }
1115 @Override
1116 public JarEntry getEntry(ZipFile zip, String name,
1117 Function<String, JarEntry> func) {
|
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.io.Closeable;
29 import java.io.InputStream;
30 import java.io.IOException;
31 import java.io.EOFException;
32 import java.io.File;
33 import java.io.RandomAccessFile;
34 import java.io.UncheckedIOException;
35 import java.lang.ref.Cleaner.Cleanable;
36 import java.nio.charset.Charset;
37 import java.nio.file.attribute.BasicFileAttributes;
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.Collections;
44 import java.util.Deque;
45 import java.util.Enumeration;
46 import java.util.HashMap;
47 import java.util.Iterator;
48 import java.util.Objects;
49 import java.util.NoSuchElementException;
50 import java.util.Set;
51 import java.util.Spliterator;
52 import java.util.Spliterators;
53 import java.util.WeakHashMap;
54
55 import java.util.function.Consumer;
56 import java.util.function.Function;
57 import java.util.function.IntFunction;
58 import java.util.jar.JarEntry;
59 import java.util.stream.Stream;
60 import java.util.stream.StreamSupport;
61 import jdk.internal.misc.JavaUtilZipFileAccess;
62 import jdk.internal.misc.SharedSecrets;
63 import jdk.internal.misc.VM;
64 import jdk.internal.perf.PerfCounter;
65 import jdk.internal.ref.CleanerFactory;
66 import jdk.internal.vm.annotation.Stable;
67
68 import sun.nio.cs.UTF_8;
69
70 import static java.util.zip.ZipConstants64.*;
71 import static java.util.zip.ZipUtils.*;
72
73 /**
74 * This class is used to read entries from a zip file.
75 *
76 * <p> Unless otherwise noted, passing a {@code null} argument to a constructor
77 * or method in this class will cause a {@link NullPointerException} to be
78 * thrown.
79 *
80 * @apiNote
81 * To release resources used by this {@code ZipFile}, the {@link #close()} method
82 * should be called explicitly or by try-with-resources. Subclasses are responsible
83 * for the cleanup of resources acquired by the subclass. Subclasses that override
84 * {@link #finalize()} in order to perform cleanup should be modified to use alternative
85 * cleanup mechanisms such as {@link java.lang.ref.Cleaner} and remove the overriding
86 * {@code finalize} method.
87 *
88 * @implSpec
89 * If this {@code ZipFile} has been subclassed and the {@code close} method has
157 * method is called with the {@code name} argument as its argument to
158 * ensure the read is allowed.
159 *
160 * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
161 * decode the entry names and comments
162 *
163 * @param file the ZIP file to be opened for reading
164 * @param mode the mode in which the file is to be opened
165 * @throws ZipException if a ZIP format error has occurred
166 * @throws IOException if an I/O error has occurred
167 * @throws SecurityException if a security manager exists and
168 * its {@code checkRead} method
169 * doesn't allow read access to the file,
170 * or its {@code checkDelete} method doesn't allow deleting
171 * the file when the {@code OPEN_DELETE} flag is set.
172 * @throws IllegalArgumentException if the {@code mode} argument is invalid
173 * @see SecurityManager#checkRead(java.lang.String)
174 * @since 1.3
175 */
176 public ZipFile(File file, int mode) throws IOException {
177 this(file, mode, UTF_8.INSTANCE);
178 }
179
180 /**
181 * Opens a ZIP file for reading given the specified File object.
182 *
183 * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
184 * decode the entry names and comments.
185 *
186 * @param file the ZIP file to be opened for reading
187 * @throws ZipException if a ZIP format error has occurred
188 * @throws IOException if an I/O error has occurred
189 */
190 public ZipFile(File file) throws ZipException, IOException {
191 this(file, OPEN_READ);
192 }
193
194 /**
195 * Opens a new {@code ZipFile} to read from the specified
196 * {@code File} object in the specified mode. The mode argument
197 * must be either {@code OPEN_READ} or {@code OPEN_READ | OPEN_DELETE}.
1078 }
1079
1080 /**
1081 * Returns the names of all non-directory entries that begin with
1082 * "META-INF/" (case ignored). This method is used in JarFile, via
1083 * SharedSecrets, as an optimization when looking up manifest and
1084 * signature file entries. Returns null if no entries were found.
1085 */
1086 private String[] getMetaInfEntryNames() {
1087 synchronized (this) {
1088 ensureOpen();
1089 Source zsrc = res.zsrc;
1090 if (zsrc.metanames == null) {
1091 return null;
1092 }
1093 String[] names = new String[zsrc.metanames.length];
1094 byte[] cen = zsrc.cen;
1095 for (int i = 0; i < names.length; i++) {
1096 int pos = zsrc.metanames[i];
1097 names[i] = new String(cen, pos + CENHDR, CENNAM(cen, pos),
1098 UTF_8.INSTANCE);
1099 }
1100 return names;
1101 }
1102 }
1103
1104 private static boolean isWindows;
1105 static {
1106 SharedSecrets.setJavaUtilZipFileAccess(
1107 new JavaUtilZipFileAccess() {
1108 @Override
1109 public boolean startsWithLocHeader(ZipFile zip) {
1110 return zip.res.zsrc.startsWithLoc;
1111 }
1112 @Override
1113 public String[] getMetaInfEntryNames(ZipFile zip) {
1114 return zip.getMetaInfEntryNames();
1115 }
1116 @Override
1117 public JarEntry getEntry(ZipFile zip, String name,
1118 Function<String, JarEntry> func) {
|