How to use java to read compressed characters output by php's gzencode function

Source: Internet
Author: User
The input string is the gzencodecompressed string of PHP. you can use gzdecodeto open it, but use java.util.zip of Java. when GZIPInputStream reads a string, it always prompts java.util.zip. zipException: NotinGZIPformat. I hope some experienced users can help me solve the problem. my code is as follows: & quot; JAVAimportjava. io. byteArrayInputStream; importjava. io. byteArrayOutputStream; imp... phpjavagzdecode

The input string is the gzencodecompressed string of PHP. you can use gzdecodeto extract the string. However, when you use java.util.zip. GZIPInputStream of Java to read the string, the following message is always displayed:
Java.util.zip. ZipException: Not in GZIP format
I hope some experienced friends can help me solve the problem. my code is as follows:

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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.