1 /*
2 * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
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.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 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 import java.util.function.Consumer;
55 import java.util.function.Function;
56 import java.util.function.IntFunction;
57 import java.util.jar.JarEntry;
58 import java.util.jar.JarFile;
59 import java.util.stream.Stream;
60 import java.util.stream.StreamSupport;
61 import jdk.internal.misc.JavaLangAccess;
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
89 * been overridden, the {@code close} method will be called by the finalization
90 * when {@code ZipFile} is unreachable. But the subclasses should not depend on
91 * this specific implementation; the finalization is not reliable and the
94 * @author David Connelly
95 * @since 1.1
96 */
97 public
98 class ZipFile implements ZipConstants, Closeable {
99
100 private final String name; // zip file name
101 private volatile boolean closeRequested;
102 private final @Stable ZipCoder zc;
103
104 // The "resource" used by this zip file that needs to be
105 // cleaned after use.
106 // a) the input streams that need to be closed
107 // b) the list of cached Inflater objects
108 // c) the "native" source of this zip file.
109 private final @Stable CleanableResource res;
110
111 private static final int STORED = ZipEntry.STORED;
112 private static final int DEFLATED = ZipEntry.DEFLATED;
113
114 /**
115 * Mode flag to open a zip file for reading.
116 */
117 public static final int OPEN_READ = 0x1;
118
119 /**
120 * Mode flag to open a zip file and mark it for deletion. The file will be
121 * deleted some time between the moment that it is opened and the moment
122 * that it is closed, but its contents will remain accessible via the
123 * {@code ZipFile} object until either the close method is invoked or the
124 * virtual machine exits.
125 */
126 public static final int OPEN_DELETE = 0x4;
127
128 /**
129 * Opens a zip file for reading.
130 *
131 * <p>First, if there is a security manager, its {@code checkRead}
132 * method is called with the {@code name} argument as its argument
133 * to ensure the read is allowed.
661 int clen = CENCOM(cen, pos);
662 int flag = CENFLG(cen, pos);
663 if (name == null || bname.length != nlen) {
664 // to use the entry name stored in cen, if the passed in name is
665 // (1) null, invoked from iterator, or
666 // (2) not equal to the name stored, a slash is appended during
667 // getEntryPos() search.
668 if (!zc.isUTF8() && (flag & USE_UTF8) != 0) {
669 name = zc.toStringUTF8(cen, pos + CENHDR, nlen);
670 } else {
671 name = zc.toString(cen, pos + CENHDR, nlen);
672 }
673 }
674 ZipEntry e = func.apply(name); //ZipEntry e = new ZipEntry(name);
675 e.flag = flag;
676 e.xdostime = CENTIM(cen, pos);
677 e.crc = CENCRC(cen, pos);
678 e.size = CENLEN(cen, pos);
679 e.csize = CENSIZ(cen, pos);
680 e.method = CENHOW(cen, pos);
681 if (elen != 0) {
682 int start = pos + CENHDR + nlen;
683 e.setExtra0(Arrays.copyOfRange(cen, start, start + elen), true);
684 }
685 if (clen != 0) {
686 int start = pos + CENHDR + nlen + elen;
687 if (!zc.isUTF8() && (flag & USE_UTF8) != 0) {
688 e.comment = zc.toStringUTF8(cen, start, clen);
689 } else {
690 e.comment = zc.toString(cen, start, clen);
691 }
692 }
693 lastEntryName = e.name;
694 lastEntryPos = pos;
695 return e;
696 }
697
698 /**
699 * Returns the number of entries in the ZIP file.
700 *
|
1 /*
2 * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
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.ZipConstants64.*;
29 import static java.util.zip.ZipUtils.*;
30
31 import java.io.Closeable;
32 import java.io.EOFException;
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.RandomAccessFile;
37 import java.io.UncheckedIOException;
38 import java.lang.ref.Cleaner.Cleanable;
39 import java.nio.charset.Charset;
40 import java.nio.charset.StandardCharsets;
41 import java.nio.file.Files;
42 import java.nio.file.attribute.BasicFileAttributes;
43 import java.util.ArrayDeque;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Collections;
47 import java.util.Deque;
48 import java.util.Enumeration;
49 import java.util.HashMap;
50 import java.util.Iterator;
51 import java.util.NoSuchElementException;
52 import java.util.Objects;
53 import java.util.Set;
54 import java.util.Spliterator;
55 import java.util.Spliterators;
56 import java.util.WeakHashMap;
57 import java.util.function.Consumer;
58 import java.util.function.Function;
59 import java.util.function.IntFunction;
60 import java.util.jar.JarEntry;
61 import java.util.jar.JarFile;
62 import java.util.stream.Stream;
63 import java.util.stream.StreamSupport;
64
65 import jdk.internal.misc.JavaLangAccess;
66 import jdk.internal.misc.JavaUtilZipFileAccess;
67 import jdk.internal.misc.SharedSecrets;
68 import jdk.internal.misc.VM;
69 import jdk.internal.perf.PerfCounter;
70 import jdk.internal.ref.CleanerFactory;
71 import jdk.internal.vm.annotation.Stable;
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
90 * been overridden, the {@code close} method will be called by the finalization
91 * when {@code ZipFile} is unreachable. But the subclasses should not depend on
92 * this specific implementation; the finalization is not reliable and the
95 * @author David Connelly
96 * @since 1.1
97 */
98 public
99 class ZipFile implements ZipConstants, Closeable {
100
101 private final String name; // zip file name
102 private volatile boolean closeRequested;
103 private final @Stable ZipCoder zc;
104
105 // The "resource" used by this zip file that needs to be
106 // cleaned after use.
107 // a) the input streams that need to be closed
108 // b) the list of cached Inflater objects
109 // c) the "native" source of this zip file.
110 private final @Stable CleanableResource res;
111
112 private static final int STORED = ZipEntry.STORED;
113 private static final int DEFLATED = ZipEntry.DEFLATED;
114
115 static final int FILE_ATTRIBUTES_UNIX = 3;
116
117 /**
118 * Mode flag to open a zip file for reading.
119 */
120 public static final int OPEN_READ = 0x1;
121
122 /**
123 * Mode flag to open a zip file and mark it for deletion. The file will be
124 * deleted some time between the moment that it is opened and the moment
125 * that it is closed, but its contents will remain accessible via the
126 * {@code ZipFile} object until either the close method is invoked or the
127 * virtual machine exits.
128 */
129 public static final int OPEN_DELETE = 0x4;
130
131 /**
132 * Opens a zip file for reading.
133 *
134 * <p>First, if there is a security manager, its {@code checkRead}
135 * method is called with the {@code name} argument as its argument
136 * to ensure the read is allowed.
664 int clen = CENCOM(cen, pos);
665 int flag = CENFLG(cen, pos);
666 if (name == null || bname.length != nlen) {
667 // to use the entry name stored in cen, if the passed in name is
668 // (1) null, invoked from iterator, or
669 // (2) not equal to the name stored, a slash is appended during
670 // getEntryPos() search.
671 if (!zc.isUTF8() && (flag & USE_UTF8) != 0) {
672 name = zc.toStringUTF8(cen, pos + CENHDR, nlen);
673 } else {
674 name = zc.toString(cen, pos + CENHDR, nlen);
675 }
676 }
677 ZipEntry e = func.apply(name); //ZipEntry e = new ZipEntry(name);
678 e.flag = flag;
679 e.xdostime = CENTIM(cen, pos);
680 e.crc = CENCRC(cen, pos);
681 e.size = CENLEN(cen, pos);
682 e.csize = CENSIZ(cen, pos);
683 e.method = CENHOW(cen, pos);
684 if (CENVEM_FA(cen, pos) == FILE_ATTRIBUTES_UNIX) {
685 e.posixPerms = CENATX_PERMS(cen, pos) & 0xFFF; // 12 bits for setuid, setgid, sticky + perms
686 }
687 if (elen != 0) {
688 int start = pos + CENHDR + nlen;
689 e.setExtra0(Arrays.copyOfRange(cen, start, start + elen), true);
690 }
691 if (clen != 0) {
692 int start = pos + CENHDR + nlen + elen;
693 if (!zc.isUTF8() && (flag & USE_UTF8) != 0) {
694 e.comment = zc.toStringUTF8(cen, start, clen);
695 } else {
696 e.comment = zc.toString(cen, start, clen);
697 }
698 }
699 lastEntryName = e.name;
700 lastEntryPos = pos;
701 return e;
702 }
703
704 /**
705 * Returns the number of entries in the ZIP file.
706 *
|