Java-从Gzip文件中读出对象

Java-从Gzip文件中读出对象

Tags: Java

摘要

Java-从Gzip文件中读出对象

读取Gzip文件

同压缩一样处理,FileInputStream包裹GzipInputStream。

``java
FileInputStream fin = new FileInputStream(“/home/zhu/javacore.cn/address.gz”);
GZIPInputStream gis = new GZIPInputStream(fin);
ObjectInputStream ois = new ObjectInputStream(gis);
address = (Address) ois.readObject();



##反序列化对象完整代码

```java
package cn.javacore.io.zip;

import cn.javacore.io.serialization.Address;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.zip.GZIPInputStream;

public class _07_DeserializerFromGzip implements Serializable {

    public static void main(String args[]) {

        _07_DeserializerFromGzip deserializer = new _07_DeserializerFromGzip();
        Address address = deserializer.deserialzeAddress();
        System.out.println(address);
    }

    public Address deserialzeAddress() {

        Address address;

        try {

            FileInputStream fin = new FileInputStream("/home/zhu/javacore.cn/address.gz");
            GZIPInputStream gis = new GZIPInputStream(fin);
            ObjectInputStream ois = new ObjectInputStream(gis);
            address = (Address) ois.readObject();
            ois.close();

            return address;

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

输出结果

对应上一节中序列化前设置的内容

Street : 信息路 Country : 中国北京