如何用java读取php的gzencode函数输出的压缩字符
程序员文章站
2024-04-04 12:35:05
...
phpjavagzdecode
输入的字符串为php的gzencode压缩的字符串,可以用gzdecode解开,但是用java的java.util.zip.GZIPInputStream读入字符串时,总是提示:
java.util.zip.ZipException: Not in GZIP format
希望有经验的朋友帮我解答一下,我的代码如下:
JAVAimport java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.util.zip.GZIPInputStream;import java.util.zip.GZIPOutputStream;public class Gzdecode { public static void decompress(InputStream is, OutputStream os) throws Exception { GZIPInputStream gis = new GZIPInputStream(is); int count; byte data[] = new byte[BUFFER]; while ((count = gis.read(data, 0, BUFFER)) != -1) { os.write(data, 0, count); } gis.close(); } public static byte[] decompress(byte[] data) throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream(data); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 解压缩 decompress(bais, baos); data = baos.toByteArray(); baos.flush(); baos.close(); bais.close(); return data; } public static void main(String[] args) throws Exception { String input=""; byte[] data=input.getBytes("UTF-8"); byte[] data_decompress=decompress(data); System.out.write(data_decompress);