Java中的Java.util.zip.ZipInputStream类

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)

Java中的Java.util.zip.ZipInputStream类

该类实现一个输入流过滤器,用于读取ZIP文件格式的文件。包括对压缩和未压缩条目的支持。
构造函数:

  • ZipInputStream(InputStream in):创建一个新的ZIP输入流。
  • ZipInputStream(InputStream in,Charset charset):创建一个新的ZIP输入流

方法 :

  • int available():在EOF达到当前条目数据后返回0,否则始终返回。
    程序不应该依赖此方法来返回
    可以不受阻塞地读取的实际字节数。

    Syntax :public int available()
                  throws IOException
    Overrides:
    available in class InflaterInputStream
    Returns:
    1 before EOF and 0 after EOF has reached for current entry. 
    Programs should not count on this method to return the actual number 
    of bytes that could be read without blocking.
    Throws:
    IOException
  • void close():关闭此输入流并释放与该流关联的所有系统资源。
    Syntax :public void close()
               throws IOException
    Overrides:
    close in class InflaterInputStream
    Throws:
    IOException
  • void closeEntry():关闭当前的ZIP条目并定位流以读取下一个条目。
    Syntax :public void closeEntry()
                    throws IOException
    Throws:
    ZipException 
    IOException
  • protected ZipEntry createZipEntry(String name):为指定的条目名称创建一个新的ZipEntry对象。
    Syntax :protected ZipEntry createZipEntry(String name)
    Parameters:
    name - the ZIP file entry name
    Returns:
    the ZipEntry just created
  • ZipEntry getNextEntry():读取下一个ZIP文件条目并将流定位在条目数据的开头。
    Syntax :public ZipEntry getNextEntry()
                          throws IOException
    Returns:
    the next ZIP file entry, or null if there are no more entries
    Throws:
    ZipException 
    IOException
  • int read(byte [] b,int off,int len):从当前ZIP条目读入一个字节数组。如果len不为零,则该方法将阻塞,直到某些输入可用; 否则,不读取字节并返回0。
    Syntax :public int read(byte[] b,
           int off,
           int len)
             throws IOException
    Parameters:
    b - the buffer into which the data is read
    off - the start offset in the destination array b
    len - the maximum number of bytes read
    Returns:
    the actual number of bytes read, or -1 if the end of the entry is reached
    Throws:
    NullPointerException 
    IndexOutOfBoundsException 
    ZipException 
    IOException
  • long skip(long n):跳过当前ZIP条目中指定的字节数。
    Syntax :public long skip(long n)
              throws IOException
    Parameters:
    n - the number of bytes to skip
    Returns:
    the actual number of bytes skipped
    Throws:
    ZipException
    IOException
    IllegalArgumentException
//Java program demonstrating ZipInputStream methods

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
class ZipInputStreamDemo extends ZipInputStream
{
    public ZipInputStreamDemo(InputStream in)
    {
        super(in);
    }

    public static void main(String[] args) throws IOException
    {
        FileInputStream fis = new FileInputStream("Awesome CV.zip");
        ZipInputStream zis = new JarInputStream(fis);
        ZipInputStreamDemo obj = new ZipInputStreamDemo(zis);

        //illustrating createZipEntry()
        ZipEntry ze = obj.createZipEntry("ZipEntry");
        System.out.println(ze.getName());

        //illustrating getNextEntry()
        ZipEntry je = zis.getNextEntry();
        System.out.println(je.getName());

        //illustrating skip() method
        zis.skip(3);

        //illustrating closeEntry() method
        zis.closeEntry();
        zis.getNextEntry();
        byte b[] = new byte[10];

        //illustrating available() method
        //Reads up to byte.length bytes of data from this input stream
        if(zis.available() == 1)
            zis.read(b);
        System.out.println(Arrays.toString(b));

        //closing the stream
        zis.close();
    }
}

输出:

ZipEntry
awesome-cv.cls
[35, 32, 65, 119, 101, 115, 111, 109, 101, 32]
  •   本文标题:Java中的Java.util.zip.ZipInputStream类 - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/4394.html
      微信返利机器人
      免费:淘宝,京东,拼多多优惠券
      腾讯,爱奇艺,优酷的VIP视频免费解析,免费看
      即刻扫描二维码,添加微信机器人!

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注