Php examples of extracting gzip files or strings

Source: Internet
Author: User

In fact, it is very easy for php to decompress gzip. You can use the built-in gzdecode function, but it is a pity that I have been configured for half a day and cannot support the gzdecode function, so I have to work around it:
Copy codeThe Code is as follows:
If (! Function_exists ('gzdemo ')){
Function gzdecode ($ data ){
$ Flags = ord (substr ($ data, 3, 1 ));
$ Headerlen = 10;
$ Extralen = 0;
$ Filenamelen = 0;
If ($ flags & 4 ){
$ Extralen = unpack ('V', substr ($ data, 10, 2 ));
$ Extralen = $ extralen [1];
$ Headerlen + = 2 + $ extralen;
}
If ($ flags & 8) // Filename
$ Headerlen = strpos ($ data, chr (0), $ headerlen) + 1;
If ($ flags & 16) // Comment
$ Headerlen = strpos ($ data, chr (0), $ headerlen) + 1;
If ($ flags & 2) // CRC at end of file
$ Headerlen + = 2;
$ Unpacked = @ gzinflate (substr ($ data, $ headerlen ));
If ($ unpacked = FALSE)
$ Unpacked = $ data;
Return $ unpacked;
}
}

The call method is simple:
Copy codeThe Code is as follows:
$ F = @ file_get_contents ("http://www.jb51.net ");
Echo gzdecode ($ f );

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.