确认php安装文件夹下的ext文件夹中具有php_zip.dll文件
修改php.ini
打开
extension=php_zip.dll
重启apache
PHP Version 5.2.5
出现这个错误说明是程序在调用'ZipArchive' 这个类的时候没有成功,原因是由于在安装php的时候没有增加php zip的支持(非zlib)。
在Windows下的解决办法是 :
1、在php.ini文件中,将extension=php_zip.dll前面的分号“;”去除,然后同样在php.ini文件中,将 zlib.output_compression = Off 改为 zlib.output_compression = On ;
2、重启Apache服务器。
在Unix/Linux下的解决办法 :
1、在Linux下没有php_zip.dll这个文件(有也不会起作用的),所以需要重新编译一下php的zip模块。具体安装方法如下:
cd /usr/src
wget http://pecl.php.net/get/zip
tar -zxvf zip
cd zip-1.x.x
phpize
./configure
make
sudo make instal
其中, 在最后使用make install命令的时候,可能需要用到root的权限,所以建议使用sudo来运行。安装完之后,屏幕上会提示zip.so的位置。然后将其记录下来,如:/usr/local/lib/php/extensions/zip.so。
2、使用root权限修改php.ini(通常可能会在/usr/local/lib/文件夹下,不过视当初安装php而定,可以通过phpinfo()来查看):
增加extension = /usr/local/lib/php/extensions/zip.so,然后同样在php.ini文件中,将 zlib.output_compression = Off 改为 zlib.output_compression = On ;
3、最后别忘了重启一下Apache:apachectl restart;
这个针对php的zip模块就安装完成了,能够在php中使用ZipArchive类了。
初步看了一下ZipArchive文档,简单地封装了一下压缩和解压功能.代码如下:
/*
* CompressZipFile: 压缩文件
* lpszFile: 需要压缩的文件
* lpszZipFile: 要压缩到的目标文件
* bAppend: 标志是否添加到现有压缩文件
* 返回压缩成功与否
*/
BOOL CompressZipFile( LPCTSTR lpszFile, LPCTSTR lpszZipFile, BOOL bAppend = FALSE )
{
// 如果是追加到现有压缩文件,但现有压缩文件不存在的话 直接返回失败
if ( bAppend && ( ! ::PathFileExists( lpszZipFile ) ) )
return FALSE;
int nMode = bAppend ? CZipArchive::zipOpen : CZipArchive::zipCreate;
CZipArchive zipArchive;
if ( ! zipArchive.Open( lpszZipFile, nMode ) )
return FALSE;
// 获取路径中的文件名部分
TCHAR szFile[MAX_PATH];
lstrcpy( szFile, lpszFile );
::PathStripPath( szFile );
bool bRet = zipArchive.AddNewFile( lpszFile, szFile );
zipArchive.Close();
return bRet;
}
/*
* ExtractZipFile : 解压指定文件
* lpszZipFile: 要解压的文件
* lpszDestPath: 指定解压到的目录
* 返回压缩成功与否
*/
BOOL ExtractZipFile( LPCTSTR lpszZipFile, LPCTSTR lpszDestPath )
{
if ( ! ::PathFileExists( lpszZipFile ))
return FALSE;
CZipArchive zipArchive;
if ( ! zipArchive.Open( lpszZipFile, CZipArchive::zipOpenReadOnly ) )
return FALSE;
for ( int nIndex = 0 ; nIndex < zipArchive.GetCount (); nIndex ++ )
{
if ( ! zipArchive.ExtractFile( (WORD)nIndex, lpszDestPath ) )
break ;
}
zipArchive.Close();
return TRUE;
}
压缩文件夹可以参考如下的Demo:
CZipArchive zipArchive;
if ( ! zipArchive.Open( _T( " D://aaa.zip " ) , CZipArchive::zipCreate ) )
return ;
// 压缩目录下的所有文件,第三个参数指示是否递归压缩子目录,第五个参数指示是否连文件夹目录一起压缩
zipArchive.AddNewFiles( _T( " D://aaa// " ), _T( " *.* " ), true , - 1 , false );
zipArchive.Close();
$newFile="ziptemp/{$id}.zip";
@copy("mailcons.zip",$newFile);
$zip = new ZipArchive;
if($zip->open($newFile) === TRUE) {
$zip->addFile($imgUrl, '图'.$i.".".$ext);
$zip->addFromString('内容.txt',"标题:{$titel}/r/n内容:/r/n".$body);
$zip->close();
* zip_close — Close a ZIP file archive
* zip_entry_close — Close a directory entry
* zip_entry_compressedsize — Retrieve the compressed size of a directory entry
* zip_entry_compressionmethod — Retrieve the compression method of a directory entry
* zip_entry_filesize — Retrieve the actual file size of a directory entry
* zip_entry_name — Retrieve the name of a directory entry
* zip_entry_open — Open a directory entry for reading
* zip_entry_read — Read from an open directory entry
* zip_open — Open a ZIP file archive
* zip_read — Read next entry in a ZIP file archive
* ZipArchive::addEmptyDir — Add a new directory
* ZipArchive::addFile — Adds a file to a ZIP archive from the given path
* ZipArchive::addFromString — Add a file to a ZIP archive using its contents
* ZipArchive::close — Close the active archive (opened or newly created)
* ZipArchive::deleteIndex — delete an entry in the archive using its index
* ZipArchive::deleteName — delete an entry in the archive using its name
* ZipArchive::extractTo — Extract the archive contents
* ZipArchive::getArchiveComment — Returns the Zip archive comment
* ZipArchive::getCommentIndex — Returns the comment of an entry using the entry index
* ZipArchive::getCommentName — Returns the comment of an entry using the entry name
* ZipArchive::getFromIndex — Returns the entry contents using its index.
* ZipArchive::getFromName — Returns the entry contents using its name.
* ZipArchive::getNameIndex — Returns the name of an entry using its index
* ZipArchive::getStream — Get a file handler to the entry defined by its name (read only).
* ZipArchive::locateName — Returns the index of the entry in the archive
* ZipArchive::open — Open a ZIP file archive
* ZipArchive::renameIndex — Renames an entry defined by its index
* ZipArchive::renameName — Renames an entry defined by its name
* ZipArchive::setArchiveComment — Set the comment of a ZIP archive
* ZipArchive::setCommentIndex — Set the comment of an entry defined by its index
* ZipArchive::setCommentName — Set the comment of an entry defined by its name
* ZipArchive::statIndex — Get the details of an entry defined by its index.
* ZipArchive::statName — Get the details of an entry defined by its name.
* ZipArchive::unchangeAll — Undo all changes done in the archive.
* ZipArchive::unchangeArchive — Revert all global changes done in the archive.
* ZipArchive::unchangeIndex — Revert all changes done to an entry at the given index.
* ZipArchive::unchangeName — Revert all changes done to an entry with the given name.
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/taipingliebeiluo/archive/2010/08/27/5844137.aspx