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


Java Deflater deflate()用法及代碼示例



java.util.zip中Deflater類的deflate()函數用於壓縮輸入數據,並使用壓縮後的數據填充給定的緩衝區。該函數返回壓縮數據的字節數。

函數簽名:

public int deflate(byte[] b)
public int deflate(byte[] b, int offset, int length, int flush)
public int deflate(byte[] b, int offset, int length)

用法:


d.deflate(byte[])
d.deflate(byte[], int, int, int)
d.deflate(byte[], int, int )

參數:這些重載函數接受的各種參數是:

  • byte[] b:這是要放氣的輸入數組
  • int offset:這是要從給定數組中讀取值的起始偏移量
  • int length:這是從起始偏移量開始壓縮的最大長度。
  • int flush:這是作為參數傳遞的衝洗模式

返回類型:該函數返回一個整數值,該值是壓縮數據的大小。

異常:如果刷新模式無效,則該函數引發IllegalArgumentException。共有三種有效的刷新模式:NO_FLUSH,SYNC_FLUSH,FULL_FLUSH。

下麵的示例演示上述函數的用法:

範例1:演示deflate(byte [] b)函數的用法

// Java program to demonstrate 
// the use of deflate(byte[] b) function 
  
import java.util.zip.*; 
import java.io.UnsupportedEncodingException; 
  
class GFG { 
    public static void main(String args[]) 
        throws UnsupportedEncodingException 
    { 
        // deflater 
        Deflater d = new Deflater(); 
  
        // get the text 
        String pattern = "GeeksforGeeks", text = ""; 
  
        // generate the text 
        for (int i = 0; i < 4; i++) 
            text += pattern; 
  
        // set the input for deflator 
        d.setInput(text.getBytes("UTF-8")); 
  
        // finish 
        d.finish(); 
  
        // output bytes 
        byte output[] = new byte[1024]; 
  
        // compress the data 
        int size = d.deflate(output); 
  
        // compressed String 
        System.out.println("Compressed String:"
                           + new String(output) 
                           + "\n Size " + size); 
  
        // original String 
        System.out.println("Original String:"
                           + text + "\n Size "
                           + text.length()); 
  
        // end 
        d.end(); 
    } 
}

輸出:

Compressed String:x?sOM?.N?/r???q??
 Size 21
Original String:GeeksforGeeksGeeksforGeeksGeeksforGeeksGeeksforGeeks
 Size 52

範例2:演示deflate(byte [] b,int offset,int length)函數的用法

// Java program to demonstrate the use 
// of deflate(byte[] b, int offset, int length) function 
  
import java.util.zip.*; 
import java.io.UnsupportedEncodingException; 
  
class GFG { 
    public static void main(String args[]) 
        throws UnsupportedEncodingException 
    { 
        // deflater 
        Deflater d = new Deflater(); 
  
        // get the text 
        String pattern = "GeeksforGeeks", text = ""; 
  
        // generate the text 
        for (int i = 0; i < 4; i++) 
            text += pattern; 
  
        // set the input for deflator 
        d.setInput(text.getBytes("UTF-8")); 
  
        // finish 
        d.finish(); 
  
        // output bytes 
        byte output[] = new byte[1024]; 
  
        // compress the data, with given offset and 
        // set maximum size of compressed string 
        int size = d.deflate(output, 2, 13); 
  
        // compressed String 
        System.out.println("Compressed String:"
                           + new String(output) 
                           + "\n Size " + size); 
  
        // original String 
        System.out.println("Original String:"
                           + text + "\n Size "
                           + text.length()); 
  
        // end 
        d.end(); 
    } 
}

輸出:

Compressed String:x?sOM?.N?/r?
 Size 13
Original String:GeeksforGeeksGeeksforGeeksGeeksforGeeksGeeksforGeeks
 Size 52

範例3:演示deflate(byte [] b,int offset,int length,int flush)函數的用法

// Java program to demonstrate the use of 
// deflate(byte[] b, int offset, int length, int flush) function 
  
import java.util.zip.*; 
import java.io.UnsupportedEncodingException; 
  
class GFG { 
    public static void main(String args[]) 
        throws UnsupportedEncodingException 
    { 
        // deflater 
        Deflater d = new Deflater(); 
  
        // get the text 
        String pattern = "GeeksforGeeks", text = ""; 
  
        // generate the text 
        for (int i = 0; i < 4; i++) 
            text += pattern; 
  
        // set the input for deflator 
        d.setInput(text.getBytes("UTF-8")); 
  
        // finish 
        d.finish(); 
  
        // output bytes 
        byte output[] = new byte[1024]; 
  
        // compress the data, with given offset and 
        // set maximum size of compressed string 
        // and specified Flush 
        int size = d.deflate(output, 2, 13, Deflater.FULL_FLUSH); 
  
        // compressed String 
        System.out.println("Compressed String:"
                           + new String(output) 
                           + "\n Size " + size); 
  
        // original String 
        System.out.println("Original String:" + text 
                           + "\n Size " + text.length()); 
  
        // end 
        d.end(); 
    } 
}

輸出:

Compressed String:x?sOM?.N?/r?
 Size 13
Original String:GeeksforGeeksGeeksforGeeksGeeksforGeeksGeeksforGeeks
 Size 52

參考:



相關用法


注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 Deflater deflate() function in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。