集册 Java实例教程 解压缩GZIP中的字节数组

解压缩GZIP中的字节数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

566
解压缩GZIP中的字节数组

/* 
*来 自
 n o w j a   v  a . c o m - 时  代  Java
*/

//package com.nowjava;

import java.io.BufferedReader;

import java.io.ByteArrayInputStream;


import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;


import java.util.zip.GZIPInputStream;


public class Main {

    public static String decompress(byte[] compressed) throws IOException {

        byte[] h = new byte[2];

        h[0] = (compressed)[0];

        h[1] = (compressed)[1];

        int head = getShort(h);

        boolean t = head == 0x1f8b;

        InputStream in = null;
        /*
        时代Java - N o w  J a v a . c o m
        */

        StringBuilder sb = new StringBuilder();

        try {

            ByteArrayInputStream bis = new ByteArrayInputStream(compressed);

            if (t) {

                in = new GZIPInputStream(bis);

            } else {

                in = bis;

            }

            BufferedReader r = new BufferedReader(

                    new InputStreamReader(in), 1000);

            for (String line = r.readLine(); line != null; line = r

                    .readLine()) {

                sb.append(line);

            }

            r.close();

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            if (in != null) {
展开阅读全文