How does php compress all files in a folder into a zip file?

Source: Internet
Author: User
How does php compress all files in a folder into a zip file? How does php compress all files in a folder into a zip file? For example, my localhosthas a testfolder with many files and folders in it. how can I pack all test.zip files into test.zip ?, How does PHPcodeFunction php compress all files in a folder into a zip file?
How does php compress all files in a folder into a zip file?

For example, my localhost has a test folder with many files and folders in it. how can I package everything in test into test.zip?

------ Solution --------------------
PHP code
Function ReadFileHeader ($ zip) {$ binary_data = fread ($ zip, 30 ); $ data = unpack ('vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len ', $ binary_data ); $ header ['filename'] = fread ($ zip, $ data ['filename _ Len']); IF ($ data ['extra _ Len']! = 0) {$ header ['Extra '] = fread ($ zip, $ data ['extra _ Len']);} else {$ header ['Extra '] = '';} $ header ['compression'] = $ data ['compression']; $ header ['size'] = $ data ['size']; $ header ['compressed _ size'] = $ data ['compressed _ size']; $ header ['crc '] = $ data ['crc']; $ header ['flag'] = $ data ['flag']; $ header ['mdate'] = $ data ['mdate']; $ header ['mtime'] = $ data ['mtime']; IF ($ header ['mdate'] & $ header ['mtime']) {$ hou R = ($ header ['mtime'] & 0xF800)> 11; $ minute = ($ header ['mtime'] & 0x07E0)> 5; $ seconde = ($ header ['mtime'] & 0x001F) * 2; $ year = ($ header ['mdate'] & 0xFE00)> 9) + 1980; $ month = ($ header ['mdate'] & 0x01E0)> 5; $ day = $ header ['mdate'] & 0x001F; $ header ['mtime'] = mktime ($ hour, $ minute, $ seconde, $ month, $ day, $ year );} else {$ header ['mtime'] = time ();} $ header ['stored _ filename'] = $ header ['filename' ]; $ Header ['status'] = "OK"; Return $ header;} Function ReadCentralFileHeaders ($ zip) {$ binary_data = fread ($ zip, 46 ); $ header = unpack ('vchkid/vid/vversion/kernel/vflag/vcompression/vmtime/vmdate/Vcrc/kernel/Vsize/kernel/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/ voffset ', $ binary_data); IF ($ header ['filename _ Len']! = 0) $ header ['filename'] = fread ($ zip, $ header ['filename _ Len']); Else $ header ['filename'] = ''; IF ($ header ['extra _ Len']! = 0) $ header ['Extra '] = fread ($ zip, $ header ['extra _ Len']); Else $ header ['Extra'] = ''; IF ($ header ['Comment _ Len']! = 0) $ header ['comment'] = fread ($ zip, $ header ['Comment _ Len']); Else $ header ['comment'] = ''; IF ($ header ['mdate'] & $ header ['mtime']) {$ hour = ($ header ['mtime'] & 0xF800)> 11; $ minute = ($ header ['mtime'] & 0x07E0)> 5; $ seconde = ($ header ['mtime'] & 0x001F) * 2; $ year = ($ header ['mdate'] & 0xFE00)> 9) + 1980; $ month = ($ header ['mdate'] & 0x01E0)> 5; $ day = $ header ['mdate'] & 0x001F; $ header ['mtim E '] = mktime ($ hour, $ minute, $ seconde, $ month, $ day, $ year );} else {$ header ['mtime'] = time ();} $ header ['stored _ filename'] = $ header ['filename']; $ header ['status'] = 'OK'; IF (substr ($ header ['filename'],-1) = '/') $ header ['external '] = 0x41FF0010; Return $ header;} Function ReadCentralDir ($ zip, $ zip_name) {$ size = filesize ($ zip_name ); IF ($ size <277) {$ maximum_size = $ size;} Else {$ maximum_size = 2 77 ;}@ fseek ($ zip, $ size-$ maximum_size); $ pos = ftell ($ zip); $ bytes = 0x00000000; While ($ pos <$ size) {$ byte = @ fread ($ zip, 1); $ bytes = ($ bytes <8) | Ord ($ byte); IF ($ bytes = 0x504b0506) {$ pos ++; break;} $ pos ++;} $ data = unpack ('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size ', fread ($ zip, 18); IF ($ data ['Comment _ size']! = 0) {$ centd ['comment'] = fread ($ zip, $ data ['Comment _ size']);} else {$ centd ['comment'] = '';} $ centd ['entries'] = $ data ['entries']; $ centd ['disk _ entries'] = $ data ['disk _ entries']; $ centd ['offset'] = $ data ['offset']; $ centd ['disk _ start'] = $ data ['disk _ start']; $ centd ['size'] = $ data ['size']; $ centd ['disk'] = $ data ['disk']; Return $ centd;} Function ExtractFile ($ header, $ to, $ zip) {$ header = $ this-> re Adfileheader ($ zip); IF (substr ($ to,-1 )! = "/") $ To. = "/"; IF (! @ Is_dir ($ to) @ mkdir ($ to, 0777); $ pth = explode ("/", dirname ($ header ['filename']); for ($ I = 0; isset ($ pth [$ I]); $ I ++) {IF (! $ Pth [$ I]) continue; $ pthss. = $ pth [$ I]. "/"; IF (! Is_dir ($ to. $ pthss) @ mkdir ($ to. $ pthss, 0777);} IF (! ($ Header ['external '] = 0x41FF0010 )&&! ($ Header ['external '] = 16) {IF ($ header ['compression'] = 0) {$ fp = @ fopen ($. $ header ['filename'], 'WB '); IF (! $ Fp) Return (-1); $ size = $ header ['compressed _ size']; While ($ size! = 0) {$ read_size = ($ size <2048? $ Size: 2048); $ buffer = fread ($ zip, $ read_size); $ binary_data = pack ('A '. $ read_size, $ buffer); @ fwrite ($ fp, $ binary_data, $ read_size); $ size-= $ read_size;} fclose ($ fp); touch ($. $ header ['filename'], $ header ['mtime']);} Else {$ fp = @ fopen ($. $ header ['filename']. '.gz ', 'WB'); IF (! $ Fp) Return (-1); $ binary_data = pack ('va1a1va1a1 ', 0x8b1f, Chr ($ header ['compression']), Chr (0x00 ), time (), Chr (0x00), Chr (3); fwrite ($ fp, $ binary_data, 10 ); $ size = $ header ['compressed _ size']; While ($ size! = 0) {$ read_size = ($ size <1024? $ Size: 1024); $ buffer = fread ($ zip, $ read_size); $ binary_data = pack ('A '. $ read_size, $ buffer); @ fwrite ($ fp, $ binary_data, $ read_size); $ size-= $ read_size;} $ binary_data = pack ('vv ', $ header ['crc '], $ header ['size']); fwrite ($ fp, $ binary_data, 8); fclose ($ fp ); $ gzp = @ gzopen ($. $ header ['filename']. '.gz ', 'RB') or die ("Cette archive est compress finished"); IF (! $ Gzp) Return (-2); $ fp = @ fopen ($ to. $ header ['filename'], 'wb'); IF (! $ Fp) Return (-1); $ size = $ header ['size']; While ($ size! = 0) {$ read_size = ($ size <2048? $ Size: 2048); $ buffer = gzread ($ gzp, $ read_size); $ binary_data = pack ('A '. $ read_size, $ buffer); @ fwrite ($ fp, $ binary_data, $ read_size); $ size-= $ read_size;} fclose ($ fp); gzclose ($ gzp ); touch ($. $ header ['filename'], $ header ['mtime']); @ unlink ($. $ header ['filename']. '.gz ') ;}} Return true ;}}

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.