日期:2014-05-17  浏览次数:20748 次

php如何压缩一个文件夹里面所有的文件到zip文件里面?
php如何压缩一个文件夹里面所有的文件到zip文件里面?

如我的localhost下有个test文件夹,内部有很多文件和文件夹,如何把test里面所有东西打包成 test.zip?

------解决方案--------------------
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']){
            $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['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/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/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['mtime'] = 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 = 277;}

        @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['ent