Creating ZIP files with PHP As part of my previously mentioned OpenVPN CA I want to deliver keys, certs and config files to users in a single zip file that they can just extract onto their computers. PHP's own ZIP File Functions only supports reading zip files and not making them.Some Googling discovered an article by John Coggeshall that can create zip files. It does this by creating the binary data on the fly and can output the zip files directly to the browser from memory or by writing it to disk.I had some troubles getting hold of a usable version of this code since all these PHP code collection sites have this annoying habit of only showing the syntax highlighted versions of the code rather than give a download link. Eventually got one though and I figured I'll host a mirror of it here to help people out.Using it is very simple, this is a quick sample that will create a ZIP file and add one directory and one file into then send it directly to the client.<? require ("incl/zipfile.inc.php"); $zipfile = new zipfile();
http://www.devco.net/code/zipfile.inc.txt<?php /* Zip file creation class makes zip files on the fly... use the functions add_dir() and add_file() to build the zip file; see example code below by Eric Mueller http://www.themepark.com v1.1 9-20-01 - added comments to example v1.0 2-5-01 initial version with: - class appearance - add_file() and file() methods - gzcompress() output hacking by Denis O.Philippov, [email protected], http://www.atlant.ru */ // official ZIP file format: http://www.pkware.com/appnote.txt class zipfile { var $datasec = array(); // array to store compressed data var $ctrl_dir = array(); // central directory var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; //end of Central directory record var $old_offset = 0; function add_dir($name) // adds "directory" to archive - do this before putting any files in directory! // $name - name of directory... like this: "path/" // ...then you can add files using add_file with names like "path/file.txt" { $name = str_replace("\\", "/", $name); $fr = "\x50\x4b\x03\x04"; $fr .= "\x0a\x00"; // ver needed to extract $fr .= "\x00\x00"; // gen purpose bit flag $fr .= "\x00\x00"; // compression method $fr .= "\x00\x00\x00\x00"; // last mod time and date $fr .= pack("V",0); // crc32 $fr .= pack("V",0); //compressed filesize $fr .= pack("V",0); //uncompressed filesize $fr .= pack("v", strlen($name) ); //length of pathname $fr .= pack("v", 0 ); //extra field length $fr .= $name; // end of "local file header" segment // no "file data" segment for path // "data descriptor" segment (optional but necessary if archive is not served as file) $fr .= pack("V",$crc); //crc32 $fr .= pack("V",$c_len); //compressed filesize $fr .= pack("V",$unc_len); //uncompressed filesize // add this entry to array $this -> datasec[] = $fr; $new_offset = strlen(implode("", $this->datasec)); // ext. file attributes mirrors MS-DOS directory attr byte, detailed // at http://support.microsoft.com/support/kb/articles/Q125/0/19.asp // now add to central record $cdrec = "\x50\x4b\x01\x02"; $cdrec .="\x00\x00"; // version made by $cdrec .="\x0a\x00"; // version needed to extract $cdrec .="\x00\x00"; // gen purpose bit flag $cdrec .="\x00\x00"; // compression method $cdrec .="\x00\x00\x00\x00"; // last mod time & date $cdrec .= pack("V",0); // crc32 $cdrec .= pack("V",0); //compressed filesize $cdrec .= pack("V",0); //uncompressed filesize $cdrec .= pack("v", strlen($name) ); //length of filename $cdrec .= pack("v", 0 ); //extra field length $cdrec .= pack("v", 0 ); //file comment length $cdrec .= pack("v", 0 ); //disk number start $cdrec .= pack("v", 0 ); //internal file attributes $ext = "\x00\x00\x10\x00"; $ext = "\xff\xff\xff\xff"; $cdrec .= pack("V", 16 ); //external file attributes - 'directory' bit set $cdrec .= pack("V", $this -> old_offset ); //relative offset of local header $this -> old_offset = $new_offset; $cdrec .= $name; // optional extra field, file comment goes here // save to array $this -> ctrl_dir[] = $cdrec; } function add_file($data, $name) // adds "file" to archive // $data - file contents // $name - name of file in archive. Add path if your want { $name = str_replace("\\", "/", $name); //$name = str_replace("\\", "\\\\", $name); $fr = "\x50\x4b\x03\x04"; $fr .= "\x14\x00"; // ver needed to extract $fr .= "\x00\x00"; // gen purpose bit flag $fr .= "\x08\x00"; // compression method $fr .= "\x00\x00\x00\x00"; // last mod time and date $unc_len = strlen($data); $crc = crc32($data); $zdata = gzcompress($data); $zdata = substr( substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug $c_len = strlen($zdata); $fr .= pack("V",$crc); // crc32 $fr .= pack("V",$c_len); //compressed filesize $fr .= pack("V",$unc_len); //uncompressed filesize $fr .= pack("v", strlen($name) ); //length of filename $fr .= pack("v", 0 ); //extra field length $fr .= $name; // end of "local file header" segment
// "file data" segment $fr .= $zdata; // "data descriptor" segment (optional but necessary if archive is not served as file) $fr .= pack("V",$crc); //crc32 $fr .= pack("V",$c_len); //compressed filesize $fr .= pack("V",$unc_len); //uncompressed filesize // add this entry to array $this -> datasec[] = $fr; $new_offset = strlen(implode("", $this->datasec)); // now add to central directory record $cdrec = "\x50\x4b\x01\x02"; $cdrec .="\x00\x00"; // version made by $cdrec .="\x14\x00"; // version needed to extract $cdrec .="\x00\x00"; // gen purpose bit flag $cdrec .="\x08\x00"; // compression method $cdrec .="\x00\x00\x00\x00"; // last mod time & date $cdrec .= pack("V",$crc); // crc32 $cdrec .= pack("V",$c_len); //compressed filesize $cdrec .= pack("V",$unc_len); //uncompressed filesize $cdrec .= pack("v", strlen($name) ); //length of filename $cdrec .= pack("v", 0 ); //extra field length $cdrec .= pack("v", 0 ); //file comment length $cdrec .= pack("v", 0 ); //disk number start $cdrec .= pack("v", 0 ); //internal file attributes $cdrec .= pack("V", 32 ); //external file attributes - 'archive' bit set $cdrec .= pack("V", $this -> old_offset ); //relative offset of local header // echo "old offset is ".$this->old_offset.", new offset is $new_offset<br>"; $this -> old_offset = $new_offset; $cdrec .= $name; // optional extra field, file comment goes here // save to central directory $this -> ctrl_dir[] = $cdrec; } function file() { // dump out file $data = implode("", $this -> datasec); $ctrldir = implode("", $this -> ctrl_dir); return $data. $ctrldir. $this -> eof_ctrl_dir. pack("v", sizeof($this -> ctrl_dir)). // total # of entries "on this disk" pack("v", sizeof($this -> ctrl_dir)). // total # of entries overall pack("V", strlen($ctrldir)). // size of central dir pack("V", strlen($data)). // offset to start of central dir "\x00\x00"; // .zip file comment length } } ?>
2 最简单的方式就是用winrar或者zip通过命令行加密,这个你还不选择.
3 另外就是分析加密和未加密后的文件,通过文件读写的方式操作,这个可行性不太高,而且对实现技术要求很高.
<?php
/**
* Passport 加密函数
*
* @param string 等待加密的原字串
* @param string 私有密匙(用于解密和加密)
*
* @return string 原字串经过私有密匙加密后的结果
*/
function passport_encrypt($txt, $key)
{
// 使用随机数发生器产生 0~32000 的值并 MD5()
srand((double)microtime() * 1000000);
$encrypt_key = md5(rand(0, 32000));
// 变量初始化
$ctr = 0;
$tmp = '';
// for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
for($i = 0; $i < strlen($txt); $i++)
{
// 如果 $ctr = $encrypt_key 的长度,则 $ctr 清零
$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
// $tmp 字串在末尾增加两位,其第一位内容为 $encrypt_key 的第 $ctr 位,
// 第二位内容为 $txt 的第 $i 位与 $encrypt_key 的 $ctr 位取异或。然后 $ctr = $ctr + 1
$tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
}
// 返回结果,结果为 passport_key() 函数返回值的 base65 编码结果
return base64_encode(passport_key($tmp, $key));
}
/**
* Passport 解密函数
*
* @param string 加密后的字串
* @param string 私有密匙(用于解密和加密)
*
* @return string 字串经过私有密匙解密后的结果
*/
function passport_decrypt($txt, $key)
{
// $txt 的结果为加密后的字串经过 base64 解码,然后与私有密匙一起,
// 经过 passport_key() 函数处理后的返回值
$txt = passport_key(base64_decode($txt), $key);
// 变量初始化
$tmp = '';
// for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
for ($i = 0; $i < strlen($txt); $i++)
{
// $tmp 字串在末尾增加一位,其内容为 $txt 的第 $i 位,
// 与 $txt 的第 $i + 1 位取异或。然后 $i = $i + 1
$tmp .= $txt[$i] ^ $txt[++$i];
}
// 返回 $tmp 的值作为结果
return $tmp;
}
/**
* Passport 密匙处理函数
*
* @param string 待加密或待解密的字串
* @param string 私有密匙(用于解密和加密)
*
* @return string 处理后的密匙
*/
function passport_key($txt, $encrypt_key)
{
// 将 $encrypt_key 赋为 $encrypt_key 经 md5() 后的值
$encrypt_key = md5($encrypt_key);
// 变量初始化
$ctr = 0;
$tmp = '';
// for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
for($i = 0; $i < strlen($txt); $i++)
{
// 如果 $ctr = $encrypt_key 的长度,则 $ctr 清零
$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
// $tmp 字串在末尾增加一位,其内容为 $txt 的第 $i 位,
// 与 $encrypt_key 的第 $ctr + 1 位取异或。然后 $ctr = $ctr + 1
$tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
}
// 返回 $tmp 的值作为结果
return $tmp;
}
/**
* Passport 信息(数组)编码函数
*
* @param array 待编码的数组
*
* @return string 数组经编码后的字串
*/
function passport_encode($array)
{
// 数组变量初始化
$arrayenc = array();
// 遍历数组 $array,其中 $key 为当前元素的下标,$val 为其对应的值
foreach($array as $key => $val)
{
// $arrayenc 数组增加一个元素,其内容为 "$key=经过 urlencode() 后的 $val 值"
$arrayenc[] = $key.'='.urlencode($val);
}
// 返回以 "&" 连接的 $arrayenc 的值(implode),例如 $arrayenc = array('aa', 'bb', 'cc', 'dd'),
// 则 implode('&', $arrayenc) 后的结果为 ”aa&bb&cc&dd"
return implode('&', $arrayenc);
}
?>以上這段是抄的,希望對你有用
如果知道加密的zip文件格式定义,是不是就可以做到呢??
http://www.w3school.com.cn/php/php_ref_zip.asp
As part of my previously mentioned OpenVPN CA I want to deliver keys, certs and config files to users in a single zip file that they can just extract onto their computers. PHP's own ZIP File Functions only supports reading zip files and not making them.Some Googling discovered an article by John Coggeshall that can create zip files. It does this by creating the binary data on the fly and can output the zip files directly to the browser from memory or by writing it to disk.I had some troubles getting hold of a usable version of this code since all these PHP code collection sites have this annoying habit of only showing the syntax highlighted versions of the code rather than give a download link. Eventually got one though and I figured I'll host a mirror of it here to help people out.Using it is very simple, this is a quick sample that will create a ZIP file and add one directory and one file into then send it directly to the client.<?
require ("incl/zipfile.inc.php"); $zipfile = new zipfile();
$filedata = implode("", file("incl/zipfile.inc.php")); $zipfile->add_dir("incl/");
$zipfile->add_file($filedata, "incl/zipfile.inc.php"); header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=zipfile.zip");
echo $zipfile->file();
?>
/*
Zip file creation class
makes zip files on the fly... use the functions add_dir() and add_file() to build the zip file;
see example code below by Eric Mueller
http://www.themepark.com v1.1 9-20-01
- added comments to example v1.0 2-5-01 initial version with:
- class appearance
- add_file() and file() methods
- gzcompress() output hacking
by Denis O.Philippov, [email protected], http://www.atlant.ru */ // official ZIP file format: http://www.pkware.com/appnote.txt class zipfile
{ var $datasec = array(); // array to store compressed data
var $ctrl_dir = array(); // central directory
var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; //end of Central directory record
var $old_offset = 0; function add_dir($name) // adds "directory" to archive - do this before putting any files in directory!
// $name - name of directory... like this: "path/"
// ...then you can add files using add_file with names like "path/file.txt"
{
$name = str_replace("\\", "/", $name); $fr = "\x50\x4b\x03\x04";
$fr .= "\x0a\x00"; // ver needed to extract
$fr .= "\x00\x00"; // gen purpose bit flag
$fr .= "\x00\x00"; // compression method
$fr .= "\x00\x00\x00\x00"; // last mod time and date $fr .= pack("V",0); // crc32
$fr .= pack("V",0); //compressed filesize
$fr .= pack("V",0); //uncompressed filesize
$fr .= pack("v", strlen($name) ); //length of pathname
$fr .= pack("v", 0 ); //extra field length
$fr .= $name;
// end of "local file header" segment // no "file data" segment for path // "data descriptor" segment (optional but necessary if archive is not served as file)
$fr .= pack("V",$crc); //crc32
$fr .= pack("V",$c_len); //compressed filesize
$fr .= pack("V",$unc_len); //uncompressed filesize // add this entry to array
$this -> datasec[] = $fr; $new_offset = strlen(implode("", $this->datasec)); // ext. file attributes mirrors MS-DOS directory attr byte, detailed
// at http://support.microsoft.com/support/kb/articles/Q125/0/19.asp // now add to central record
$cdrec = "\x50\x4b\x01\x02";
$cdrec .="\x00\x00"; // version made by
$cdrec .="\x0a\x00"; // version needed to extract
$cdrec .="\x00\x00"; // gen purpose bit flag
$cdrec .="\x00\x00"; // compression method
$cdrec .="\x00\x00\x00\x00"; // last mod time & date
$cdrec .= pack("V",0); // crc32
$cdrec .= pack("V",0); //compressed filesize
$cdrec .= pack("V",0); //uncompressed filesize
$cdrec .= pack("v", strlen($name) ); //length of filename
$cdrec .= pack("v", 0 ); //extra field length
$cdrec .= pack("v", 0 ); //file comment length
$cdrec .= pack("v", 0 ); //disk number start
$cdrec .= pack("v", 0 ); //internal file attributes
$ext = "\x00\x00\x10\x00";
$ext = "\xff\xff\xff\xff";
$cdrec .= pack("V", 16 ); //external file attributes - 'directory' bit set $cdrec .= pack("V", $this -> old_offset ); //relative offset of local header
$this -> old_offset = $new_offset; $cdrec .= $name;
// optional extra field, file comment goes here
// save to array
$this -> ctrl_dir[] = $cdrec;
}
function add_file($data, $name) // adds "file" to archive
// $data - file contents
// $name - name of file in archive. Add path if your want {
$name = str_replace("\\", "/", $name);
//$name = str_replace("\\", "\\\\", $name); $fr = "\x50\x4b\x03\x04";
$fr .= "\x14\x00"; // ver needed to extract
$fr .= "\x00\x00"; // gen purpose bit flag
$fr .= "\x08\x00"; // compression method
$fr .= "\x00\x00\x00\x00"; // last mod time and date $unc_len = strlen($data);
$crc = crc32($data);
$zdata = gzcompress($data);
$zdata = substr( substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
$c_len = strlen($zdata);
$fr .= pack("V",$crc); // crc32
$fr .= pack("V",$c_len); //compressed filesize
$fr .= pack("V",$unc_len); //uncompressed filesize
$fr .= pack("v", strlen($name) ); //length of filename
$fr .= pack("v", 0 ); //extra field length
$fr .= $name;
// end of "local file header" segment
// "file data" segment
$fr .= $zdata; // "data descriptor" segment (optional but necessary if archive is not served as file)
$fr .= pack("V",$crc); //crc32
$fr .= pack("V",$c_len); //compressed filesize
$fr .= pack("V",$unc_len); //uncompressed filesize // add this entry to array
$this -> datasec[] = $fr; $new_offset = strlen(implode("", $this->datasec)); // now add to central directory record
$cdrec = "\x50\x4b\x01\x02";
$cdrec .="\x00\x00"; // version made by
$cdrec .="\x14\x00"; // version needed to extract
$cdrec .="\x00\x00"; // gen purpose bit flag
$cdrec .="\x08\x00"; // compression method
$cdrec .="\x00\x00\x00\x00"; // last mod time & date
$cdrec .= pack("V",$crc); // crc32
$cdrec .= pack("V",$c_len); //compressed filesize
$cdrec .= pack("V",$unc_len); //uncompressed filesize
$cdrec .= pack("v", strlen($name) ); //length of filename
$cdrec .= pack("v", 0 ); //extra field length
$cdrec .= pack("v", 0 ); //file comment length
$cdrec .= pack("v", 0 ); //disk number start
$cdrec .= pack("v", 0 ); //internal file attributes
$cdrec .= pack("V", 32 ); //external file attributes - 'archive' bit set $cdrec .= pack("V", $this -> old_offset ); //relative offset of local header
// echo "old offset is ".$this->old_offset.", new offset is $new_offset<br>";
$this -> old_offset = $new_offset; $cdrec .= $name;
// optional extra field, file comment goes here
// save to central directory
$this -> ctrl_dir[] = $cdrec;
} function file() { // dump out file
$data = implode("", $this -> datasec);
$ctrldir = implode("", $this -> ctrl_dir); return
$data.
$ctrldir.
$this -> eof_ctrl_dir.
pack("v", sizeof($this -> ctrl_dir)). // total # of entries "on this disk"
pack("v", sizeof($this -> ctrl_dir)). // total # of entries overall
pack("V", strlen($ctrldir)). // size of central dir
pack("V", strlen($data)). // offset to start of central dir
"\x00\x00"; // .zip file comment length
}
} ?>
$filename = "filename.zip";
$pdf = "document.pdf";
echo system("zip -P 1234 -j $filename \"$pdf\"");
?>
2 最简单的方式就是用winrar或者zip通过命令行加密,这个你还不选择.
3 另外就是分析加密和未加密后的文件,通过文件读写的方式操作,这个可行性不太高,而且对实现技术要求很高.
高版本的没有这个扩展了。