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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。