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


Java Object Compression用法及代码示例

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

需要对象压缩

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

Java 中对象压缩是如何完成的

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

我们需要扩展 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.

Before Object Compression: After Object Compression: The size of the file can be compressed up to 70% of its initial size.

优点

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

坏处

  1. 如果文件很大,压缩和解压缩可能会花费一些时间。


相关用法


注:本文由纯净天空筛选整理自abhishekg25大神的英文原创作品 Object Compression in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。