ZipFile.java revision d95a908045486d862dfe0be056dd4ecfb3bd7693
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.  Oracle designates this
9 * particular file as subject to the "Classpath" exception as provided
10 * by Oracle in the LICENSE file that accompanied this code.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23 * or visit www.oracle.com if you need additional information or have any
24 * questions.
25 */
26
27package java.util.zip;
28
29import java.io.Closeable;
30import java.io.InputStream;
31import java.io.IOException;
32import java.io.EOFException;
33import java.io.File;
34import java.io.FileNotFoundException;
35import java.nio.charset.Charset;
36import java.nio.charset.StandardCharsets;
37import java.util.ArrayDeque;
38import java.util.Deque;
39import java.util.Enumeration;
40import java.util.HashMap;
41import java.util.HashSet;
42import java.util.Map;
43import java.util.NoSuchElementException;
44import java.util.Set;
45import java.util.WeakHashMap;
46import java.security.AccessController;
47
48import dalvik.system.CloseGuard;
49import sun.security.action.GetPropertyAction;
50
51import static java.util.zip.ZipConstants64.*;
52
53/**
54 * This class is used to read entries from a zip file.
55 *
56 * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
57 * or method in this class will cause a {@link NullPointerException} to be
58 * thrown.
59 *
60 * @author      David Connelly
61 */
62public
63class ZipFile implements ZipConstants, Closeable {
64    private long jzfile;           // address of jzfile data
65    private final String name;     // zip file name
66    private final int total;       // total number of entries
67    private final boolean locsig;  // if zip file starts with LOCSIG (usually true)
68    private volatile boolean closeRequested = false;
69
70    private final CloseGuard guard = CloseGuard.get();
71
72    // Android changed, needed for alternative OPEN_DELETE implementation
73    // that doesn't use unlink before closing the file.
74    private final File fileToRemoveOnClose;
75
76    private static final int STORED = ZipEntry.STORED;
77    private static final int DEFLATED = ZipEntry.DEFLATED;
78
79    /**
80     * Mode flag to open a zip file for reading.
81     */
82    public static final int OPEN_READ = 0x1;
83
84    /**
85     * Mode flag to open a zip file and mark it for deletion.  The file will be
86     * deleted some time between the moment that it is opened and the moment
87     * that it is closed, but its contents will remain accessible via the
88     * <tt>ZipFile</tt> object until either the close method is invoked or the
89     * virtual machine exits.
90     */
91    public static final int OPEN_DELETE = 0x4;
92
93    private static final boolean usemmap;
94
95    static {
96        // Android-changed: always use mmap.
97        usemmap = true;
98    }
99
100    /**
101     * Opens a zip file for reading.
102     *
103     * <p>First, if there is a security manager, its <code>checkRead</code>
104     * method is called with the <code>name</code> argument as its argument
105     * to ensure the read is allowed.
106     *
107     * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
108     * decode the entry names and comments.
109     *
110     * @param name the name of the zip file
111     * @throws ZipException if a ZIP format error has occurred
112     * @throws IOException if an I/O error has occurred
113     * @throws SecurityException if a security manager exists and its
114     *         <code>checkRead</code> method doesn't allow read access to the file.
115     *
116     * @see SecurityManager#checkRead(java.lang.String)
117     */
118    public ZipFile(String name) throws IOException {
119        this(new File(name), OPEN_READ);
120    }
121
122    /**
123     * Opens a new <code>ZipFile</code> to read from the specified
124     * <code>File</code> object in the specified mode.  The mode argument
125     * must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>.
126     *
127     * <p>First, if there is a security manager, its <code>checkRead</code>
128     * method is called with the <code>name</code> argument as its argument to
129     * ensure the read is allowed.
130     *
131     * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
132     * decode the entry names and comments
133     *
134     * @param file the ZIP file to be opened for reading
135     * @param mode the mode in which the file is to be opened
136     * @throws ZipException if a ZIP format error has occurred
137     * @throws IOException if an I/O error has occurred
138     * @throws SecurityException if a security manager exists and
139     *         its <code>checkRead</code> method
140     *         doesn't allow read access to the file,
141     *         or its <code>checkDelete</code> method doesn't allow deleting
142     *         the file when the <tt>OPEN_DELETE</tt> flag is set.
143     * @throws IllegalArgumentException if the <tt>mode</tt> argument is invalid
144     * @see SecurityManager#checkRead(java.lang.String)
145     * @since 1.3
146     */
147    public ZipFile(File file, int mode) throws IOException {
148        this(file, mode, StandardCharsets.UTF_8);
149    }
150
151    /**
152     * Opens a ZIP file for reading given the specified File object.
153     *
154     * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
155     * decode the entry names and comments.
156     *
157     * @param file the ZIP file to be opened for reading
158     * @throws ZipException if a ZIP format error has occurred
159     * @throws IOException if an I/O error has occurred
160     */
161    public ZipFile(File file) throws ZipException, IOException {
162        this(file, OPEN_READ);
163    }
164
165    private ZipCoder zc;
166
167    /**
168     * Opens a new <code>ZipFile</code> to read from the specified
169     * <code>File</code> object in the specified mode.  The mode argument
170     * must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>.
171     *
172     * <p>First, if there is a security manager, its <code>checkRead</code>
173     * method is called with the <code>name</code> argument as its argument to
174     * ensure the read is allowed.
175     *
176     * @param file the ZIP file to be opened for reading
177     * @param mode the mode in which the file is to be opened
178     * @param charset
179     *        the {@linkplain java.nio.charset.Charset charset} to
180     *        be used to decode the ZIP entry name and comment that are not
181     *        encoded by using UTF-8 encoding (indicated by entry's general
182     *        purpose flag).
183     *
184     * @throws ZipException if a ZIP format error has occurred
185     * @throws IOException if an I/O error has occurred
186     *
187     * @throws SecurityException
188     *         if a security manager exists and its <code>checkRead</code>
189     *         method doesn't allow read access to the file,or its
190     *         <code>checkDelete</code> method doesn't allow deleting the
191     *         file when the <tt>OPEN_DELETE</tt> flag is set
192     *
193     * @throws IllegalArgumentException if the <tt>mode</tt> argument is invalid
194     *
195     * @see SecurityManager#checkRead(java.lang.String)
196     *
197     * @since 1.7
198     */
199    public ZipFile(File file, int mode, Charset charset) throws IOException
200    {
201        if (((mode & OPEN_READ) == 0) ||
202            ((mode & ~(OPEN_READ | OPEN_DELETE)) != 0)) {
203            throw new IllegalArgumentException("Illegal mode: 0x"+
204                                               Integer.toHexString(mode));
205        }
206
207
208        // Android-changed: Error out early if the file is too short or non-existent.
209        long length = file.length();
210        if (length < ZipConstants.ENDHDR) {
211            if (length == 0 && !file.exists()) {
212                throw new FileNotFoundException("File doesn't exist: " + file);
213            } else {
214                throw new ZipException("File too short to be a zip file: " + file.length());
215            }
216        }
217        String name = file.getPath();
218
219        // Android changed, handle OPEN_DELETE case in #close().
220        fileToRemoveOnClose = ((mode & OPEN_DELETE) != 0) ? file : null;
221
222        if (charset == null)
223            throw new NullPointerException("charset is null");
224        this.zc = ZipCoder.get(charset);
225        jzfile = open(name, mode, file.lastModified(), usemmap);
226        this.name = name;
227        this.total = getTotal(jzfile);
228        this.locsig = startsWithLOC(jzfile);
229        Enumeration<? extends ZipEntry> entries = entries();
230
231        // Android-changed: Error out early if the zipfile has no entries.
232        if (size() == 0 || !entries.hasMoreElements()) {
233            close();
234            throw new ZipException("No entries");
235        }
236
237        guard.open("close");
238    }
239
240    /**
241     * Opens a zip file for reading.
242     *
243     * <p>First, if there is a security manager, its <code>checkRead</code>
244     * method is called with the <code>name</code> argument as its argument
245     * to ensure the read is allowed.
246     *
247     * @param name the name of the zip file
248     * @param charset
249     *        the {@linkplain java.nio.charset.Charset charset} to
250     *        be used to decode the ZIP entry name and comment that are not
251     *        encoded by using UTF-8 encoding (indicated by entry's general
252     *        purpose flag).
253     *
254     * @throws ZipException if a ZIP format error has occurred
255     * @throws IOException if an I/O error has occurred
256     * @throws SecurityException
257     *         if a security manager exists and its <code>checkRead</code>
258     *         method doesn't allow read access to the file
259     *
260     * @see SecurityManager#checkRead(java.lang.String)
261     *
262     * @since 1.7
263     */
264    public ZipFile(String name, Charset charset) throws IOException
265    {
266        this(new File(name), OPEN_READ, charset);
267    }
268
269    /**
270     * Opens a ZIP file for reading given the specified File object.
271     * @param file the ZIP file to be opened for reading
272     * @param charset
273     *        The {@linkplain java.nio.charset.Charset charset} to be
274     *        used to decode the ZIP entry name and comment (ignored if
275     *        the <a href="package-summary.html#lang_encoding"> language
276     *        encoding bit</a> of the ZIP entry's general purpose bit
277     *        flag is set).
278     *
279     * @throws ZipException if a ZIP format error has occurred
280     * @throws IOException if an I/O error has occurred
281     *
282     * @since 1.7
283     */
284    public ZipFile(File file, Charset charset) throws IOException
285    {
286        this(file, OPEN_READ, charset);
287    }
288
289    /**
290     * Returns the zip file comment, or null if none.
291     *
292     * @return the comment string for the zip file, or null if none
293     *
294     * @throws IllegalStateException if the zip file has been closed
295     *
296     * Since 1.7
297     */
298    public String getComment() {
299        synchronized (this) {
300            ensureOpen();
301            byte[] bcomm = getCommentBytes(jzfile);
302            if (bcomm == null)
303                return null;
304            return zc.toString(bcomm, bcomm.length);
305        }
306    }
307
308    /**
309     * Returns the zip file entry for the specified name, or null
310     * if not found.
311     *
312     * @param name the name of the entry
313     * @return the zip file entry, or null if not found
314     * @throws IllegalStateException if the zip file has been closed
315     */
316    public ZipEntry getEntry(String name) {
317        if (name == null) {
318            throw new NullPointerException("name");
319        }
320        long jzentry = 0;
321        synchronized (this) {
322            ensureOpen();
323            jzentry = getEntry(jzfile, zc.getBytes(name), true);
324            if (jzentry != 0) {
325                ZipEntry ze = getZipEntry(name, jzentry);
326                freeEntry(jzfile, jzentry);
327                return ze;
328            }
329        }
330        return null;
331    }
332
333    private static native long getEntry(long jzfile, byte[] name,
334                                        boolean addSlash);
335
336    // freeEntry releases the C jzentry struct.
337    private static native void freeEntry(long jzfile, long jzentry);
338
339    // the outstanding inputstreams that need to be closed,
340    // mapped to the inflater objects they use.
341    private final Map<InputStream, Inflater> streams = new WeakHashMap<>();
342
343    /**
344     * Returns an input stream for reading the contents of the specified
345     * zip file entry.
346     *
347     * <p> Closing this ZIP file will, in turn, close all input
348     * streams that have been returned by invocations of this method.
349     *
350     * @param entry the zip file entry
351     * @return the input stream for reading the contents of the specified
352     * zip file entry.
353     * @throws ZipException if a ZIP format error has occurred
354     * @throws IOException if an I/O error has occurred
355     * @throws IllegalStateException if the zip file has been closed
356     */
357    public InputStream getInputStream(ZipEntry entry) throws IOException {
358        if (entry == null) {
359            throw new NullPointerException("entry");
360        }
361        long jzentry = 0;
362        ZipFileInputStream in = null;
363        synchronized (this) {
364            ensureOpen();
365            if (!zc.isUTF8() && (entry.flag & EFS) != 0) {
366                jzentry = getEntry(jzfile, zc.getBytesUTF8(entry.name), true);
367            } else {
368                jzentry = getEntry(jzfile, zc.getBytes(entry.name), true);
369            }
370            if (jzentry == 0) {
371                return null;
372            }
373            in = new ZipFileInputStream(jzentry);
374
375            switch (getEntryMethod(jzentry)) {
376            case STORED:
377                synchronized (streams) {
378                    streams.put(in, null);
379                }
380                return in;
381            case DEFLATED:
382                // MORE: Compute good size for inflater stream:
383                long size = getEntrySize(jzentry) + 2; // Inflater likes a bit of slack
384                if (size > 65536) size = 8192;
385                if (size <= 0) size = 4096;
386                Inflater inf = getInflater();
387                InputStream is =
388                    new ZipFileInflaterInputStream(in, inf, (int)size);
389                synchronized (streams) {
390                    streams.put(is, inf);
391                }
392                return is;
393            default:
394                throw new ZipException("invalid compression method");
395            }
396        }
397    }
398
399    private class ZipFileInflaterInputStream extends InflaterInputStream {
400        private volatile boolean closeRequested = false;
401        private boolean eof = false;
402        private final ZipFileInputStream zfin;
403
404        ZipFileInflaterInputStream(ZipFileInputStream zfin, Inflater inf,
405                int size) {
406            super(zfin, inf, size);
407            this.zfin = zfin;
408        }
409
410        public void close() throws IOException {
411            if (closeRequested)
412                return;
413            closeRequested = true;
414
415            super.close();
416            Inflater inf;
417            synchronized (streams) {
418                inf = streams.remove(this);
419            }
420            if (inf != null) {
421                releaseInflater(inf);
422            }
423        }
424
425        // Override fill() method to provide an extra "dummy" byte
426        // at the end of the input stream. This is required when
427        // using the "nowrap" Inflater option.
428        protected void fill() throws IOException {
429            if (eof) {
430                throw new EOFException("Unexpected end of ZLIB input stream");
431            }
432            len = in.read(buf, 0, buf.length);
433            if (len == -1) {
434                buf[0] = 0;
435                len = 1;
436                eof = true;
437            }
438            inf.setInput(buf, 0, len);
439        }
440
441        public int available() throws IOException {
442            if (closeRequested)
443                return 0;
444            long avail = zfin.size() - inf.getBytesWritten();
445            return (avail > (long) Integer.MAX_VALUE ?
446                    Integer.MAX_VALUE : (int) avail);
447        }
448
449        protected void finalize() throws Throwable {
450            close();
451        }
452    }
453
454    /*
455     * Gets an inflater from the list of available inflaters or allocates
456     * a new one.
457     */
458    private Inflater getInflater() {
459        Inflater inf;
460        synchronized (inflaterCache) {
461            while (null != (inf = inflaterCache.poll())) {
462                if (false == inf.ended()) {
463                    return inf;
464                }
465            }
466        }
467        return new Inflater(true);
468    }
469
470    /*
471     * Releases the specified inflater to the list of available inflaters.
472     */
473    private void releaseInflater(Inflater inf) {
474        if (false == inf.ended()) {
475            inf.reset();
476            synchronized (inflaterCache) {
477                inflaterCache.add(inf);
478            }
479        }
480    }
481
482    // List of available Inflater objects for decompression
483    private Deque<Inflater> inflaterCache = new ArrayDeque<>();
484
485    /**
486     * Returns the path name of the ZIP file.
487     * @return the path name of the ZIP file
488     */
489    public String getName() {
490        return name;
491    }
492
493    /**
494     * Returns an enumeration of the ZIP file entries.
495     * @return an enumeration of the ZIP file entries
496     * @throws IllegalStateException if the zip file has been closed
497     */
498    public Enumeration<? extends ZipEntry> entries() {
499        ensureOpen();
500        return new Enumeration<ZipEntry>() {
501                private int i = 0;
502                public boolean hasMoreElements() {
503                    synchronized (ZipFile.this) {
504                        ensureOpen();
505                        return i < total;
506                    }
507                }
508                public ZipEntry nextElement() throws NoSuchElementException {
509                    synchronized (ZipFile.this) {
510                        ensureOpen();
511                        if (i >= total) {
512                            throw new NoSuchElementException();
513                        }
514                        long jzentry = getNextEntry(jzfile, i++);
515                        if (jzentry == 0) {
516                            String message;
517                            if (closeRequested) {
518                                message = "ZipFile concurrently closed";
519                            } else {
520                                message = getZipMessage(ZipFile.this.jzfile);
521                            }
522                            throw new ZipError("jzentry == 0" +
523                                               ",\n jzfile = " + ZipFile.this.jzfile +
524                                               ",\n total = " + ZipFile.this.total +
525                                               ",\n name = " + ZipFile.this.name +
526                                               ",\n i = " + i +
527                                               ",\n message = " + message
528                                );
529                        }
530                        ZipEntry ze = getZipEntry(null, jzentry);
531                        freeEntry(jzfile, jzentry);
532                        return ze;
533                    }
534                }
535            };
536    }
537
538    private ZipEntry getZipEntry(String name, long jzentry) {
539        ZipEntry e = new ZipEntry();
540        e.flag = getEntryFlag(jzentry);  // get the flag first
541        if (name != null) {
542            e.name = name;
543        } else {
544            byte[] bname = getEntryBytes(jzentry, JZENTRY_NAME);
545            if (!zc.isUTF8() && (e.flag & EFS) != 0) {
546                e.name = zc.toStringUTF8(bname, bname.length);
547            } else {
548                e.name = zc.toString(bname, bname.length);
549            }
550        }
551        e.time = getEntryTime(jzentry);
552        e.crc = getEntryCrc(jzentry);
553        e.size = getEntrySize(jzentry);
554        e. csize = getEntryCSize(jzentry);
555        e.method = getEntryMethod(jzentry);
556        e.extra = getEntryBytes(jzentry, JZENTRY_EXTRA);
557        byte[] bcomm = getEntryBytes(jzentry, JZENTRY_COMMENT);
558        if (bcomm == null) {
559            e.comment = null;
560        } else {
561            if (!zc.isUTF8() && (e.flag & EFS) != 0) {
562                e.comment = zc.toStringUTF8(bcomm, bcomm.length);
563            } else {
564                e.comment = zc.toString(bcomm, bcomm.length);
565            }
566        }
567        return e;
568    }
569
570    private static native long getNextEntry(long jzfile, int i);
571
572    /**
573     * Returns the number of entries in the ZIP file.
574     * @return the number of entries in the ZIP file
575     * @throws IllegalStateException if the zip file has been closed
576     */
577    public int size() {
578        ensureOpen();
579        return total;
580    }
581
582    /**
583     * Closes the ZIP file.
584     * <p> Closing this ZIP file will close all of the input streams
585     * previously returned by invocations of the {@link #getInputStream
586     * getInputStream} method.
587     *
588     * @throws IOException if an I/O error has occurred
589     */
590    public void close() throws IOException {
591        if (closeRequested)
592            return;
593        guard.close();
594        closeRequested = true;
595
596        synchronized (this) {
597            // Close streams, release their inflaters
598            synchronized (streams) {
599                if (false == streams.isEmpty()) {
600                    Map<InputStream, Inflater> copy = new HashMap<>(streams);
601                    streams.clear();
602                    for (Map.Entry<InputStream, Inflater> e : copy.entrySet()) {
603                        e.getKey().close();
604                        Inflater inf = e.getValue();
605                        if (inf != null) {
606                            inf.end();
607                        }
608                    }
609                }
610            }
611
612            // Release cached inflaters
613            Inflater inf;
614            synchronized (inflaterCache) {
615                while (null != (inf = inflaterCache.poll())) {
616                    inf.end();
617                }
618            }
619
620            if (jzfile != 0) {
621                // Close the zip file
622                long zf = this.jzfile;
623                jzfile = 0;
624
625                close(zf);
626            }
627
628            // Android-changed, explicit delete for OPEN_DELETE ZipFile.
629            if (fileToRemoveOnClose != null) {
630                fileToRemoveOnClose.delete();
631            }
632        }
633    }
634
635    /**
636     * Ensures that the system resources held by this ZipFile object are
637     * released when there are no more references to it.
638     *
639     * <p>
640     * Since the time when GC would invoke this method is undetermined,
641     * it is strongly recommended that applications invoke the <code>close</code>
642     * method as soon they have finished accessing this <code>ZipFile</code>.
643     * This will prevent holding up system resources for an undetermined
644     * length of time.
645     *
646     * @throws IOException if an I/O error has occurred
647     * @see    java.util.zip.ZipFile#close()
648     */
649    protected void finalize() throws IOException {
650        if (guard != null) {
651            guard.warnIfOpen();
652        }
653
654        close();
655    }
656
657    private static native void close(long jzfile);
658
659    private void ensureOpen() {
660        if (closeRequested) {
661            throw new IllegalStateException("zip file closed");
662        }
663
664        if (jzfile == 0) {
665            throw new IllegalStateException("The object is not initialized.");
666        }
667    }
668
669    private void ensureOpenOrZipException() throws IOException {
670        if (closeRequested) {
671            throw new ZipException("ZipFile closed");
672        }
673    }
674
675    /*
676     * Inner class implementing the input stream used to read a
677     * (possibly compressed) zip file entry.
678     */
679   private class ZipFileInputStream extends InputStream {
680        private volatile boolean closeRequested = false;
681        protected long jzentry; // address of jzentry data
682        private   long pos;     // current position within entry data
683        protected long rem;     // number of remaining bytes within entry
684        protected long size;    // uncompressed size of this entry
685
686        ZipFileInputStream(long jzentry) {
687            pos = 0;
688            rem = getEntryCSize(jzentry);
689            size = getEntrySize(jzentry);
690            this.jzentry = jzentry;
691        }
692
693        public int read(byte b[], int off, int len) throws IOException {
694            // Android-changed : Always throw an exception on read if the zipfile
695            // has already been closed.
696            ensureOpenOrZipException();
697
698            if (rem == 0) {
699                return -1;
700            }
701            if (len <= 0) {
702                return 0;
703            }
704            if (len > rem) {
705                len = (int) rem;
706            }
707            synchronized (ZipFile.this) {
708                len = ZipFile.read(ZipFile.this.jzfile, jzentry, pos, b,
709                                   off, len);
710            }
711            if (len > 0) {
712                pos += len;
713                rem -= len;
714            }
715            if (rem == 0) {
716                close();
717            }
718            return len;
719        }
720
721        public int read() throws IOException {
722            byte[] b = new byte[1];
723            if (read(b, 0, 1) == 1) {
724                return b[0] & 0xff;
725            } else {
726                return -1;
727            }
728        }
729
730        public long skip(long n) {
731            if (n > rem)
732                n = rem;
733            pos += n;
734            rem -= n;
735            if (rem == 0) {
736                close();
737            }
738            return n;
739        }
740
741        public int available() {
742            return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
743        }
744
745        public long size() {
746            return size;
747        }
748
749        public void close() {
750            if (closeRequested)
751                return;
752            closeRequested = true;
753
754            rem = 0;
755            synchronized (ZipFile.this) {
756                if (jzentry != 0 && ZipFile.this.jzfile != 0) {
757                    freeEntry(ZipFile.this.jzfile, jzentry);
758                    jzentry = 0;
759                }
760            }
761            synchronized (streams) {
762                streams.remove(this);
763            }
764        }
765
766        protected void finalize() {
767            close();
768        }
769    }
770
771    /**
772     * Returns {@code true} if, and only if, the zip file begins with {@code
773     * LOCSIG}.
774     *
775     * @hide
776     */
777    public boolean startsWithLocHeader() {
778        return locsig;
779    }
780
781    /** @hide */
782    // @VisibleForTesting
783    public int getFileDescriptor() {
784        return getFileDescriptor(jzfile);
785    }
786
787    private static native int getFileDescriptor(long jzfile);
788
789    private static native long open(String name, int mode, long lastModified,
790                                    boolean usemmap) throws IOException;
791    private static native int getTotal(long jzfile);
792    private static native boolean startsWithLOC(long jzfile);
793    private static native int read(long jzfile, long jzentry,
794                                   long pos, byte[] b, int off, int len);
795
796    // access to the native zentry object
797    private static native long getEntryTime(long jzentry);
798    private static native long getEntryCrc(long jzentry);
799    private static native long getEntryCSize(long jzentry);
800    private static native long getEntrySize(long jzentry);
801    private static native int getEntryMethod(long jzentry);
802    private static native int getEntryFlag(long jzentry);
803    private static native byte[] getCommentBytes(long jzfile);
804
805    private static final int JZENTRY_NAME = 0;
806    private static final int JZENTRY_EXTRA = 1;
807    private static final int JZENTRY_COMMENT = 2;
808    private static native byte[] getEntryBytes(long jzentry, int type);
809
810    private static native String getZipMessage(long jzfile);
811}
812