Home >Backend Development >PHP Tutorial >Code for php zlib compression and decompression of swf files_PHP tutorial
It's different when using PHP. PHP contains the zlib link library, and you can directly use its related functions. The following is an example of compressing and uncompressing SWF files I wrote:
//There is no addition to determine whether the SWF file has been compressed. The input needs can be judged according to whether the first byte of the file is 'F' or 'C'
Compressed swf file:
//----------------- -------------------------------------------------- -------------------------------
//File name
$filename = "test.swf";
//Open the file
$rs = fopen($filename,"r");
//Read the data of the file
$str = fread($rs,filesize($filename)) ;
//Set the swf header file
$head = substr($str,1,8);
$head = "C".$head;
//Get the swf file content
$body = substr($str,8);
//Compress the file content, using the highest compression level 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 the file
fwrite($ws,$str);
//Close the file and leave it behind
fclose($ws);
//------------------------------------------------ -------------------------------------------------- --
?>
Extract the swf file:
//----------------------------- -------------------------------------------------- ---------------------
//File name
$filename = "test.swf";
//Open file
$rs = fopen($filename,"r");
//Read file data
$str = fread($rs,filesize($filename));
//Set swf header file
$head = substr($str,1,8);
$head = "F".$head;
//Get the swf file content
$body = substr($str,8 );
//Decompress file content
$body = gzuncompress($body);
//Merge file header and content
$str = $head.$body;
// Close the read file stream
fclose($rs);
//Create a new file
$ws = fopen("create.swf","w");
//Write File
fwrite($ws,$str);
//Close the file to stay
fclose($ws);
//--------------- -------------------------------------------------- -----------------------------------
?> How about
? Isn’t it great? Simple? Haha, php gives us more than just simplicity"`