Using PHP's zlib compression and decompression SWF file

Source: Internet
Author: User
Tags fread
I've written about how to use C # to compress and decompress a SWF file. Decompression, compression and read Flash header file information need to use an open Source link library, and not very convenient to use, but use PHP is not the same, PHP contains the zlib link library, you can directly use its related functions, Here are my examples of compressed and knot-compressed SWF files:

Not joined to determine whether the SWF file has been compressed, in need can be based on the first byte of the file is ' F ' or ' C ' to determine


To compress a SWF file:

<?php
//--------------------------------------------------------------------------------------------------
Filename
$filename = "test.swf";
Open File
$rs = fopen ($filename, "R");
Reading data from a file
$str = Fread ($rs, FileSize ($filename));
Set SWF header File
$head = substr ($str, 1,8);
$head = "C". $head;
Get SWF file contents
$body = substr ($STR, 8);
Compress file contents using the highest compression level 9
$body = Gzcompress ($body, 9);
Merging file headers and content
$str = $head. $body;
Turn off the Read file stream
Fclose ($RS);
Create a new file
$ws = fopen ("create.swf", "w");
Write a file
Fwrite ($ws, $STR);
Close file Stay
Fclose ($WS);
//----------------------------------------------------------------------------------------------------
?>
To extract a SWF file:

<?php
//----------------------------------------------------------------------------------------------------
Filename
$filename = "test.swf";
Open File
$rs = fopen ($filename, "R");
Reading data from a file
$str = Fread ($rs, FileSize ($filename));
Set SWF header File
$head = substr ($str, 1,8);
$head = "F". $head;
Get SWF file contents
$body = substr ($STR, 8);
Extract the contents of a file
$body = gzuncompress ($body);
Merging file headers and content
$str = $head. $body;
Turn off the Read file stream
Fclose ($RS);
Create a new file
$ws = fopen ("create.swf", "w");
Write a file
Fwrite ($ws, $STR);
Close file Stay
Fclose ($WS);
//----------------------------------------------------------------------------------------------------
?>

Is it easy? hehe, PHP gives us more than just simple ...

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.