1

Desired:
Create compress and uncompress a string or byte array. After that I plan to Base64 encode it for writing to a log file.

Unfortunately, it looks like it is not recognizing it as being compressed or something. I'm assuming that it is missing some metadata bytes, but couldn't find what i needed.

Output: enter image description here

Test Class:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.codec.binary.Base64;

public class TestEncodeDecode {
    private static final String UTF_8 = "UTF-8";

    public static void main(String[] args) throws IOException {
        String originalString = "     This online sample demonstrates functionality of a base64 property, ByteArray class and Huge asp file upload.\n      The sample uses a special Base64 algorithm written for the ByteArray class.";

        System.out.println(originalString);
        System.out.println("------------------------------------------------------");
        String compressedString = compressString(originalString);
        System.out.println(compressedString);
        System.out.println("------------------------------------------------------");
        String uncompressedString = uncompressString(compressedString);
        System.out.println(uncompressedString);
        Validate.isTrue(originalString.equals(uncompressedString));
    }

    public static String compressString(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        byte[] bytes = str.getBytes(UTF_8);
        ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length);
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(bytes);
        gzip.flush();
        gzip.close(); //Finalizes the ByteArrayOutputStream's value

        String compressedString = out.toString(UTF_8);
        return compressedString;
    }

    public static String uncompressString(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes(UTF_8));
        GZIPInputStream gzip = new GZIPInputStream(in);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] b = new byte[1000];
        int len;
        while ((len = in.read(b)) > 0) {
            out.write(b, 0, len);
        }
        gzip.close();
        out.close();

        String uncompressedString = out.toString(UTF_8);
        return uncompressedString;
    }
}
ScrappyDev
  • 2,307
  • 8
  • 40
  • 60
  • 3
    First, gzipped data is not necessarily UTF-8 encoded text. Don't work with `String` as the intermediary, work with `byte[]`. Second, you're reading from `in`, instead of reading from `gzip` in your `uncompressString` method. – Sotirios Delimanolis Aug 18 '16 at 02:34
  • Possible duplicate of [How can I Zip and Unzip a string using GZIPOutputStream that is compatible with .Net?](http://stackoverflow.com/questions/6717165/how-can-i-zip-and-unzip-a-string-using-gzipoutputstream-that-is-compatible-with) – SkyWalker Aug 18 '16 at 02:37
  • Thanks Sotirios, using the right stream and using byte[] fixed it. – ScrappyDev Aug 18 '16 at 02:47

0 Answers0