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


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


此類實現一個輸出流過濾器,用於解壓縮以 “deflate” 壓縮格式存儲的數據。
構造函數

  • InflaterOutputStream(輸出流輸出):創建具有默認解壓縮器和緩衝區大小的新輸出流。
  • InflaterOutputStream(OutputStream out, Inflater infl):使用指定的解壓縮器和默認緩衝區大小創建新的輸出流。
  • InflaterOutputStream(OutputStream out, Inflater infl, int bufLen):使用指定的解壓縮器和緩衝區大小創建新的輸出流。

方法:

  • 無效close():將任何剩餘的未壓縮數據寫入輸出流並關閉底層輸出流。
    Syntax :
    public void close()
               throws IOException
    Overrides:
    close in class FilterOutputStream
    Throws:
    IOException
  • 無效finish():完成將未壓縮數據寫入輸出流,而不關閉底層流。
    Syntax :public void finish()
                throws IOException
    Throws:
    IOException
  • 無效flush():刷新此輸出流,強製寫入任何掛起的緩衝輸出字節。
    Syntax :public void flush()
               throws IOException
    Overrides:
    flush in class FilterOutputStream
    Throws:
    IOException
  • void write(byte[] b, int off, int len):將字節數組寫入未壓縮的輸出流。
    Syntax :public void write(byte[] b,
             int off,
             int len)
               throws IOException
    Overrides:
    write in class FilterOutputStream
    Parameters:
    b - buffer containing compressed data to decompress and write to the output stream
    off - starting offset of the compressed data within b
    len - number of bytes to decompress from b
    Throws:
    IndexOutOfBoundsException 
    IOException
    NullPointerException 
    ZipException
  • 無效寫入(int b):將一個字節寫入未壓縮的輸出流。
    Syntax :public void write(int b)
               throws IOException
    Parameters:
    b - a single byte of compressed data to decompress and write to the output stream
    Throws:
    IOException
    ZipException 
    

程序1:


//Java program to illustrate InflaterInputStream class 
import java.io.*; 
import java.util.Arrays; 
import java.util.zip.DeflaterInputStream; 
import java.util.zip.InflaterOutputStream; 
  
class InflaterOutputStreamDemo  
{ 
    public static void main(String[] args) throws IOException  
    { 
        byte b[] = {1, 2, 3, 4, 5, 6}; 
        ByteArrayInputStream bin = new ByteArrayInputStream(b); 
        DeflaterInputStream din = new DeflaterInputStream(bin); 
        byte c[] = new byte[10]; 
        din.read(c); 
        din.close(); 
  
  
        ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
        InflaterOutputStream ios = new InflaterOutputStream(bos); 
        //illustrating write(byte b[],int off,int len) 
        ios.write(c); 
  
        //flushing 
        ios.flush(); 
          
        //Finishes writing uncompressed data to the output stream 
        // without closing the underlying stream. 
        ios.finish(); 
        System.out.println(Arrays.toString(bos.toByteArray())); 
        bos.close(); 
          
        //illustrating close() 
        ios.close(); 
    } 
} 

輸出:

[1, 2, 3, 4, 5, 6]

程序2:


//Java program to illustrate InflaterInputStream class 
import java.io.*; 
import java.util.Arrays; 
import java.util.zip.DeflaterInputStream; 
import java.util.zip.InflaterOutputStream; 
  
class InflaterOutputStreamDemo  
{ 
    public static void main(String[] args) throws IOException  
    { 
        byte b[] = {1, 2, 3, 4, 5, 6}; 
        ByteArrayInputStream bin = new ByteArrayInputStream(b); 
        DeflaterInputStream din = new DeflaterInputStream(bin); 
        FileOutputStream fos=new FileOutputStream("file.txt"); 
        byte c[] = new byte[10]; 
        din.read(c); 
        fos.write(c); 
        din.close(); 
        fos.close(); 
  
        //reading the compressed data 
        FileInputStream fis = new FileInputStream("file.txt"); 
  
        ByteArrayOutputStream bos1=new ByteArrayOutputStream(); 
        InflaterOutputStream ios = new InflaterOutputStream(bos1); 
        int ch; 
          
        //illustrating write() method 
        while ( (ch=fis.read() ) != -1)  
        { 
            ios.write(ch); 
        } 
        System.out.print(Arrays.toString(bos1.toByteArray())); 
    } 
  
} 

輸出:

[1, 2, 3, 4, 5, 6]

下一篇: Java 中的 Java.util.zip.InflaterInputStream 類



相關用法


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