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


Java Java.util.zip.GZIPInputStream用法及代碼示例


此類實現一個流過濾器,用於讀取 GZIP 文件格式的壓縮數據。

構造函數

  • GZIPInputStream(輸入流中):創建具有默認緩衝區大小的新輸入流。
  • GZIPInputStream(輸入流,int大小):創建具有指定緩衝區大小的新輸入流。

方法:

  • 無效close():關閉此輸入流並釋放與該流關聯的所有係統資源。
    Syntax :public void close()
               throws IOException
    Specified by:
    close in interface Closeable
    Specified by:
    close in interface AutoCloseable
    Overrides:
    close in class InflaterInputStream
    Throws:
    IOException 
  • int 讀取(字節[] buf,int 關閉,int len):將未壓縮的數據讀入字節數組。如果 len 不為零,該方法將阻塞,直到可以解壓縮某些輸入;否則,不會讀取任何字節並返回 0。
    Syntax :public int read(byte[] buf,
           int off,
           int len)
             throws IOException
    Overrides:
    read in class InflaterInputStream
    Parameters:
    buf - 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
    compressed input stream is reached
    Throws:
    NullPointerException
    IndexOutOfBoundsException
    ZipException
    IOException 

從類 java.util.zip.InflaterInputStream 繼承的方法
可用、填充、標記、標記支持、讀取、重置、跳過
從類 java.io.FilterInputStream 繼承的方法

從類 java.lang.Object 繼承的方法
克隆、等於、終結、getClass、hashCode、通知、notifyAll、toString、等待、等待、等待

程序:


                                                                                
//Java program demonstrating GZipInputStream methods  
  
import java.io.FileInputStream;                  
import java.io.FileOutputStream;      
import java.io.IOException;                  
import java.util.Arrays; 
import java.util.zip.GZIPInputStream;  
  
class GZipInputStreamDemo          
{                                                                              
    public static void main(String[] args) throws IOException  
    {                                                                                              
        FileInputStream fis = new FileInputStream("file.txt");  
        GZIPInputStream gzis = new GZIPInputStream(fis);     
          
        //Uncompressed FileContents          
        //01234567890  
        byte b[]=new byte[10]; 
                                                      
        //skipping 1 byte      
        gzis.skip(1); 
          
        //illustrating available() and  
        //read(byte b[],int off,int len)  
        if( gzis.available()!=-1)      
            gzis.read(b);                      
        System.out.println(Arrays.toString(b)); 
                                      
        //closing the stream                                                  
        gzis.close();                                                          
    }                                                                          
}  

輸出:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]


相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.util.zip.GZIPInputStream class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。