Php uses zlib to compress and decompress files (swf)

Source: Internet
Author: User
Tags fread ini

Before we use the zlip function, we need to perform the following operations, otherwise we will not be able to read it down.

First set in PHP. ini

The code is as follows: Copy code

Zlib. output_compression = On
Zlib. output_compression_level = 6

The first option is enable compression, and the second option is compression ratio. The optional range is 1-9;

Then enable deflate compression in apach to remove the well number.

The code is as follows: Copy code

# LoadModule deflate_module modules/mod_deflate.so

So far, this still does not work. You also need to select a blank area in http. conf and add the output filter of the file type to select which suffixes to press.

.

The code is as follows: Copy code

AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php

AddOutputFilter DEFLATE css js txt php xml html htm

If you do not have the permission to modify the php. Ini file, you can use the phpr ini_set function to perform operations.
For example

 

The code is as follows: Copy code

<? Php
Ini_set ("zlib. output_compression", "On ");
?>


<? Php
Ini_set ("zlib. output_compression", 4096 );
?>


Okay, now everything is ready for us to import the final file.

Compress the swf file:

The code is as follows: Copy code

<? Php

// File name
$ Filename = "test.swf ";
// Open the file
$ Rs = fopen ($ filename, "r ");
// Read the file data
$ Str = fread ($ rs, filesize ($ filename ));
// Set the swf header file
$ Head = substr ($ str, 1, 8 );
$ Head = "C". $ head;
// Obtain the swf file content
$ Body = substr ($ str, 8 );
// Compress the file content with a maximum compression level of 9
$ Body = gzcompress ($ body, 9 );
// Merge the file header and content
$ Str = $ head. $ body;
// Close the read File Stream
Fclose ($ rs );
// Create a new file
$ Ws = fopen ("create.swf", "w ");
// Write an object
Fwrite ($ ws, $ str );
// Close File retention
Fclose ($ ws );

?>

Decompress the flash swf file
 
// File name
$ Filename = "test.swf ";
// Open the file
$ Rs = fopen ($ filename, "r ");
// Read the file data
$ Str = fread ($ rs, filesize ($ filename ));
// Set the swf header file
$ Head = substr ($ str, 1, 8 );
$ Head = "F". $ head;
// Obtain the swf file content
$ Body = substr ($ str, 8 );
// Decompress the file content
$ Body = gzuncompress ($ body );
// Merge the file header and content
$ Str = $ head. $ body;
// Close the read File Stream
Fclose ($ rs );
// Create a new file
$ Ws = fopen ("create.swf", "w ");
// Write an object
Fwrite ($ ws, $ str );
// Close File retention
Fclose ($ ws );
?>

Note: The gzip data header is larger than the zlib data header because it stores file names and other file system information. In fact, this is a widely used gzip file

Data header format. Note that the zlib library itself cannot create a gzip file, but it is quite easy to write compressed data to a gzip file.

.

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.