📜  Java中的 Deflate()函数和示例

📅  最后修改于: 2022-05-13 01:54:45.343000             🧑  作者: Mango

Java中的 Deflate()函数和示例


Java .util.zipDeflater 类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-