當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。