PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > ZipArchive() (adding a directory)
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
ZipArchive() (adding a directory)

Réponse
 
LinkBack Outils de la discussion
Vieux 29/02/2008, 17h26   #1
techjohnny@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut ZipArchive() (adding a directory)

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();
?>
  Réponse avec citation
Vieux 29/02/2008, 22h32   #2
wowo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ZipArchive() (adding a directory)

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);
  Réponse avec citation
Vieux 01/03/2008, 17h53   #3
techjohnny@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ZipArchive() (adding a directory)

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');
?>
  Réponse avec citation
Vieux 03/03/2008, 03h09   #4
techjohnny@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ZipArchive() (adding a directory)

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h36.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14915 seconds with 12 queries