當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。