ziparchive

Extract folder content using ZipArchive

老子叫甜甜 提交于 2019-12-04 09:21:46
I have compressed_file.zip on a site with this structure: I want to extract all content from version_1.x folder to my root folder: How can I do that? is possible without recursion? It's possible, but you have to read and write the file yourself using ZipArchive::getStream : $source = 'version_1.x'; $target = '/path/to/target'; $zip = new ZipArchive; $zip->open('myzip.zip'); for($i=0; $i<$zip->numFiles; $i++) { $name = $zip->getNameIndex($i); // Skip files not in $source if (strpos($name, "{$source}/") !== 0) continue; // Determine output filename (removing the $source prefix) $file = $target.'

使用PHP处理zip压缩文件之ZipArchive

流过昼夜 提交于 2019-12-04 01:48:31
PHP5.2以后,强化了对压缩文件的处理,不仅对zip算法,还包括rar算法、gzip算法等都有了相应的支持。 今天,我来和大家分享一下用PHP处理zip文件。我们用到的是ZipArchive类,如果你安装了PHP5.2以上,无需做任何配置即可开始使用该类。 创建压缩文件: <?php //实例化类 $zip = new ZipArchive(); //需要打开的zip文件,文件不存在将会自动创建 $filename = "./test.zip"; if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { //如果是Linux系统,需要保证服务器开放了文件写权限 exit("文件打开失败!"); } //将一段字符串添加到压缩文件中,test.txt文件会自动创建 $zip->addFromString("test.txt", "你好 , 世界"); //将test.php文件添加到压缩文件中 $zip->addFile("test.php"); //输出加入的文件数 , 这里应该是 2 echo "文件数 : ".$zip->numFiles; //关闭文件 $zip->close(); ?> 创建的文件结构: 下面再来看解压文件: <?php $zip = new ZipArchive; if ($zip->open(

PHP学习笔记-非常有用的 PHP 代码片段(1)

痴心易碎 提交于 2019-12-04 01:48:16
怎么开启 ZipArchive 扩展 请可自行百度。 直接上代码 //单个文件插入Zip包 function addFileToZip($path, $zip) { $handler = opendir($path); //打开当前文件夹由$path指定。 /* 循环的读取文件夹下的所有文件和文件夹 其中$filename = readdir($handler)是每次循环的时候将读取的文件名赋值给$filename, 为了不陷于死循环,所以还要让$filename !== false。 一定要用!==,因为如果某个文件名如果叫'0',或者某些被系统认为是代表false,用!=就会停止循环 */ while (($filename = readdir($handler)) !== false) { if ($filename != "." && $filename != "..") {//文件夹文件名字为'.'和‘..’,不要对他们进行操作 if (is_dir($path . "/" . $filename)) {// 如果读取的某个对象是文件夹,则递归 addFileToZip($path . "/" . $filename, $zip); } else { //将文件加入zip对象 $zip->addFile($path . "/" . $filename); } } }

Enable ZipArchive on localhost [closed]

梦想的初衷 提交于 2019-12-04 00:46:27
Closed. This question is off-topic. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 7 years ago . I need to enable ZipArchive php class on my localhost , How can I enable it !? add extension=zip.so to your php.ini if you already have it installed (not sure what OS you're on) Linux: pecl install zip Restart the web server once php.ini is edited. 来源: https://stackoverflow.com/questions/12477837/enable-ziparchive-on-localhost

String to Zipped Stream in php

☆樱花仙子☆ 提交于 2019-12-03 22:53:46
问题 I have a processing server with my database and a serving database to serve up files with a low bandwidth cost. On the processing server, php is not able to create files so everything must be done with streams and/or stay in memory before being sent over to another server for download. A few days ago I found out about the stream abstraction with 'php://memory' and that I can do something like $fp=fopen('php://memory','w+'); fwrite($fp,"Hello world"); fseek($fp,0,SEEK_SET); //make a ftp

PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载

别等时光非礼了梦想. 提交于 2019-12-02 22:17:43
PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有疑问欢迎交流。这里整理一下常用的示例供参考。 一、解压缩zip文件 $zip = new ZipArchive;//新建一个ZipArchive的对象 /* 通过ZipArchive的对象处理zip文件 $zip->open这个方法的参数表示处理的zip文件名。 如果对zip文件对象操作成功,$zip->open这个方法会返回TRUE */ if ($zip->open('test.zip') === TRUE) { $zip->extractTo('images');//假设解压缩到在当前路径下images文件夹的子文件夹php $zip->close();//关闭处理的zip文件 } 二、将文件压缩成zip文件 $zip = new ZipArchive; /* $zip->open这个方法第一个参数表示处理的zip文件名。 第二个参数表示处理模式,ZipArchive::OVERWRITE表示如果zip文件存在,就覆盖掉原来的zip文件。 如果参数使用ZIPARCHIVE::CREATE,系统就会往原来的zip文件里添加内容。 如果不是为了多次添加内容到zip文件

c# UnauthorizedAccessException when using ZipArchive but not ZipFile

北城余情 提交于 2019-12-02 07:37:34
问题 I am able to zip files from a specific folder using ZipFile.CreateFromDirectory in the following test code (I only used this code to test how zipping works): // Where the files are located string strStartPath = txtTargetFolder.Text; // Where the zip file will be placed string strZipPath = @"C:\Users\smelmo\Desktop\testFinish\" + strFileNameRoot + "_" + txtDateRange1.Text.Replace(@"/", "_") + "_" + txtDateRange2.Text.Replace(@"/", "_") + ".zip"; ZipFile.CreateFromDirectory(strStartPath,

PHP ZipArchive Corrupt in Windows

≯℡__Kan透↙ 提交于 2019-12-01 02:52:45
I am using PHP's ZipArchive class to create a zip file containing photos and then serve it up to the browser for download. Here is my code: /** * Grabs the order, packages the files, and serves them up for download. * * @param string $intEntryID * @return void * @author Jesse Bunch */ public static function download_order_by_entry_id($intUniqueID) { $objCustomer = PhotoCustomer::get_customer_by_unique_id($intUniqueID); if ($objCustomer): if (!class_exists('ZipArchive')): trigger_error('ZipArchive Class does not exist', E_USER_ERROR); endif; $objZip = new ZipArchive(); $strZipFilename = sprintf

Php create zip file (from post attachments wordpress)

三世轮回 提交于 2019-11-30 21:29:17
I am trying to create a zip file from post attachments in wordpress . I have tried both methods below - but it results in nothing (No error messages , no file created) - What am I doing wrong (again .. ) I do not think that The fact that those are post attachments in wordpress has anything to do with it - because those methods failed also with normal files . why ? --> See the answer . $files_to_zip = array();// create files array //run a query $post_id = get_the_id(); $args = array( 'post_type' => 'attachment', 'numberposts' => null, 'post_status' => null, 'post_parent' => $post_id );

using zipArchive addFile() will not add image to zip

好久不见. 提交于 2019-11-30 19:20:44
I've been at this for a while. This actually worked one time, then never again. it simply does not create the zip file. The file does exist. $zip = new ZipArchive(); $filename = "./test" . time() .".zip"; if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { exit("cannot open <$filename>\n"); } $thisdir = "$_SERVER[DOCUMENT_ROOT]/zip"; $zip->addFile($thisdir . "/trash-icon.png", "/gabage.png"); echo "numfiles: " . $zip->numFiles . "\n"; echo "status:" . $zip->status . "\n"; $zip->close(); If I add something like $zip->addFromString("testfilephp.txt", "#1 This is a test string added as