📜  Java中的对象压缩与示例

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

Java中的对象压缩与示例

对象压缩是在各种类和方法的帮助下减小对象大小的过程。然后接收方通过解压缩发送方压缩的对象来检索完整信息。看下图来区分压缩前的文件大小和压缩后的文件大小:

需要对象压缩

Java对象压缩在我们需要通过网络传输大量对象的情况下非常有用。对象压缩在发送方完成,并在另一端(即接收方)解压缩。这样,我们可以提高性能,并且可以在两端之间发送和接收大量的对象。

Java中如何进行对象压缩

Java对象压缩是使用 GZIPOutputStream 类完成的(该类实现了一个流过滤器,用于以 GZIP 文件格式写入压缩数据)并将其传递给 ObjectOutputStream 类(该类扩展了 OutputStream 并实现了 ObjectOutput、ObjectStreamConstants)以将对象写入一个外部文件。

我们需要扩展 Serializable 类,因为我们要压缩的对象将由 User 对象表示。这就是为什么我们需要使 User 对象可序列化。

Java中对象压缩的实现

Java
// Java Program to demonstrate
// Object Compression
  
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.zip.GZIPOutputStream;
import java.io.File;
  
public class GFG {
  
    public static void main(String args[])
    {
  
        // Creating objects of Java Class Bill
        Bill b1
            = new Bill("176BU", "Abhishek Gupta");
        Bill b2
            = new Bill("176DA", "Sushant Singh");
        FileOutputStream f = null;
  
        // Creates a GZIPOutputStream and
        // initialize it with null
        GZIPOutputStream g = null;
  
        // Creates an ObjectOutputStream and
        // initialise it with null
        // ObjectOutputStream writes to the
        // defined GZIPOutputStream.
        ObjectOutputStream o = null;
  
        // Write path of the file in the argument
        File newFile
            = new File("File.dat");
        try {
  
            // Pass the File object
            // (newFile) to the
            // FileOutputStream
            f = new FileOutputStream(newFile);
            g = new GZIPOutputStream(f);
  
            // Now pass the GZIPOutputStream object
            // to the ObjectOutputStream
            o = new ObjectOutputStream(g);
  
            // Writes the object that are going
            // to be compressed to the
            // ObjectOutputStream using
            // writeObject(objectName)
            o.writeObject(b1);
            o.writeObject(b2);
  
            // flush() API methods of
            // ObjectOutputStream.
            o.flush();
            System.out.println(
                "Process done..");
            System.out.println(
                "Objects are compressed");
        }
        catch (
            FileNotFoundException e) {
  
            // Catch Block
            System.out.println(
                e.message());
        }
        catch (IOException e) {
  
            // Catch Block
            System.out.println(
                e.message());
        }
        finally {
            try {
  
                // Using their
                // close() API methods,
                // closes both
                // the GZIPOutputStream
                // and the
                // ObjectOutputStream
                if (o != null)
                    o.close();
                if (g != null)
                    g.close();
            }
            catch (Exception ex) {
  
                // Catch block
                System.out.println(
                    ex.message());
            }
        }
    }
}
  
class Bill implements Serializable {
  
    // Declaring the private variables
    private String billno;
    private String buyerName;
  
    // Creating constructor
    // of Java Class Bill
    public Bill(
        String bill, String buyer)
    {
        this.billno = bill;
        this.buyerName = buyer;
    }
  
    // Defining methods initializing
    // variables billno and buyerName
    public String getBill()
    {
        return billno;
    }
    public void setBill(
        String billno)
    {
        this.billno = billno;
    }
    public String getBuyerName()
    {
        return buyerName;
    }
    public void setBuyerName(
        String buyer)
    {
        this.buyerName = buyer;
    }
}


输出:

Process done..
Objects are compressed.

好处

  1. 可以通过网络传输大量对象,而不会出现任何性能问题。
  2. 压缩-解压缩过程中不会丢失任何数据。

坏处

  1. 如果文件的大小非常大,压缩和解压缩可能需要一些时间。