【问题标题】:How to compress/decompress long string from php to android?如何将长字符串从 php 压缩/解压缩到 android?
【发布时间】:2015-01-05 16:29:26
【问题描述】:

在我的 android 应用程序中,它从 php.ini 下载了一个巨大的字符串。我想压缩它以节省带宽。我找到了这个页面http://php.net/manual/en/function.gzcompress.php,它允许我在 php 中压缩它。但是一旦我在android中拥有它,我怎样才能取回原始字符串?

谢谢

【问题讨论】:

标签: php android compression


【解决方案1】:

在我的情况下,我需要在 PHP 中解压缩在 Android 中压缩的字符串,反之亦然,这些字符串由 RabbitMQ 传输。

我是这样解决的: 在 Android 中:

public static byte[] compress(String string) throws IOException {
    // string = "Lorem ipsum shizzle ma nizle";
    byte[] data = string.getBytes("UTF-8");
    ByteArrayOutputStream os = new ByteArrayOutputStream( data.length );
    GZIPOutputStream gos = new GZIPOutputStream(os);
    gos.write( data );
    gos.close();
    os.close();
    return os.toByteArray();
}

public static String decompress(byte[] compressed) throws IOException {
    final int BUFFER_SIZE = compressed.length;
    ByteArrayInputStream is = new ByteArrayInputStream(compressed);
    GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE);
    StringBuilder string = new StringBuilder();
    byte[] data = new byte[BUFFER_SIZE];
    int bytesRead;
    while ((bytesRead = gis.read(data)) != -1) {
        string.append(new String(data, 0, bytesRead));
    }
    gis.close();
    is.close();
    return string.toString();
}

来自 Android 的 RabbitMQ 调用:

.
.
// Publish
chan.basicPublish(EXCHANGE, routing_key, MessageProperties.PERSISTENT_BASIC, compress(message.payload));
.
.
// Subscribe
String message = decompress(body);
.
.
.

从 RabbitMQ 管理插件中,我检索了拉丁文字符串的 Base64 编码版本,并针对这个 PHP 测试应用程序运行它以查看哪种解压缩方法有效。

<?php

$data = "Lorem ipsum shizzle ma nizle";
// try all the compression algorithms and out the results in Hex format
echo "\n\n\n";
for($i=-1;$i<=9;$i++)
    echo chunk_split(strtoupper(bin2hex(gzcompress($data,$i))),2," ") . PHP_EOL  ;
echo "\n\n";
for($i=-1;$i<=9;$i++)
    echo chunk_split(strtoupper(bin2hex(gzdeflate($data,$i))),2," ") . PHP_EOL  ;
echo "\n\n";
for($i=-1;$i<=9;$i++)
    echo chunk_split(strtoupper(bin2hex(gzencode($data,$i,FORCE_GZIP))),2," ")  . PHP_EOL;
echo "\n\n";
for($i=-1;$i<=9;$i++)
    echo chunk_split(strtoupper(bin2hex(gzencode($data,$i,FORCE_DEFLATE))),2," ") . PHP_EOL;
echo "\n\n";


// add the Base64 encoded text from the RabbitMQ plugin
$data = "H4sIAAAAAAAAAPPJL0rNVcgsKC7NVSjOyKyqyklVyE1UyMsEMgCX3v4lHAAAAA==";
$data = base64_decode($data);
// output the binary version of the compressed string in Hex format
echo chunk_split(strtoupper(bin2hex($data)),2," ") . PHP_EOL  ;
// match the HEX string to the samples from above as there may be an offset that you need to deal with

// finally see which of the decompression methods work
echo gzuncompress($data)."\n";
echo gzinflate($data)."\n";
echo gzdecode($data)."\n";

// Double check that you have the correct one
$data = "Lorem ipsum shizzle ma nizle";
echo chunk_split(strtoupper(bin2hex(gzencode($data,9,FORCE_GZIP))),2," ")  . PHP_EOL;
$data = gzencode($data,9,FORCE_GZIP);
echo gzdecode($data)."\n";

谢谢

【讨论】:

    【解决方案2】:

    看起来它只是使用 gzip 压缩。如果是这种情况,请查看:

    http://developer.android.com/reference/java/util/zip/GZIPInputStream.html

    如果这不是所使用的压缩算法,您可以根据需要使用更多内置算法:

    http://developer.android.com/reference/java/util/zip/package-summary.html

    【讨论】:

      猜你喜欢
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 2015-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多