欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php怎么压缩一个文件夹里面所有的文件到zip文件里面

程序员文章站 2022-05-20 14:58:14
...
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  readfileheader($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($to . $header['filename'], 'wb');
                IF(!$fp) Return(-1);
                $size = $header['compressed_size'];

                While($size != 0){
                    $read_size = ($size