问题描述
如何解压缩由php gzcompress()函数缩放的字符串?
有任何完整示例?
thx
我现在像这样尝试了:
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.请参阅 example .com/Question/3627401/gzipinputStream-to-string/3627442#3627442">这个问题.
其他推荐答案
见
util/zip/offaterinputstream.html
由于Deflate算法为GZIP.
问题描述
How can i decompress a String that was zipped by PHP gzcompress() function?
Any full examples?
thx
I tried it now like this:
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; }
but it's not working if i i'm trying to unzip a PHP gzcompress (-ed) string.
推荐答案
PHP's gzcompress uses 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()); }
其他推荐答案
Try a GZIPInputStream. See this example and this SO question.
其他推荐答案
See
http://developer.android.com/reference/java/util/zip/InflaterInputStream.html
since the DEFLATE algorithm is gzip.
相关问答
相关标签/搜索