zipfile.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 555 行 · 第 1/2 页
JAVA
555 行
/* * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package java.util.zip;import java.io.InputStream;import java.io.IOException;import java.io.EOFException;import java.io.File;import java.util.Vector;import java.util.Enumeration;import java.util.NoSuchElementException;import java.security.AccessController;import java.security.PrivilegedAction;import sun.io.Markable;import sun.io.MarkableReader;/** * This class is used to read entries from a zip file. * * @version 1.60, 10/10/06 * @author David Connelly */publicclass ZipFile implements ZipConstants { private long jzfile; // address of jzfile data private String name; // zip file name private int total; // total number of entries private boolean deletemode = false; // zip file mode private File zipfile; // zip file pointer for OPEN_DELETE mode private static final int STORED = ZipEntry.STORED; private static final int DEFLATED = ZipEntry.DEFLATED; /** * Mode flag to open a zip file for reading. */ public static final int OPEN_READ = 0x1; /** * Mode flag to open a zip file and mark it for deletion. The file will be * deleted some time between the moment that it is opened and the moment * that it is closed, but its contents will remain accessible via the * <tt>ZipFile</tt> object until either the close method is invoked or the * virtual machine exits. */ public static final int OPEN_DELETE = 0x4; static { AccessController.doPrivileged( new sun.security.action.LoadLibraryAction("zip")); initIDs(); } private static native void initIDs(); /** * Opens a zip file for reading. * * <p>First, if there is a security * manager, its <code>checkRead</code> method * is called with the <code>name</code> argument * as its argument to ensure the read is allowed. * * @param name the name of the zip file * @exception ZipException if a ZIP format error has occurred * @exception IOException if an I/O error has occurred * @exception SecurityException if a security manager exists and its * <code>checkRead</code> method doesn't allow read access to the file. * @see SecurityManager#checkRead(java.lang.String) */ public ZipFile(String name) throws IOException { this(new File(name), OPEN_READ); } /** * Opens a new <code>ZipFile</code> to read from the specified * <code>File</code> object in the specified mode. The mode argument * must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>. * * <p>First, if there is a security manager, its <code>checkRead</code> * method is called with the <code>name</code> argument as its argument to * ensure the read is allowed. * * @param file the ZIP file to be opened for reading * @param mode the mode in which the file is to be opened * @exception ZipException if a ZIP format error has occurred * @exception IOException if an I/O error has occurred * @exception SecurityException if a security manager exists and its * <code>checkRead</code> method doesn't allow read access to the file, * or <code>checkDelete</code> method doesn't allow deleting the file * when <tt>OPEN_DELETE</tt> flag is set. * @exception IllegalArgumentException * If the <tt>mode</tt> argument is invalid * @see SecurityManager#checkRead(java.lang.String) */ public ZipFile(File file, int mode) throws IOException { if (((mode & OPEN_READ) == 0) || ((mode & ~(OPEN_READ | OPEN_DELETE)) != 0)) { throw new IllegalArgumentException("Illegal mode: 0x"+ Integer.toHexString(mode)); } String name = file.getPath(); SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkRead(name); if ((mode & OPEN_DELETE) != 0) { sm.checkDelete(name); } } /* Mask out OPEN_DELETE */ if ((mode & OPEN_DELETE) != 0) { mode &= ~OPEN_DELETE; deletemode = true; zipfile = file; } long jzfileCopy = open(name, mode, file.lastModified()); this.name = name; this.total = getTotal(jzfileCopy); jzfile = jzfileCopy; } private static native long open(String name, int mode, long lastModified); private static native int getTotal(long jzfile); /** * Opens a ZIP file for reading given the specified File object. * @param file the ZIP file to be opened for reading * @exception ZipException if a ZIP error has occurred * @exception IOException if an I/O error has occurred */ public ZipFile(File file) throws ZipException, IOException { this(file, OPEN_READ); } /** * Returns the zip file entry for the specified name, or null * if not found. * * @param name the name of the entry * @return the zip file entry, or null if not found * @exception IllegalStateException if the zip file has been closed */ public ZipEntry getEntry(String name) { if (name == null) { throw new NullPointerException("name"); } long jzentry = 0; synchronized (this) { ensureOpen(jzfile); jzentry = getEntry(jzfile, name); if (jzentry == 0 && !name.endsWith("/")) { // try a directory name jzentry = getEntry(jzfile, name + "/"); } if (jzentry != 0) { ZipEntry ze = new ZipEntry(name, jzentry); freeEntry(jzfile, jzentry); return ze; } } return null; } private static native long getEntry(long jzfile, String name); // freeEntry releases the C jzentry struct. private static native void freeEntry(long jzfile, long jzentry); /** * Returns an input stream for reading the contents of the specified * zip file entry. * * Returns an input stream for reading the contents of the specified * zip file entry. * * <p> Closing this ZIP file will, in turn, close all input * streams that have been returned by invocations of this method. * * @param entry the zip file entry * @return the input stream for reading the contents of the specified * zip file entry. * @exception ZipException if a ZIP format error has occurred * @exception IOException if an I/O error has occurred * @exception IllegalStateException if the zip file has been closed */ public InputStream getInputStream(ZipEntry entry) throws IOException { return getInputStream(entry.name); } /** * Returns an input stream for reading the contents of the specified * entry, or null if the entry was not found. */ private InputStream getInputStream(String name) throws IOException { if (name == null) { throw new NullPointerException("name"); } long jzentry = 0; InputStream in = null; synchronized (this) { ensureOpen(jzfile); jzentry = getEntry(jzfile, name); if (jzentry == 0) { return null; } in = new ZipFileInputStream(jzentry, this); } switch (getMethod(jzentry)) { case STORED: return in; case DEFLATED: return new InflaterInputStream(in, getInflater()) { private boolean isClosed = false; public void close() throws IOException { if (!isClosed) { releaseInflater(inf); this.in.close(); isClosed = true; } } // Override fill() method to provide an extra "dummy" byte // at the end of the input stream. This is required when // using the "nowrap" Inflater option. protected void fill() throws IOException { if (eof) { throw new EOFException( "Unexpected end of ZLIB input stream"); } len = this.in.read(buf, 0, buf.length); if (len == -1) { buf[0] = 0; len = 1; eof = true; } inf.setInput(buf, 0, len); } private boolean eof; public int available() throws IOException { if (super.available() != 0) { return this.in.available(); } else { return 0; } } };
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?