【问题标题】:How to compressed Large String with gzip in android [duplicate]如何在android中使用gzip压缩大字符串[重复]
【发布时间】:2014-08-16 06:14:06
【问题描述】:

我尝试在 android 中像这样压缩大字符串:

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();
}

但此代码返回内存不足错误:

Base64.encodeToString(bytes, true); 行中的内存不足返回

【问题讨论】:

标签: android gzipstream


【解决方案1】:

试试这个:

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();
        }
    }
}

【讨论】:

  • 我尝试了,但无法解决我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-20
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
  • 2010-11-24
  • 1970-01-01
相关资源
最近更新 更多