模块  java.base
软件包  java.util.zip

Class ZipFile

  • 实现的所有接口
    CloseableAutoCloseable
    已知直接子类:
    JarFile

    public class ZipFile
    extends Object
    implements Closeable
    该类用于从zip文件中读取条目。

    除非另有说明,否则将null参数传递给null中的构造函数或方法将导致抛出NullPointerException

    API Note:
    要释放此ZipFile使用的资源,应显式调用close()方法或使用try-with-resources调用。 子类负责清理子类获取的资源。 应修改覆盖finalize()以执行清理的子类,以使用替代清理机制(如Cleaner并删除重写finalize方法。
    实现要求:
    如果这个ZipFile已经子类和close方法已被重写,该close方法将被终结时调用ZipFile不可达。 但是子类不应该依赖于这个具体的实现; 最终确定不可靠,并且不推荐使用finalize方法。
    从以下版本开始:
    1.1
    • 构造方法详细信息

      • ZipFile

        public ZipFile​(String name)
                throws IOException
        打开一个zip文件进行阅读。

        首先,如果有安全管理器,则使用name参数作为其参数调用其checkRead方法,以确保允许读取。

        UTF-8 charset用于解码条目名称和注释。

        参数
        name - zip文件的名称
        异常
        ZipException - 如果发生ZIP格式错误
        IOException - 如果发生I / O错误
        SecurityException - 如果存在安全管理器且其 checkRead方法不允许对文件进行读访问。
        另请参见:
        SecurityManager.checkRead(java.lang.String)
      • ZipFile

        public ZipFile​(File file,
                       int mode)
                throws IOException
        打开新的ZipFile以在指定模式下从指定的File对象读取。 mode参数必须是OPEN_READOPEN_READ | OPEN_DELETE

        首先,如果有安全管理器,则调用其checkRead方法,并将name参数作为其参数,以确保允许读取。

        UTF-8 charset用于解码条目名称和注释

        参数
        file - 要打开以供阅读的ZIP文件
        mode - 要打开文件的模式
        异常
        ZipException - 如果发生ZIP格式错误
        IOException - 如果发生I / O错误
        SecurityException - 如果存在安全管理器且其 checkRead方法不允许对该文件进行读访问,或者其 checkDelete方法在设置 OPEN_DELETE标志时不允许删除该文件。
        IllegalArgumentException - 如果 mode参数无效
        从以下版本开始:
        1.3
        另请参见:
        SecurityManager.checkRead(java.lang.String)
      • ZipFile

        public ZipFile​(File file)
                throws ZipException,
                       IOException
        在给定指定的File对象的情况下打开ZIP文件以进行读取。

        UTF-8 charset用于解码条目名称和注释。

        参数
        file - 要打开以供阅读的ZIP文件
        异常
        ZipException - 如果发生ZIP格式错误
        IOException - 如果发生I / O错误
      • ZipFile

        public ZipFile​(File file,
                       int mode,
                       Charset charset)
                throws IOException
        打开新的ZipFile以在指定模式下从指定的File对象读取。 mode参数必须是OPEN_READOPEN_READ | OPEN_DELETE

        首先,如果有安全管理器, checkRead name参数作为参数调用其checkRead方法,以确保允许读取。

        参数
        file - 要打开以供阅读的ZIP文件
        mode - 要打开文件的模式
        charset - 用于解码未使用UTF-8编码编码的ZIP条目名称和注释的charset (由条目的通用标志指示)。
        异常
        ZipException - 如果发生ZIP格式错误
        IOException - 如果发生I / O错误
        SecurityException - 如果存在安全管理器且其 checkRead方法不允许对文件进行读访问,或者其 checkDelete方法不允许在设置 OPEN_DELETE标志时删除该文件
        IllegalArgumentException - 如果 mode参数无效
        从以下版本开始:
        1.7
        另请参见:
        SecurityManager.checkRead(java.lang.String)
      • ZipFile

        public ZipFile​(String name,
                       Charset charset)
                throws IOException
        打开一个zip文件进行阅读。

        首先,如果有安全管理器,则调用其checkRead方法,并将name参数作为其参数,以确保允许读取。

        参数
        name - zip文件的名称
        charset - 用于解码未使用UTF-8编码编码的ZIP条目名称和注释的charset (由条目的通用标志指示)。
        异常
        ZipException - 如果发生ZIP格式错误
        IOException - 如果发生I / O错误
        SecurityException - 如果存在安全管理器且其 checkRead方法不允许对文件进行读访问
        从以下版本开始:
        1.7
        另请参见:
        SecurityManager.checkRead(java.lang.String)
      • ZipFile

        public ZipFile​(File file,
                       Charset charset)
                throws IOException
        在给定指定的File对象的情况下打开ZIP文件以进行读取。
        参数
        file - 要打开以供阅读的ZIP文件
        charset -该charset被用来ZIP条目名称和注释(忽略,如果解码language encoding bit ZIP条目的通用位标志的设置)。
        异常
        ZipException - 如果发生ZIP格式错误
        IOException - 如果发生I / O错误
        从以下版本开始:
        1.7
    • 方法详细信息

      • getComment

        public String getComment()
        返回zip文件注释,如果没有,则返回null。
        结果
        zip文件的注释字符串,如果没有则为null
        异常
        IllegalStateException - 如果zip文件已关闭
        从以下版本开始:
        1.7
      • getEntry

        public ZipEntry getEntry​(String name)
        返回指定名称的zip文件条目,如果未找到,则返回null。
        参数
        name - 条目的名称
        结果
        zip文件条目,如果未找到则为null
        异常
        IllegalStateException - 如果zip文件已关闭
      • getInputStream

        public InputStream getInputStream​(ZipEntry entry)
                                   throws IOException
        返回用于读取指定zip文件条目内容的输入流。

        关闭此ZIP文件将依次关闭此方法调用返回的所有输入流。

        参数
        entry - zip文件条目
        结果
        用于读取指定zip文件条目内容的输入流。
        异常
        ZipException - 如果发生ZIP格式错误
        IOException - 如果发生I / O错误
        IllegalStateException - 如果zip文件已关闭
      • getName

        public String getName()
        返回ZIP文件的路径名。
        结果
        ZIP文件的路径名
      • stream

        public Stream<? extends ZipEntry> stream()
        在ZIP文件条目上返回有序的Stream 条目按照它们出现在ZIP文件中心目录中的顺序显示在Stream中。
        结果
        此ZIP文件中有条目的 Stream条目
        异常
        IllegalStateException - 如果zip文件已关闭
        从以下版本开始:
        1.8
      • size

        public int size()
        返回ZIP文件中的条目数。
        结果
        ZIP文件中的条目数
        异常
        IllegalStateException - 如果zip文件已关闭
      • finalize

        @Deprecated(since="9",
                    forRemoval=true)
        protected void finalize()
                         throws IOException
        Deprecated, for removal: This API element is subject to removal in a future version.
        The finalize method has been deprecated and will be removed. It is implemented as a no-op. Subclasses that override finalize in order to perform cleanup should be modified to use alternative cleanup mechanisms and to remove the overriding finalize method. The recommended cleanup for ZipFile object is to explicitly invoke close method when it is no longer in use, or use try-with-resources. If the close is not invoked explicitly the resources held by this object will be released when the instance becomes unreachable.
        确保在没有对此ZipFile对象的引用时释放此ZipFile对象所拥有的系统资源。
        重写:
        finalizeObject
        异常
        IOException - 如果发生I / O错误
        另请参见:
        WeakReferencePhantomReference