GZIPInputStream to String

前端 未结 8 1106
北恋
北恋 2020-12-04 21:49

I am attempting to convert the gzipped body of a HTTP response to plaintext. I\'ve taken the byte array of this response and converted it to a ByteArrayInputStream. I\'ve th

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 22:39

    import java.io.*;
    import java.util.zip.*;
    
    public class Ex1 {
    
        public static void main(String[] args) throws Exception{
            String str ;
    
            H h1 = new H();
            h1.setHcfId("PH12345658");
            h1.setHcfName("PANA HEALTH ACRE FACILITY");
    
            str = h1.toString();
            System.out.println(str);
    
            if (str == null || str.length() == 0) {
                return ;
            }
            ByteArrayOutputStream out = new ByteArrayOutputStream(str.length());
            GZIPOutputStream gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes());
            gzip.close();
            out.close();
    
            String s =  out.toString() ;
            System.out.println( s );
            byte[] ba = out.toByteArray();
            System.out.println( "---------------BREAK-------------" );
    
            ByteArrayInputStream in = new ByteArrayInputStream(ba);
            GZIPInputStream gzis = new GZIPInputStream(in);
            InputStreamReader reader = new InputStreamReader(gzis);
            BufferedReader pr = new BufferedReader(reader);
    
            String readed;
            while ((readed = pr.readLine()) != null) {
                System.out.println(readed);
            }
    
            //Close all the streams
        }
    
    }
    

提交回复
热议问题