1
votes

I am using following code to compress and then decompress the string. But I am getting a different length of string after decompression, also few characters are missing from the decompressed string.

Compression:

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            GZIPOutputStream zos = new GZIPOutputStream(baos);

            zos.write(text.getBytes());
            zos.finish();
            zos.flush();

            byte[] udpBuffer = baos.toByteArray();          
            System.out.println("Compressed length: " + udpBuffer.length);

Decompression:

        GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes));
        BufferedReader br = new BufferedReader(new InputStreamReader(gis));     

        StringBuilder sb = new StringBuilder();
        while (br.readLine()!= null) {
            sb.append(br.readLine());
        }

        System.err.println(sb.toString());

Text Original length: 45627 bytes
Text Compressed length: 3732 bytes
Text Uncompressed length: 20328 bytes (which should be equal to Original length)

My Original text is something like this:

<html>
<head>
   <title></title>
</head>
<body>
<p><span class="preheader" style="display:none!important;mso-hide:all">Hey wazzup? </span></p>

<table align="center" border="0" cellpadding="0" cellspacing="0" width="640">
   <tbody>
       <tr>
           <td align="center" height="30" style="font-size:11px;color:#333;font-family:Verdana,Geneva,sans-serif">
.
.
.
</tbody>
</body>
</html>

And my decompressed text is coming like (see the start tag is missing, and also the

<title> tag, similarly many tags and other parts are missing from my uncompressed text:
<head></head><p><span class="preheader" style="display:none!important;mso-hide:all">
.
.
.

Can anyone point out the mistake? Or is this the expected behaviour?

1

1 Answers

1
votes

Change

while (br.readLine()!= null) {
    sb.append(br.readLine());
}

to

String line;
while ((line = br.readLine()) != null) {
    sb.append(line);
}

Also, zos.flush() should be called before zos.finish().