zip or rar 做了一个需求就是列出zip和rar的内容并入库,朋友是dedecms的系统,需要的是ajax前台显示的,匆忙之间就写了这么一个类!里边有着详细的说明,好了现在代码放出来值得一提的是需要安装个组件http://pecl.php.net/package/rar这里下载,然后也有说明文档好了不多说了,上代码了!

<?php
 /*
*    Author: widuu
*
*    Email:admin@widuu.com
*    
*    时间:2013-08
*
*    说明:列出压缩包中的内容(需要安装RarArchive下载地址http://pecl.php.net/package/rar)
*
*/
define(PATH,"D:/wwwroot/thinksns/");
require 'mfdb.class.php';
$mfdb = new mfdb();
$mfdb->connect("localhost","root","dgj99349","test");
class zip
{

    /**
     *  获取zip文件中的文件列表
     *
     * @access    public
     * @param     string  $zip_name  zip文件名
     * @param     string  $id  zip对应的id
     * @status    is ok 插入成功 insert false 插入失败
     */
    function get_zip_List($zip_name,$id)
    {
        $ret = '';
        $zip = @fopen(PATH.$zip_name, 'rb');
        if(!$zip)
        {
            return(0);
        }
        $centd = $this->ReadCentralDir($zip,PATH.$zip_name);
        @rewind($zip);
        @fseek($zip, $centd['offset']);
        for ($i=0; $i<$centd['entries']; $i++)
        {
            $header = $this->ReadCentralFileHeaders($zip);
            $header['index'] = $i;$info['filename'] = $header['filename'];
            $info['status'] = $header['status'];
            $ret[]=$info; 
            unset($header);
        }
        $file = array();
        foreach ($ret as $key => $value) {
            if($value['status'] == "ok"){
                $file_rel = pathinfo($value['filename']);
                if(is_file($file_rel['basename']))
                {
                    $file[] = $file_rel['basename']; 
                }
               
            }else{
                $this->exit("zip error");
            }
        }
        $file = $this->array2string($file);
        global $mfdb;
        $result = $mfdb->query("insert into _mf_zip (`fid`,`filepath`) value ('{$id}','{$file}')");
         if($result)
         {
            echo "is ok";
         }else{
            echo "insert false";
         }
    }

     /**
     *  获取rar文件中的文件列表
     *
     * @access    public
     * @param     string  $zipname  zip文件名
     * @param     string  $id  zip对应的id
     * @status    is ok 插入成功 insert false 插入失败
     */

     function get_rar_list($zipname,$id){
        $rar_file  =  rar_open (PATH.$zipname) or die( "Can't open Rar archive" ); 
        $entries  =  rar_list ( $rar_file ); 
        $file = array();
        foreach ( $entries  as  $entry ) { 
            $file_rel = pathinfo($entry -> getName ());
            if(is_file($file_rel['basename'])){
                  $file[] = $file_rel['basename']; 
            }
          

            $entry -> extract ( '/dir/extract/to/' ); 
        } 
        rar_close ( $rar_file ); 
        $file = $this -> array2string($file);
        global $mfdb;
        $result = $mfdb -> query("insert into _mf_zip (`fid`,`filepath`) value ('{$id}','{$file}')");
        if($result)
        {
            echo "is ok";
        }else{
            echo "insert false";
        }
     }

	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['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 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;
    }


	 /**
      * 判断并插入压缩包内数据
      *
      * @param    $zipname  字符串      压缩包文件名称
      * @param    $id       字符串      填写的压缩包对应的id
      * @return   返回状态 id exists    数据库已存在  is not zip or rar 不是zip活着rar文件
      */
	function judge($zipname,$id){
        global $mfdb;
        $data = $mfdb->get_one("select * from _mf_zip where fid = $id");
        if ($data !=Null) 
        {
              exit("id exists");
        }
		$suffix = substr($zipname,-4);
		if($suffix ==".rar")
		{
			$this->get_rar_List($zipname,$id);
		}else if($suffix == ".zip"){
			$this->get_zip_List($zipname,$id);
		}else{
		      exit('is not zip or rar');
		}
	}
	
     /**
      * 将数组转换为字符串
      *
      * @param    array   $data       数组
      * @param    bool    $isformdata 如果为0,则不使用new_stripslashes处理,可选参数,默认为1
      * @return   string  返回字符串,如果,data为空,则返回空
      */
    function array2string($data, $isformdata = 1) {
        if($data == '') return '';
        if($isformdata) $data = $this->new_stripslashes($data);
        return addslashes(var_export($data, TRUE));
    }
    
    /**
     * 返回经stripslashes处理过的字符串或数组
     * @param $string 需要处理的字符串或数组
     * @return mixed
     */
    function new_stripslashes($string) {
        if(!is_array($string)) return stripslashes($string);
        foreach($string as $key => $val) $string[$key] =$this-> new_stripslashes($val);
        return $string;
    }
   
}

//举例  附赠插入数据库
/*
 *DROP TABLE IF EXISTS `_mf_zip`;
 *CREATE TABLE `_mf_zip` (
 *`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 *`fid` int(10) unsigned NOT NULL,
 *`filepath` text NOT NULL,
 *PRIMARY KEY (`id`)
 *) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 */
$zip = new zip();
$zipname = $zip->judge("php.rar",4);
OK 大家有什么需求或者使用的可以联系我,我的邮箱admin#widuu.com(#换成@)或者qq联系23169450

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部