在我的android應用程序中,它從php下載了一個巨大的字符串。我想壓縮它以節省帶寬。我發現這個頁面http://php.net/manual/en/function.gzcompress.php,它允許我在PHP中壓縮它。但是一旦我在android中使用它,我怎樣才能找回原始字符串?如何壓縮/解壓縮長字符串從PHP到Android?
謝謝
在我的android應用程序中,它從php下載了一個巨大的字符串。我想壓縮它以節省帶寬。我發現這個頁面http://php.net/manual/en/function.gzcompress.php,它允許我在PHP中壓縮它。但是一旦我在android中使用它,我怎樣才能找回原始字符串?如何壓縮/解壓縮長字符串從PHP到Android?
謝謝
看起來它只是使用gzip壓縮。如果是這樣的話,就來看看:
http://developer.android.com/reference/java/util/zip/GZIPInputStream.html
如果這是不完全所使用的壓縮算法,有幾個內置的算法,可以根據需要使用:
http://developer.android.com/reference/java/util/zip/package-summary.html
在我的情況下,我需要解壓縮在Android中壓縮的PHP字符串,反之亦然,這些字符串由RabbitMQ傳輸。
這是我如何解決它: 在Android中:從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();
}
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";
感謝
看到http://stackoverflow.com/questions/11402813/how-to-compress-a-jsonobject-send-it-over-http-in-android和http:// stackoverflow.com/questions/7153484/gzip-post-request-with-httpclient-in-java – 2014-11-09 06:36:29