如何压缩/解压缩从php到android的长字符串?

时间:2014-11-09 06:33:09

标签: php android compression

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

由于

2 个答案:

答案 0 :(得分:2)

在我的情况下,我需要在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";

由于

答案 1 :(得分:0)

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

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

如果这不是正在使用的压缩算法,那么可以根据需要使用一些内置算法:

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