0

I try to compressed large string in android Like this:

try {
    String str = "MyLarge String";
    ByteArrayOutputStream rstBao = new ByteArrayOutputStream(str.length());
    GZIPOutputStream zos = new GZIPOutputStream(rstBao);
    zos.write(str.getBytes());
    IOUtils.closeQuietly(zos);

    byte[] bytes = rstBao.toByteArray();

    String compressedString = Base64.encodeToString(bytes, true);
    Log.i("Compressed", compressedString);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

but this code return outofmemory error:

outofmemory return in Base64.encodeToString(bytes, true); line

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 1
    Did you try literally with `"MyLarge String"` or with a really large string? – Henry Aug 16 '14 at 07:09
  • I fail to see how this question is a duplicate of http://stackoverflow.com/questions/6717165/how-can-i-zip-and-unzip-a-string-using-gzipoutputstream-that-is-compatible-with – Henry Aug 17 '14 at 17:58

1 Answers1

0

try this:

public static String compressString(String str) throws IOException {
    if (str == null || str.length() == 0) {
        return str;
    }

    BufferedWriter writer = null;

    try {
        File file = new File("your.gzip")
        GZIPOutputStream zip = new GZIPOutputStream(new FileOutputStream(file));

        writer = new BufferedWriter(new OutputStreamWriter(zip, "UTF-8"));

        writer.append(str);
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}
Community
  • 1
  • 1
Elango
  • 412
  • 4
  • 24