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

参考资料
- https://docs.oracle.com/javase/9/docs/api/java/util/zip/Deflater.html#deflate-byte:A-
 - https://docs.oracle.com/javase/9/docs/api/java/util/zip/Deflater.html#deflate-byte:A-int-int-
 - https://docs.oracle.com/javase/9/docs/api/java/util/zip/Deflater.html#deflate-byte:A-int-int-int-
 
极客教程