当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.util.zip.ZipInputStream用法及代码示例


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

  • ZipInputStream(输入流中):创建新的 ZIP 输入流。
  • ZipInputStream(InputStream in, Charset 字符集):创建新的 ZIP 输入流

方法:

  • int available():当前条目数据到达 EOF 后返回 0,否则始终返回 。
    程序不应指望此方法返回实际字节数
    可以无阻塞地读取。
    Syntax :public int available()
                  throws IOException
    Overrides:
    available in class InflaterInputStream
    返回:
    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 
  • 无效close():关闭此输入流并释放与该流关联的所有系统资源。
    Syntax :public void close()
               throws IOException
    Overrides:
    close in class InflaterInputStream
    Throws:
    IOException
  • 无效closeEntry():关闭当前 ZIP 条目并定位流以读取下一个条目。
    Syntax :public void closeEntry()
                    throws IOException
    Throws:
    ZipException 
    IOException 
  • protected ZipEntry createZipEntry(字符串名称):为指定条目名称创建新的ZipEntry对象。
    Syntax :protected ZipEntry createZipEntry(String name)
    参数:
    name - the ZIP file entry name
    返回:
    the ZipEntry just created
  • ZipEntry getNextEntry():读取下一个 ZIP 文件条目并将流定位在条目数据的开头。
    Syntax :public ZipEntry getNextEntry()
                          throws IOException
    返回:
    the next ZIP file entry, or null if there are no more entries
    Throws:
    ZipException 
    IOException 
  • int 读取(字节[] b,int 关闭,int len):从当前 ZIP 条目读取到字节数组中。如果 len 不为零,则该方法将阻塞,直到某些输入可用为止;否则,不会读取任何字节并返回 0。
    Syntax :public int read(byte[] b,
           int off,
           int len)
             throws IOException
    参数:
    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
    返回:
    the actual number of bytes read, or -1 if the end of the entry is reached
    Throws:
    NullPointerException 
    IndexOutOfBoundsException 
    ZipException 
    IOException
  • 长跳过(长n):跳过当前 ZIP 条目中指定的字节数。
    Syntax :public long skip(long n)
              throws IOException
    参数:
    n - the number of bytes to skip
    返回:
    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.util.zip.ZipInputStream class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。