PHP访问ZIP压缩文件
1. 对于PHP5.0以上只需要首先 查看php目录的ext下有没有php_zip.dll文件,然后 打开php.ini中的extension=php_zip<wbr></wbr>.dll
2.
然后就可以使用PHP的zip function了
实例程序Zip Usage Example
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
<?
php
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
if
(
file_exists
(
'
test.zip
'
))
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />{
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
$zip
=
zip_open(
realpath
(
'
test.zip
'
));
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
if
(
is_resource
(
$zip
)){
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
while
(
$zip_entry
=
zip_read(
$zip
)){
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
echo
"
Name:
"
.
zip_entry_name(
$zip_entry
)
.
"
"
;
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
echo
"
ActualFilesize:
"
.
zip_entry_filesize(
$zip_entry
)
.
"
"
;
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
echo
"
CompressedSize:
"
.
zip_entry_compressedsize(
$zip_entry
)
.
"
"
;
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
echo
"
CompressionMethod:
"
.
zip_entry_compressionmethod(
$zip_entry
)
.
"
"
;
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
if
(zip_entry_open(
$zip
,
$zip_entry
,
"
r
"
)){
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
echo
"
FileContents:
"
;
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
$buf
=
zip_entry_read(
$zip_entry
,
zip_entry_filesize(
$zip_entry
));
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
echo
"
$buf
"
;
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />zip_entry_close(
$zip_entry
);
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />}
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
echo
"
"
;
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />}
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />zip_close(
$zip
);
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />}
else
{
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
echo
"
cannotopenthefile:test.zip
"
;
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />}
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />}
else
{
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
echo
"
cannotfindthefile:test.zip
"
;
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />}
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
?>
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
3.
<!-- D(["mb","需要注意的是:\n\u003cbr\>\u003cbr\>\u003c/span\>\u003c/font\>a. 上面程序的第一句将会错误;\u003cbr\>$zip \u003d zip_open("test.zip");\u003cbr\>正确情况下$zip将返回一个resource变量,如果错误的话\u003cbr\>Returns a resource handle for later use with zip_read() and zip_close() or returns the number of error if filename does not exist or in case of other error.\n\u003cbr\>会返回一个预定义的整形常量;\u003cbr\>\u003cbr\>b.\u003cbr\>zip_open函数执行失败的原因是zip_open函数中zi\u003cWBR\>p文件的路径不能使用相对路径\u003cbr\>举例说明:\u003cbr\>如果在以上zip_test.php路径下有test.zip,正确的需要添加上realpath为\u003cbr\>$zip\u003dzip_open(realpath('test.zip'));\u003cbr\>\u003cbr\>\u003cbr\>\u003cbr\>详情参考\u003cbr\>\u003ca href\u003d\"http://www.w3schools.com/php/php_ref_zip.asp\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\"\>\nhttp://www.w3schools.com/php\u003cWBR\>/php_ref_zip.asp\u003c/a\>\u003cbr\>\u003ca href\u003d\"http://cn.php.net/zip\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\"\>http://cn.php.net/zip\u003c/a\>\u003cbr\>\u003ca href\u003d\"http://cn.php.net/manual/zh/function.zip-open.php\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\"\>http://cn.php.net/manual/zh\u003cWBR\>/function.zip-open.php\n\u003c/a\>\u003cbr\>\u003cbr\>\n",0] ); D(["ce"]); //--> 需要注意的是:
a. 上面程序的第一句将会错误;
$zip = zip_open("test.zip");
正确情况下$zip将返回一个resource变量,如果错误的话
Returns a resource handle for later use with zip_read() and zip_close() or returns the number of error if filename does not exist or in case of other error.
会返回一个预定义的整形常量;
b.
zip_open函数执行失败的原因是zip_open函数中zi
1. 对于PHP5.0以上只需要首先 查看php目录的ext下有没有php_zip.dll文件,然后 打开php.ini中的extension=php_zip<wbr></wbr>.dll
2.
然后就可以使用PHP的zip function了
实例程序Zip Usage Example
3.
<!-- D(["mb","需要注意的是:\n\u003cbr\>\u003cbr\>\u003c/span\>\u003c/font\>a. 上面程序的第一句将会错误;\u003cbr\>$zip \u003d zip_open("test.zip");\u003cbr\>正确情况下$zip将返回一个resource变量,如果错误的话\u003cbr\>Returns a resource handle for later use with zip_read() and zip_close() or returns the number of error if filename does not exist or in case of other error.\n\u003cbr\>会返回一个预定义的整形常量;\u003cbr\>\u003cbr\>b.\u003cbr\>zip_open函数执行失败的原因是zip_open函数中zi\u003cWBR\>p文件的路径不能使用相对路径\u003cbr\>举例说明:\u003cbr\>如果在以上zip_test.php路径下有test.zip,正确的需要添加上realpath为\u003cbr\>$zip\u003dzip_open(realpath('test.zip'));\u003cbr\>\u003cbr\>\u003cbr\>\u003cbr\>详情参考\u003cbr\>\u003ca href\u003d\"http://www.w3schools.com/php/php_ref_zip.asp\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\"\>\nhttp://www.w3schools.com/php\u003cWBR\>/php_ref_zip.asp\u003c/a\>\u003cbr\>\u003ca href\u003d\"http://cn.php.net/zip\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\"\>http://cn.php.net/zip\u003c/a\>\u003cbr\>\u003ca href\u003d\"http://cn.php.net/manual/zh/function.zip-open.php\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\"\>http://cn.php.net/manual/zh\u003cWBR\>/function.zip-open.php\n\u003c/a\>\u003cbr\>\u003cbr\>\n",0] ); D(["ce"]); //--> 需要注意的是:
a. 上面程序的第一句将会错误;
$zip = zip_open("test.zip");
正确情况下$zip将返回一个resource变量,如果错误的话
Returns a resource handle for later use with zip_read() and zip_close() or returns the number of error if filename does not exist or in case of other error.
会返回一个预定义的整形常量;
b.
zip_open函数执行失败的原因是zip_open函数中zi