|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hello, group:
I'm able to successfully add files to a zip archive using php with the following script, but I can't find anywhere I add directories, can somebody ? Thanks, --TJ <?php $zip = new ZipArchive(); $filename = "./test112.zip"; if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { exit("cannot open <$filename>\n"); } $zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n"); $zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n"); $zip->addFile($thisdir . "/too.php","/testfromfile.php"); $zip->close(); ?> |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 29 Lut, 18:26, "techjoh...@gmail.com" <techjoh...@gmail.com> wrote:
> Hello, group: > > I'm able to successfully add files to a zip archive using php with the > following script, but I can't find anywhere I add directories, can > somebody ? > > Thanks, > > --TJ > > <?php > > $zip = new ZipArchive(); > $filename = "./test112.zip"; > > if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { > exit("cannot open <$filename>\n"); > > } > > $zip->addFromString("testfilephp.txt" . time(), "#1 This is a test > string added as testfilephp.txt.\n"); > $zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test > string added as testfilephp2.txt.\n"); > $zip->addFile($thisdir . "/too.php","/testfromfile.php"); > $zip->close(); > ?> $zip->AddEmptyDirectory($_folderName) if you wish to add file to this directory: $zip->AddFile($_folderName .'/' . $_file); |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Feb 29, 2:32 pm, wowo <wow...@gmail.com> wrote:
> On 29 Lut, 18:26, "techjoh...@gmail.com" <techjoh...@gmail.com> wrote: > > > > > Hello, group: > > > I'm able to successfully add files to a zip archive using php with the > > following script, but I can't find anywhere I add directories, can > > somebody ? > > > Thanks, > > > --TJ > > > <?php > > > $zip = new ZipArchive(); > > $filename = "./test112.zip"; > > > if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { > > exit("cannot open <$filename>\n"); > > > } > > > $zip->addFromString("testfilephp.txt" . time(), "#1 This is a test > > string added as testfilephp.txt.\n"); > > $zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test > > string added as testfilephp2.txt.\n"); > > $zip->addFile($thisdir . "/too.php","/testfromfile.php"); > > $zip->close(); > > ?> > > $zip->AddEmptyDirectory($_folderName) > if you wish to add file to this directory: > $zip->AddFile($_folderName .'/' . $_file); Here is what is working for me now: (php.net) <?php class ZipFolder { protected $zip; protected $root; protected $ignored_names; function __construct($file, $folder, $ignored=null) { $this->zip = new ZipArchive(); $this->ignored_names = is_array($ignored) ? $ignored : $ignored ? array($ignored) : array(); if ($this->zip->open($file, ZIPARCHIVE::CREATE)!==TRUE) { throw new Exception("cannot open <$file>\n"); } $folder = substr($folder, -1) == '/' ? substr($folder, 0, strlen($folder)-1) : $folder; if(strstr($folder, '/')) { $this->root = substr($folder, 0, strrpos($folder, '/')+1); $folder = substr($folder, strrpos($folder, '/')+1); } $this->zip($folder); $this->zip->close(); } function zip($folder, $parent=null) { $full_path = $this->root.$parent.$folder; $zip_path = $parent.$folder; $this->zip->addEmptyDir($zip_path); $dir = new DirectoryIterator($full_path); foreach($dir as $file) { if(!$file->isDot()) { $filename = $file->getFilename(); if(!in_array($filename, $this->ignored_names)) { if($file->isDir()) { $this->zip($filename, $zip_path.'/'); } else { $this->zip->addFile($full_path.'/'.$filename, $zip_path.'/'.$filename); } } } } } } // full path used to demonstrate it's root-path stripping ability $zip = new ZipFolder('/tmp/test.zip', dirname(__FILE__).'/templates/', '.svn'); ?> |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Mar 1, 9:53 am, "techjoh...@gmail.com" <techjoh...@gmail.com>
wrote: > On Feb 29, 2:32 pm, wowo <wow...@gmail.com> wrote: > > > > > On 29 Lut, 18:26, "techjoh...@gmail.com" <techjoh...@gmail.com> wrote: > > > > Hello, group: > > > > I'm able to successfully add files to a zip archive using php with the > > > following script, but I can't find anywhere I add directories, can > > > somebody ? > > > > Thanks, > > > > --TJ > > > > <?php > > > > $zip = new ZipArchive(); > > > $filename = "./test112.zip"; > > > > if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { > > > exit("cannot open <$filename>\n"); > > > > } > > > > $zip->addFromString("testfilephp.txt" . time(), "#1 This is a test > > > string added as testfilephp.txt.\n"); > > > $zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test > > > string added as testfilephp2.txt.\n"); > > > $zip->addFile($thisdir . "/too.php","/testfromfile.php"); > > > $zip->close(); > > > ?> > > > $zip->AddEmptyDirectory($_folderName) > > if you wish to add file to this directory: > > $zip->AddFile($_folderName .'/' . $_file); > > Here is what is working for me now: (php.net) > > <?php > class ZipFolder { > protected $zip; > protected $root; > protected $ignored_names; > > function __construct($file, $folder, $ignored=null) { > $this->zip = new ZipArchive(); > $this->ignored_names = is_array($ignored) ? $ignored : > $ignored ? array($ignored) : array(); > if ($this->zip->open($file, ZIPARCHIVE::CREATE)!==TRUE) { > throw new Exception("cannot open <$file>\n"); > } > $folder = substr($folder, -1) == '/' ? substr($folder, 0, > strlen($folder)-1) : $folder; > if(strstr($folder, '/')) { > $this->root = substr($folder, 0, strrpos($folder, '/')+1); > $folder = substr($folder, strrpos($folder, '/')+1); > } > $this->zip($folder); > $this->zip->close(); > } > > function zip($folder, $parent=null) { > $full_path = $this->root.$parent.$folder; > $zip_path = $parent.$folder; > $this->zip->addEmptyDir($zip_path); > $dir = new DirectoryIterator($full_path); > foreach($dir as $file) { > if(!$file->isDot()) { > $filename = $file->getFilename(); > if(!in_array($filename, $this->ignored_names)) { > if($file->isDir()) { > $this->zip($filename, $zip_path.'/'); > } > else { > $this->zip->addFile($full_path.'/'.$filename, > $zip_path.'/'.$filename); > } > } > } > } > }} > > // full path used to demonstrate it's root-path stripping ability > $zip = new ZipFolder('/tmp/test.zip', dirname(__FILE__).'/templates/', > '.svn'); > ?> Actually, maybe somebody can me zip a file within the root of the directory? Right now, it uses the root directory of the directory the file is created in. Thanks, --JP |
|
![]() |
| Outils de la discussion | |
|
|