Decompressing a .gz file via PHP

前端 未结 4 503
夕颜
夕颜 2021-01-14 09:42

I need to be able to decompress through PHP some data that I have in a string which uses the gzip format. I need to do this via PHP, not by calling - through

4条回答
  •  感动是毒
    2021-01-14 10:06

    Well I found my answer by reading the comments on the gzdecode page I linked in my original post. One of the users, Aaron G, provided an implementation of it and it works:

     0) {
        switch ($method) {
          case 8:
            // Currently the only supported compression method:
            $data = gzinflate($body);
            break;
          default:
            // Unknown compression method
            return false;
        }
      } else {
        // I'm not sure if zero-byte body content is allowed.
        // Allow it for now...  Do nothing...
      }
    
      // Verifiy decompressed size and CRC32:
      // NOTE: This may fail with large data sizes depending on how
      //       PHP's integer limitations affect strlen() since $isize
      //       may be negative for large sizes.
      if ($isize != strlen($data) || crc32($data) != $datacrc) {
        // Bad format!  Length or CRC doesn't match!
        return false;
      }
      return $data;
    }
    
    ?>
    

提交回复
热议问题