Search code examples
javabase64deflateinflate

Compression and Encoding giving Wrong results in Strings


I'm trying to compress a string . I'm using Base64 encoding and decoding to conversion of String to Bytes and viceversa.

import org.apache.axis.encoding.Base64;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class UtilTesting {
    public static void main(String[] args) {


        try {
            String original = "I am the god";
            System.out.println("Starting Zlib");
            System.out.println("==================================");
            String zcompressed = compressString(original);
            String zdecompressed = decompressString(zcompressed);
            System.out.println("Original String: "+original);
            System.out.println("Compressed String: "+zcompressed);
            System.out.println("Decompressed String: "+zdecompressed);
        } catch (IOException e) {
            e.printStackTrace();
        }


    public static String compressString(String uncompressedString){
        String compressedString = null;
        byte[] bytes = Base64.decode(uncompressedString);
        try {
            bytes = compressBytes(bytes);
            compressedString = Base64.encode(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return compressedString;
    }

    public static String decompressString(String compressedString){
        String decompressedString = null;
        byte[] bytes = Base64.decode(compressedString);
        try {
            bytes = decompressBytes(bytes);
            decompressedString = Base64.encode(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DataFormatException e) {
            e.printStackTrace();
        }
        return decompressedString;
    }

    public static byte[] compressBytes(byte[] data) throws IOException {
        Deflater deflater = new Deflater();
        deflater.setInput(data);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
        deflater.finish();
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer); // returns the generated code... index
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
        byte[] output = outputStream.toByteArray();
        return output;
    }

    public static byte[] decompressBytes(byte[] data) throws IOException, DataFormatException {
        Inflater inflater = new Inflater();
        inflater.setInput(data);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
        byte[] output = outputStream.toByteArray();
        return output;
    }
}

This is giving the result :

    Starting Zlib
==================================
Original String: I am the god
Compressed String: eJxTXLm29YUGAApUAw0=
Decompressed String: Iamthego

As you can see, it is missing the white-spaces and it even lost the final letter in the given String.

Can someone please suggest what is wrong with this code. I'm following below steps:

  1. Decode
  2. compress
  3. encode
  4. save
  5. retrieve
  6. decode
  7. decompress
  8. encode.

Please help. Thank you.


Solution

  • In compressString, replace:

    Base64.decode(uncompressedString)
    

    with

    uncompressString.getBytes(StandardCharsets.UTF_8)
    

    You're not passing in a base64-encoded string; you simply want the bytes of the input string. Note that spaces never appear in base64 encoding, so they are likely treated as redundant and discarded.

    Similarly in decompressString, replace:

    Base64.encode(bytes)
    

    with

    new String(bytes, StandardCharsets.UTF_8)