Android:解压缩用PHP压缩的字符串gzcompress()
如何解压缩由PHP gzcompress()函数压缩的字符串?
有完整的例子吗?
谢谢
我现在尝试了这样:
public static String unzipString(String zippedText) throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream(zippedText.getBytes("UTF-8")); GZIPInputStream gzis = new GZIPInputStream(bais); InputStreamReader reader = new InputStreamReader(gzis); BufferedReader in = new BufferedReader(reader); String unzipped = ""; while ((unzipped = in.readLine()) != null) unzipped+=unzipped; return unzipped; }
但是如果我正在尝试解压缩PHP gzcompress(-ed)字符串,它就无法正常工作。
PHP的gzcompress使用Zlib NOT GZIP
public static String unzipString(String zippedText) { String unzipped = null; try { byte[] zbytes = zippedText.getBytes("ISO-8859-1"); // Add extra byte to array when Inflater is set to true byte[] input = new byte[zbytes.length + 1]; System.arraycopy(zbytes, 0, input, 0, zbytes.length); input[zbytes.length] = 0; ByteArrayInputStream bin = new ByteArrayInputStream(input); InflaterInputStream in = new InflaterInputStream(bin); ByteArrayOutputStream bout = new ByteArrayOutputStream(512); int b; while ((b = in.read()) != -1) { bout.write(b); } bout.close(); unzipped = bout.toString(); } catch (IOException io) { printIoError(io); } return unzipped; } private static void printIoError(IOException io) { System.out.println("IO Exception: " + io.getMessage()); }
试试GZIPInputStream。 看到这个例子和这个问题 。
- java.lang.ArrayIndexOutOfBoundsException:length = 0; index = 0 – 数据库阅读 – Android
- Android改造设计模式
- Android + Java从服务器上的URL获取图像并作为字符串传递给客户端:Base64解码和编码无法正常工作
- 解密文件的内容时返回不同的字节
- 如何动态添加图像到GridView?
- 如何在android中使用正则表达式搜索sqlite数据库?
- Android如何知道应用程序已启动并根据开始时间确定应用程序的优先级
- 更改活动会阻止ImageButton setImageResource工作
- 如何知道我是否已成功连接到Android中的外发号码?