cvs: peardoc /fr/package/fileformats/file-archive archmod.xml cache.xml examples-gallery.xml examples.xml predicates.xml readers.xml writers.xml

From: Date: Tue, 15 Apr 2008 15:58:54 +0000
Subject: cvs: peardoc /fr/package/fileformats/file-archive archmod.xml cache.xml examples-gallery.xml examples.xml predicates.xml readers.xml writers.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-10668@lists.php.net to get a copy of this message
moosh Tue Apr 15 15:58:54 2008 UTC Added files: /peardoc/fr/package/fileformats/file-archive archmod.xml cache.xml examples-gallery.xml examples.xml predicates.xml readers.xml writers.xml Log: launch translation

http://cvs.php.net/viewvc.cgi/peardoc/fr/package/fileformats/file-archive/archmod.xml?view=markup&rev=1.1 Index: peardoc/fr/package/fileformats/file-archive/archmod.xml +++ peardoc/fr/package/fileformats/file-archive/archmod.xml <?xml version="1.0" encoding="utf-8" ?> <!-- $Id: archmod.xml,v 1.1 2008/04/15 15:58:54 moosh Exp $ --> <!-- $Revision: 1.1 $ --> <!-- EN-Revision: 1.1 Maintainer: moosh Status: started --> <refentry id="package.fileformats.file-archive.archmod"> <refnamediv> <refname>Archive modification</refname> <refpurpose>Supprimer et ajouter des fichiers</refpurpose> </refnamediv> <refsect1 id="package.fileformats.file-archive.archmod.intro"> <title>Introduction</title> <para> <classname>File_Archive</classname> version 1.3 introduit quelques nouvelles fonctions pour éditer les archives existantes. Ces fonctions vous permettent de supprimer et ajouter des fichiers se trouvant dans une archive donnée. </para> <para> Since for <classname>File_Archive</classname>, the file system is just another reader / writer, those modifications can be done on "real" archives (real files), or on nested archives (an archive inside another archive). </para> </refsect1> <refsect1 id="package.fileformats.file-archive.archmod.remove"> <title>Remove files from an existing archive</title> <para> Pour supprimer des fichiers d'une archive, vous allez utiliser juste une de fonctions suivante de la classe <classname>File_Archive</classname>: </para> <itemizedlist> <listitem> <funcsynopsis> <funcprototype> <funcdef> <function>remove</function> </funcdef> <paramdef>&amp;$pred</paramdef> <paramdef>$URL</paramdef> </funcprototype> </funcsynopsis> <para> Removes all the files that follow a given predicate from file <varname>$URL</varname>. Note that the URL is the same as in the <function>File_Archive::read</function> function. You can use nested archives. </para> </listitem> <listitem> <funcsynopsis> <funcprototype> <funcdef> <function>removeFromSource</function> </funcdef> <paramdef>&amp;$pred</paramdef> <paramdef>&amp;$source</paramdef> <paramdef>$URL = null</paramdef> </funcprototype> </funcsynopsis> <para> Same as remove, but use <varname>$source</varname> instead of the default file system reader. If no URL is specified, <varname>$source</varname> must be an archive, and the files will be removed from here. </para> </listitem> <listitem> <funcsynopsis> <funcprototype> <funcdef> <function>removeDuplicates</function> </funcdef> <paramdef>$URL</paramdef> </funcprototype> </funcsynopsis> <para> This function will remove all the doublons from the archive at <varname>$URL</varname>; only the most recent file will be kept. </para> <para> If the modification date is not specified for a file, it will be considered infinitely old. </para> <para> If two files have the same modification date, the one that has highest position in the archive (usually the one that was added to the archive at last) will be kept. </para> </listitem> <listitem> <funcsynopsis> <funcprototype> <funcdef> <function>removeDuplicatesFromSource</function> </funcdef> <paramdef>&amp;$source</paramdef> <paramdef>$URL = null</paramdef> </funcprototype> </funcsynopsis> <para> Same as <function>removeDuplicate</function>, but use <varname>$source</varname> instead of the default file system reader. If no URL is specified, the duplicates will be removed from <varname>$source</varname> itself. </para> </listitem> </itemizedlist> <para> Note that those functions will not recursively uncompress archives. </para> <example> <title>Retirer les fichiers Jpg, gif et Bmp d'une archive zip</title> <programlisting role="php"><![CDATA[ <?php require_once "File/Archive.php"; File_Archive::remove( File_Archive::predExtension( array('jpg', 'jpeg', 'bmp', 'gif') ), 'archive.zip' ); ?>]]></programlisting> </example> <example> <title>Remove image files from a nested archive</title> <programlisting role="php"><![CDATA[ <?php require_once 'File/Archive.php'; File_Archive::remove( File_Archive::predMIME( array('image/*') ), 'archive.zip/data.tgz' ); ?>]]></programlisting> </example> </refsect1> <refsect1 id="package.fileformats.file-archive.archmod.add"> <title>Appending files to an archive</title> <para> To append files to an archive, you'll use one of the following functions from <classname>File_Archive</classname> class: </para> <itemizedlist> <listitem> <funcsynopsis> <funcprototype> <funcdef> <function>appender</function> </funcdef> <paramdef>$URL</paramdef> <paramdef><parameter><optional>$unique = null</optional></parameter></paramdef> <paramdef><parameter><optional>$type = null</optional></parameter></paramdef> <paramdef><parameter><optional>$stat = array()</optional></parameter></paramdef> </funcprototype> </funcsynopsis> <para> Allows to append to the archive specified by the URL. </para> <para> If unique is set to <literal>true</literal>, the eventual duplicates created by the insertion of new files will be automatically removed. If set to <literal>null</literal>, the default value (that you can change using <command>File_Archive::setOption('appendRemoveDuplicates', true/false))</command> is used (<literal>false</literal> by default). </para> <para> If the archive does not exist, it will be created using the type specified in parameter (or looking at the extension in the URL if the type is not specified), and the stat array. </para> </listitem> <listitem> <funcsynopsis> <funcprototype> <funcdef> <function>appenderFromSource</function> </funcdef> <paramdef><parameter>&amp;$source</parameter></paramdef> <paramdef><parameter><optional>$URL = null</optional></parameter></paramdef> <paramdef><parameter><optional>$unique = null</optional></parameter></paramdef> <paramdef><parameter><optional>$type = null</optional></parameter></paramdef> <paramdef><parameter><optional>$stat = array()</optional></parameter></paramdef> </funcprototype> </funcsynopsis> <para> Same as appender, but using <varname>$source</varname> instead of the default file system reader. </para> </listitem> </itemizedlist> <para> Both of these functions return a writer that you can use as in the previous examples, using the extract function </para> <example> <title>Add a folder to an archive</title> <programlisting role="php"><![CDATA[ <?php require_once "File/Archive.php"; File_Archive::extract( File_Archive::read('folder'), File_Archive::appender('archive.zip') ); ?>]]></programlisting> </example> <para> Note that if you use a string instead of a writer in a function, the string will be converted using <function>File_Archive::appender</function>. Thus the previous example could be rewritten to: </para> <example> <title>Ajouter un dossier dans une archive</title> <programlisting role="php"><![CDATA[ <?php require_once "File/Archive.php"; File_Archive::extract( $src = 'folder', $dest= 'archive.zip' ); ?>]]></programlisting> </example> </refsect1> </refentry> http://cvs.php.net/viewvc.cgi/peardoc/fr/package/fileformats/file-archive/cache.xml?view=markup&rev=1.1 Index: peardoc/fr/package/fileformats/file-archive/cache.xml +++ peardoc/fr/package/fileformats/file-archive/cache.xml <?xml version="1.0" encoding="utf-8" ?> <!-- $Id: cache.xml,v 1.1 2008/04/15 15:58:54 moosh Exp $ --> <!-- $Revision: 1.1 $ --> <!-- EN-Revision: 1.1 Maintainer: moosh Status: started --> <refentry id="package.fileformats.file-archive.cache"> <refnamediv> <refname>Cache</refname> <refpurpose>Caching compressed files</refpurpose> </refnamediv> <refsect1 id="package.fileformats.file-archive.cache.intro"> <title>Introduction</title> <para> <classname>File_Archive</classname> 1.4 introduit la possibilité d'utiliser un tampon (cache) pour stocker le résultat intermédiare d'une compression zip. Pour cela, on utilise le paquet pear <classname>Cache_Lite</classname>. </para> <para> A zip file is made of compressed files, one after the others. So if you generate an archive that contains files A, B and C and then another archive that contains A and C, you will compress twice the files A and C. The use of the cache will allow to save the compressed version of files A, B and C on the first compression, to use them again in the second compression. </para> </refsect1> <refsect1 id="package.fileformats.file-archive.cache.examples"> <title>Exemples d'utilisation</title> <para> The cache can be (and should be) used if you dynamically create some zip archive that contains frequently the same files. For example, you may want to allow the user to select some images, videos or other files from your gallery and allow them to download a compressed zip archive that contains these files. </para> <para> If you do so without cache, your server will answer very slowly if a lot of users ask the files. With the cache, the files will be compressed only once. </para> <para> On my machine (a thinkpad T42P with default factory equipment), generating a 200MB zip archive takes around 30s of CPU without the cache, 32s of CPU with an empty cache and 2s of CPU if all the files to compress are already in cache. </para> </refsect1> <refsect1 id="package.fileformats.file-archive.cache.howtouse"> <title>How to use the cache</title> <para> The cache is a Cache_Lite object. So you must have installed the package. Then all you have to do is use the <function>File_Archive::setOption</function> function with the <varname>cache</varname> parameter. </para> <example> <title>Set up the cache</title> <programlisting role="php"><![CDATA[ <?php require_once "File/Archive.php"; require_once "Cache/Lite.php"; //Create the cache object $cache = new Cache_Lite( array( //See the doc of Cache_Lite for its constructor parameters ) ); //Ask File_Archive to use the cache object we just created File_Archive::setOption('cache', $cache); //And then create your archives as usual //Generate a file called archive.zip in the working folder File_Archive::extract( 'folderToCompress', 'archive.zip' ); //Send an archive to the user File_Archive::extract( 'folderToCompress', File_Archive::toArchive( 'archive.zip', File_Archive::toOutput() ) ); ?>]]></programlisting> </example> </refsect1> </refentry> http://cvs.php.net/viewvc.cgi/peardoc/fr/package/fileformats/file-archive/examples-gallery.xml?view=markup&rev=1.1 Index: peardoc/fr/package/fileformats/file-archive/examples-gallery.xml +++ peardoc/fr/package/fileformats/file-archive/examples-gallery.xml <?xml version="1.0" encoding="utf-8" ?> <!-- $Id: examples-gallery.xml,v 1.1 2008/04/15 15:58:54 moosh Exp $ --> <!-- $Revision: 1.1 $ --> <!-- EN-Revision: 1.1 Maintainer: moosh Status: started --> <refentry id="package.fileformats.file-archive.examples-gallery"> <refnamediv> <refname>Génération dynamique d'archive pour une gallerie photo</refname> <refpurpose>How to use File_Archive for a photo/video gallery</refpurpose> </refnamediv> <refsect1 id="package.fileformats.file-archive.examples-gallery.intro"> <title></title> <para> One possible use case of <classname>File_Archive</classname> is to dynamically generate archives that contain pictures or videos from a gallery. </para> <para> The choice of the file format is important if you want an efficient generation. Let's see what are the possibilities: </para> <qandaset> <qandaentry> <question><para>Tar</para></question> <answer> <para> <emphasis>Pros</emphasis>: Generation very efficient, constant memory usage, no need to cache </para> </answer> <answer> <para> <emphasis>Cons</emphasis>: No compression (but anyway images or video can hardly be compressed), not as widely used as Zip </para> </answer> </qandaentry> <qandaentry> <question><para>Tgz, Tbz</para></question> <answer> <para> <emphasis>Pros</emphasis>: Very high compression ratio, constant memory usage </para> </answer> <answer> <para> <emphasis>Cons</emphasis>: Can't be cached, needs a lot of CPU at each generation </para> </answer> </qandaentry> <qandaentry> <question><para>Zip</para></question> <answer> <para> <emphasis>Pros</emphasis>: Intermediate result can be cached, compressed, you can choose the compression level, widely used </para> </answer> <answer> <para> <emphasis>Cons</emphasis>: Compression ratio lower than for Tgz/Tbz </para> </answer> </qandaentry> </qandaset> <para> We will focus on Tar and Zip generation, Tgz and Tbz are too CPU expensive for an "on the fly" archive generation. </para> </refsect1> <refsect1 id="package.fileformats.file-archive.examples-gallery.targen"> <title>Tar generation</title> <programlisting role="php"><![CDATA[ <?php require_once "File/Archive.php"; // $files is an array of path to the files that must be added to the archive File_Archive::extract( $files, File_Archive::toArchive( 'myGallery.tar', File_Archive::toOutput() ) ); ?> ]]></programlisting> </refsect1> <refsect1 id="package.fileformats.file-archive.examples-gallery.zipgen"> <title>Zip generation</title> <para> The main advantages of the Zip generation is that it is not very expensive (due to the ability to cache the result), and widely used. I think 2 viable options are to generate uncompressed Zip archives (since you don't reduce a lot the size of picture and video files by compressing them) or to generate compressed Zip archive using a cache system. </para> <example> <title>On the fly creation of an uncompressed ZIP archive</title> <programlisting role="php"><![CDATA[ <?php require_once "File/Archive.php"; File_Archive::setOption('zipCompressionLevel', 0); // $files is an array of path to the files that must be added to the archive File_Archive::extract( $files, File_Archive::toArchive( 'myGallery.zip', File_Archive::toOutput() ) ); ?>]]></programlisting> </example> <example> <title>On the fly creation of a compressed ZIP archive with a cache</title> <programlisting role="php"><![CDATA[ <?php require_once "File/Archive.php"; require_once "Cache/Lite.php"; // See the documentation of cache lite for the meaning of the $options array // fileNameProtection must be left to the default true value // automaticSerialization is not required and should be left to false $options = array('cacheDir' => 'tmp'); File_Archive::setOption('cache', new Cache_Lite($options)); File_Archive::setOption('zipCompressionLevel', 9); // $files is an array of path to the files that must be added to the archive File_Archive::extract( $files, File_Archive::toArchive( 'myGallery.zip', File_Archive::toOutput() ) ); ?>]]></programlisting> </example> </refsect1> <refsect1 id="package.fileformats.file-archive.examples-gallery.puttingtogether"> <title>Putting it all together</title> <para> Since generating a zip or a tar archive is pretty much the same code, you can write a simple code that lets the user choose what format he wants. The following code is taken from a code I really use in my gallery. </para> <example> <title>Custom archive</title> <programlisting role="php"><![CDATA[ <?php $allowedFormats = array('tar', 'zip'); if (!in_array($_GET['type'], $allowedFormats)) { die('Type ' . htmlspecialchars($_GET['type']) . ' is either unknown or not allowed'); } require_once "File/Archive.php"; File_Archive::setOption('zipCompressionLevel', 0); /** * I skipped the generation of the $files array since it really * depends on you gallery and what files the user requires */ File_Archive::extract( $files, File_Archive::toArchive( 'myGallery.' . $_GET['type'], File_Archive::toOutput() ) ); ?>]]></programlisting> </example> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/fr/package/fileformats/file-archive/examples.xml?view=markup&rev=1.1 Index: peardoc/fr/package/fileformats/file-archive/examples.xml +++ peardoc/fr/package/fileformats/file-archive/examples.xml <?xml version="1.0" encoding="utf-8" ?> <!-- $Id: examples.xml,v 1.1 2008/04/15 15:58:54 moosh Exp $ --> <!-- $Revision: 1.1 $ --> <!-- EN-Revision: 1.1 Maintainer: moosh Status: ready --> <refentry id="package.fileformats.file-archive.examples"> <refnamediv> <refname>Exemples</refname> <refpurpose>Quelques exemples simples</refpurpose> </refnamediv> <refsect1 id="package.fileformats.file-archive.examples.extractarchive"> <title>Extraire une archive tar dans sous-répertoire</title> <para> Ici nous allons simplement prendre l'archive tar appelée <filename>archive.tar</filename> et extraire son contenu dans le dossier <filename>output/</filename>. </para> <programlisting role="php"><![CDATA[ <?php require_once "File/Archive.php"; File_Archive::extract('archive.tar/', 'output'); ?> ]]></programlisting> </refsect1> <refsect1 id="package.fileformats.file-archive.examples.archivetostdout"> <title> Envoyer une archive zip contenant le contenu d'une archive tar sur la sortie standard </title> <programlisting role="php"><![CDATA[ <?php require_once "File/Archive.php"; File_Archive::extract( //Le contenu de archive.tar apparaît dans le dossier racine (argument par défaut) 'archive.tar/', //Et est ensuite écrit dans ... File_Archive::toArchive( // ... une archive zip 'archive.zip', // appelée archive.zip File_Archive::toOutput() // qui peut alors être envoyée sur la sortie standard ) ); ?>]]></programlisting> </refsect1> <refsect1 id="package.fileformats.file-archive.examples.extractfile"> <title>Extraire un fichier précis d'une archive</title> <para> Utilisez <function>extract</function> pour récuperer un fichier précis qui se trouve dans une archive. Quand vous donnez le fichier à extraire, assurez-vous d'utiliser le nom de l'archive comme premier niveau de répertoire. </para> <programlisting role="php"><![CDATA[ <?php require_once "File/Archive.php"; File_Archive::extract( 'archive.tar/interne.tgz/fichier.txt', File_Archive::toOutput() ); ?> ]]></programlisting> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/fr/package/fileformats/file-archive/predicates.xml?view=markup&rev=1.1 Index: peardoc/fr/package/fileformats/file-archive/predicates.xml +++ peardoc/fr/package/fileformats/file-archive/predicates.xml <?xml version="1.0" encoding="utf-8" ?> <!-- $Id: predicates.xml,v 1.1 2008/04/15 15:58:54 moosh Exp $ --> <!-- $Revision: 1.1 $ --> <!-- EN-Revision: 1.1 Maintainer: moosh Status: started --> <refentry id="package.fileformats.file-archive.predicates"> <refnamediv> <refname>Predicates</refname> <refpurpose>Filters</refpurpose> </refnamediv> <refsect1 id="package.fileformats.file-archive.predicates.intro"> <title>Introduction</title> <para> <classname>File_Archive</classname> introduces the concept of filters to be able to select the files from a source. A filter is a particular reader that you can create with the <function>File_Archive::filter</function> function. This function requires you to give a predicate. You can build this predicate using the <command>File_Archive::pred*</command> functions. </para> <para> The standard logic predicates are: </para> <itemizedlist> <listitem> <para> <command>predTrue()</command>: always evaluates to true </para> </listitem> <listitem> <para> <command>predFalse()</command>: always evaluates to false </para> </listitem> <listitem> <para> <command>predAnd($p1, $p2, ...)</command>: evaluates to <literal>$p1 &amp;&amp; $p2 &amp;&amp; ...</literal> </para> </listitem> <listitem> <para> <command>predOr($p1, $p2, ...)</command>: evaluates to <literal>$p1 || $p2 || ...</literal> </para> </listitem> <listitem> <para> <command>predNot($p)</command>: evaluates to <literal>!$p</literal> </para> </listitem> </itemizedlist> <para> Some other predicats will help you filtering the files: </para> <itemizedlist> <listitem> <para> <command>predMinSize($size)</command>: keep only the files which size is <literal>&gt;= $size</literal> (in bytes). </para> </listitem> <listitem> <para> <command>predMinTime($time)</command>: keep only the files that have been modified after $time (unix timestamp). </para> </listitem> <listitem> <para> <command>predMaxDepth($depth)</command>: keep only the files that have a public name with less than <varname>$depth</varname> directories. </para> </listitem> <listitem> <para> <command>predExtension($list)</command>: keep only the files with a given extension. <varname>$list</varname> is an array or a comma separated string of allowed extensions. </para> </listitem> <listitem> <para> <command>predEreg($ereg)</command>: keep only the files that have a public name that matches the given regular expression. </para> </listitem> <listitem> <para> <command>predEregi($ereg)</command>: same as <function>predEreg</function>, but the test is case insensitive. </para> </listitem> <listitem> <para> <command>predMIME($mimes)</command>: Select files with a certain MIME type. You can pass an array of types or a string, and use wildcards. </para> </listitem> </itemizedlist> <example> <title>Exemples de filtres</title> <programlisting role="php"><![CDATA[ <?php //Extract all the files that contain an 'a' in their path // or filename from a tar archive File_Archive::extract( File_Archive::filter( File_Archive::predEreg('a'), File_Archive::read( 'archive.tar/', 'folder' ) ), File_Archive::toFiles() ); //Compress a directory to a zip file, including only the files //smaller than 1MB that have changed since last hour File_Archive::extract( File_Archive::filter( File_Archive::predAnd( File_Archive::predNot( File_Archive::predMinSize(1024 * 1024) ), File_Archive::predMinTime(time()-3600) ), File_Archive::read('directory') ), File_Archive::toArchive( 'directory.zip', File_Archive::toFiles() ) ); ?>]]></programlisting> </example> </refsect1> </refentry> http://cvs.php.net/viewvc.cgi/peardoc/fr/package/fileformats/file-archive/readers.xml?view=markup&rev=1.1 Index: peardoc/fr/package/fileformats/file-archive/readers.xml +++ peardoc/fr/package/fileformats/file-archive/readers.xml <?xml version="1.0" encoding="utf-8" ?> <!-- $Id: readers.xml,v 1.1 2008/04/15 15:58:54 moosh Exp $ --> <!-- $Revision: 1.1 $ --> <!-- EN-Revision: 1.1 Maintainer: moosh Status: started --> <refentry id="package.fileformats.file-archive.readers"> <refnamediv> <refname>Readers</refname> <refpurpose>Récuperer les fichiers stocké dans une archive</refpurpose> </refnamediv> <refsect1 id="package.fileformats.file-archive.readers.intro"> <title>Introduction</title> <para> A reader is an object that represents a list of files and directories. Those files can be generated dynamically or exist physically. For example, there is a reader class for a directory, or for each archive format handled by <classname>File_Archive</classname>, and they all have the same interface. </para> <para> To create a reader, you will have to use the <classname>File_Archive</classname> factory. The important function is the <function>read</function> function: </para> <funcsynopsis> <funcprototype> <funcdef> <function>read</function> </funcdef> <paramdef> &type.string; <parameter>$url</parameter> </paramdef> <paramdef> &type.string; <parameter><optional>$symbolic = null</optional></parameter> </paramdef> <paramdef> &type.int; <parameter><optional>$uncompression = 0</optional></parameter> </paramdef> <paramdef> &type.int; <parameter><optional>$directoryDepth = -0</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> In this function the URL will represent what you want to read. </para> <example> <title>Generation of sources</title> <programlisting role="php"><![CDATA[ <?php require_once "File/Archive.php"; /* To read a directory, just give the directory name By default, the directory and all the subdirectories will be parsed (see $directoryDepth to change this) */ $source = File_Archive::read("Path/to/dir"); /* To read from one single file, simply provide the name of the file */ $source = File_Archive::read("Path/to/dir/file.txt"); /* An archive will be considered as a directory if a slash follows This example reads the directory and all the subdirectories of inner/dir contained in archive Path/to/dir/archive.tar This reads all the .txt files in the inner directory of the archive.tar */ $source = File_Archive::read("Path/to/dir/archive.tar/inner/*.txt"); /* If you want to uncompress the archive (read all its content) Note: if you ommit the trailing /, the archive will be treated as a single file */ $source = File_Archive::read("Path/to/dir/archive.tar/"); ?> ]]></programlisting> </example> <para> The <literal>symbolic</literal> attribute says how the files read will be displayed for future use. If <varname>$URL</varname> is a directory, <varname>$URL</varname> will be replaced by <varname>$symbolic</varname> (or <literal>''</literal> if <varname>$symbolic</varname> is <literal>null</literal>). So, in our first example, the files will be displayed as if the current directory was <literal>'Path/to/dir'</literal>: Since by default <varname>$symbolic</varname> is empty, <literal>Path/to/dir</literal> will be simply removed from the file. You may want to put <literal>Path/to/dir</literal> as <varname>$symbolic</varname> to keep the full path <literal>Path/to/dir</literal>. </para> <para> If <varname>$URL</varname> is a file, then only the filename will be kept, and <varname>$symbolic</varname> will be added to it. So, in our second example, the source contains a file with symbolic name <literal>file.txt</literal>. If a symbolic name <literal>foo</literal> had been specified, the source would contain <literal>foo/file.txt</literal>. </para> <para> The <varname>$uncompression</varname> parameter indicate how many files will be uncompressed while parsing the tree to files. By default the files are not uncompressed. So, if you do <command>File_Archive::read('archive.tar/inner/dir', 'inner/dir')</command>, and if <filename>archive.tar</filename> contains a file called <filename>archive.tar/inner/dir/file.tgz</filename>, this second archive will appear as a file and not as a directory. It won't be uncompressed because <varname>$uncompression</varname> is <literal>0</literal>. If <varname>$uncompression</varname> is set to <literal>1</literal>, <filename>file.tgz</filename> would appear as a directory, but the files inside this archive would not be uncompressed. If <varname>$uncompression</varname> is set to <literal>-1</literal>, all the files would be uncompressed, regardless of the depth. </para> <note> <para> The compressed files that may appear in <varname>$URL</varname> are not taken into account by <varname>$uncompression</varname> variable. </para> </note> <para> The <varname>$directoryDepth</varname> parameter gives a limit to the number of directory read by the reader. </para> </refsect1> <refsect1 id="package.fileformats.file-archive.readers.multireader"> <title>Multi readers</title> <para> Using a multi reader, you can make several sources appear as one. You can create a multi reader using the <command>File_Archive::readMulti()</command> function. </para> <example> <title>Multi reader</title> <programlisting role="php"><![CDATA[ <?php //This reader contains the content of directory and archive.tar $source = File_Archive::readMulti( array( File_Archive::read('directory'), File_Archive::read('archive.tar/') ) ); ?>]]></programlisting> </example> </refsect1> <refsect1 id="package.fileformats.file-archive.readers.readingcontent"> <title>Reading the content of a data reader</title> <para> Any reader provides the following interface: </para> <itemizedlist> <listitem> <para><command>function next()</command></para> <para> Go to the next file in the source. Returns <literal>false</literal> when the end of the archive is reached. </para> </listitem> <listitem> <para><command>function getFilename()</command></para> <para> Returns the filename of the currently selected entry in the archive. </para> </listitem> <listitem> <para><command>function getStat()</command></para> <para> Returns the stat as the <function>stat</function> function does This function may not return a complete array, it may even return <literal>array()</literal>. </para> </listitem> <listitem> <para><command>function getDataFilename()</command></para> <para> For optimisation purposes: if the source is a physical file, this function returns the name of the file; otherwise it returns <literal>null</literal>. </para> </listitem> <listitem> <para><command>function getData($length = -1)</command></para> <para> Reads some data from the source. This function will return a string which size is determined by the smallest of <itemizedlist> <listitem> <para> <varname>$length</varname> if <varname>$length</varname> <literal>&gt;= 0</literal> </para> </listitem> <listitem> <para> the end of the file. </para> </listitem> </itemizedlist> If the end of the file is reached, the function will return <literal>null</literal>. </para> </listitem> <listitem> <para><command>function skip($length)</command></para> <para> Equivalent to <command>getData($length)</command>, but does not return any data. Depending on the data reader, this function can be far more efficient than <function>getData</function>. </para> </listitem> <listitem> <para><command>function close()</command></para> <para> Should be called after having used the data reader (closes the file handles...). This function moves the object in the same state as it was before the first call to <function>next</function>. After this call, you can iterate again on the data reader. </para> </listitem> </itemizedlist> <example> <title>Listing the content of a data reader</title> <programlisting role="php"><![CDATA[ <?php $source->close(); //Move back to the begining of the source while($source->next()) { echo $source->getFilename() . "<br/>\n"; } ?>]]></programlisting> </example> </refsect1> <refsect1 id="package.fileformats.file-archive.readers.functionsusing"> <title>Functions that use readers</title> <para> All <classname>File_Archive</classname> functions that take a reader as an argument also accept strings and arrays. The strings will be automatically interpreted as a reader using <command>File_Archive::read</command> function. The arrays will be interpreted as a multi reader. </para> <para> Since the readers are passed by reference, you will have to pass a variable and not the raw string or array. </para> <para> It is thus possible to rewrite the previous example like that: </para> <example> <title>Multi reader</title> <programlisting role="php"><![CDATA[ <?php //This reader contains the content of directory and archive.tar File_Archive::extract( array( 'directory', 'archive.tar/' ), File_Archive::toArchive('test.zip') ); ?>]]></programlisting> </example> </refsect1> </refentry> http://cvs.php.net/viewvc.cgi/peardoc/fr/package/fileformats/file-archive/writers.xml?view=markup&rev=1.1 Index: peardoc/fr/package/fileformats/file-archive/writers.xml +++ peardoc/fr/package/fileformats/file-archive/writers.xml <?xml version="1.0" encoding="utf-8" ?> <!-- $Id: writers.xml,v 1.1 2008/04/15 15:58:54 moosh Exp $ --> <!-- $Revision: 1.1 $ --> <!-- EN-Revision: 1.1 Maintainer: moosh Status: started --> <refentry id="package.fileformats.file-archive.writers"> <refnamediv> <refname>Writers</refname> <refpurpose>Sauvegarder une archives</refpurpose> </refnamediv> <refsect1 id="package.fileformats.file-archive.writers.intro"> <title>Introduction</title> <para> A writer is an object that deals with data. Some writers transform data (this is the case of the archive writers), some save them to disk (for files writers), or to memory (for the memory writer)... They all implement the same interface. </para> <para> You can transfer data from a reader to a writer using the <command>File_Archive::extract</command> function. </para> <para> All the writers can be created thanks to the <classname>File_Archive</classname> factory, and more particularly the <command>File_Archive::to*</command> functions. </para> </refsect1> <refsect1 id="package.fileformats.file-archive.writers.writearchives"> <title>Write archives</title> <para> To create a writer that will generate an archive, use the <function>toArchive</function> function: </para> <funcsynopsis> <funcprototype> <funcdef> <function>toArchive</function> </funcdef> <paramdef> &type.string; <parameter>$filename</parameter> </paramdef> <paramdef> <parameter>&amp;$innerWriter</parameter> </paramdef> <paramdef> <parameter><optional>$type = null</optional></parameter> </paramdef> <paramdef> <parameter><optional>$stat = array()</optional></parameter> </paramdef> <paramdef> <parameter><optional>$autoClose = true</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <itemizedlist> <listitem> <para> <varname>$filename</varname> is the name of the generated archive </para> </listitem> <listitem> <para> <varname>$innerWriter</varname> is another writer in which the archive file will be written </para> </listitem> <listitem> <para> <varname>$type</varname> is one of <literal>Tar</literal>, <literal>Gzip</literal> or <literal>Zip</literal> and indicates the format of compression. If not specified, the type is determined thanks to the extension of the filename. </para> </listitem> <listitem> <para> <varname>$stat</varname> is an optional array to indicate stats about the archive (see the PHP stat function for the possible indexes). </para> </listitem> <listitem> <para> <varname>$autoClose</varname> indicates whether the inner writer will be closed once the data are sent. It may be useful not to close the writer if you want to append some more data after writing. In general, you won't need to keep the writer open, so you should just keep the default value. </para> </listitem> </itemizedlist> <example> <title>Generation of archive writers</title> <programlisting role="php"><![CDATA[ <?php require_once "File/Archive.php"; /* Writer to a tar file */ File_Archive::toArchive("archive.tar", $innerWriter); /* Writer to a tar.gz file */ File_Archive::toArchive("archive.tgz", $innerWriter); /* Writer to a zip file */ File_Archive::toArchive("archive.zip", $innerWriter); ?>]]></programlisting> </example> </refsect1> <refsect1 id="package.fileformats.file-archive.writers.writefiles"> <title>Write to files</title> <para> A writer can write the files to physical files. To create such a writer, call <command>File_Archive::toFiles();</command>. If a directory does not exist, it will be automatically created. </para> <example> <title>Use files writer</title> <programlisting role="php"><![CDATA[ <?php require_once "File/Archive.php"; /* Copy a whole directory to another location */ File_Archive::extract( File_Archive::read('Path/to/dir', 'new/directory'); File_Archive::toFiles() ); /* Convert an archive to another format: tgz to zip */ File_Archive::extract( File_Archive::read('archive.tgz/'), File_Archive::toArchive( 'archive.zip', File_Archive::toFiles() ) ); ?>]]></programlisting> </example> </refsect1> <refsect1 id="package.fileformats.file-archive.writers.sendmails"> <title>Send emails</title> <para> You can send emails as attachment using a mail writer available thanks to <command>File_Archive::toMail</command> function. </para> <funcsynopsis> <funcprototype> <funcdef> <function>toMail</function> </funcdef> <paramdef> &type.array; <parameter>$to</parameter> </paramdef> <paramdef> &type.array; <parameter>$headers</parameter> </paramdef> <paramdef> &type.string; <parameter>$message</parameter> </paramdef> <paramdef> <parameter><optional>&amp;$mail = null</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> This function relies on the PEAR <classname>Mail</classname> and <classname>Mail_Mime</classname> libraries, and the parameters are the same as the one of these classes: </para> <itemizedlist> <listitem> <para> <varname>$to</varname> an array or a string with comma separated recipients </para> </listitem> <listitem> <para> <varname>$headers</varname> will be sent to <classname>Mail_Mime</classname> and to <varname>$mail</varname>: an associative array of headers. The header name is used as key and the header value as value. </para> </listitem> <listitem> <para> <varname>$message</varname> is the text version of the body of the mail. You can provide an HTML version thanks to the <function>setHTMLBody</function> and <function>addHTMLImage</function> of the writer. The signatures of these functions are the same as the ones of <classname>Mail_Mime</classname>. </para> </listitem> <listitem> <para> <varname>$mail</varname> the way to send mail. This is an object created with the <function>Mail::factory</function> function. If <literal>null</literal>, <command>Mail::factory('mail')</command> will be used (and the email will be sent using the PHP mail function). </para> </listitem> </itemizedlist> <example> <title></title> <programlisting role="php"><![CDATA[ <?php require_once "File/Archive.php"; /* Send the files in the current directory (no recursion) as attachment */ File_Archive::extract( File_Archive::read('Path/to/dir', '', 0, 0), File_Archive::toMail( $to, // recipients array( 'Subject' => 'Path/to/dir directory', 'From' => 'address@of.expeditor' ), 'Find all the files attached' // body ) ); ?>]]></programlisting> </example> </refsect1> <refsect1 id="package.fileformats.file-archive.writers.stdout"> <title>Send files to the user</title> <para> To send files to the remote user (i.e. write data to the standard output), you need a special writer. You can build one calling function <function>File_Archive::toOutput</function>. </para> <para> This writer will automatically send a header forcing the download of the file. </para> <para> If you don't want that, call <command>File_Archive::toOutput(false)</command>. </para> </refsect1> <refsect1 id="package.fileformats.file-archive.writers.multiwriters"> <title>Multi writers</title> <para> Using a multi writer, you can write the data to two or more different locations in parallel. </para> <para> A typical use is to send the file to the user at the same time as you write it to a file. </para> <para> It can also be used to generate archives in different formats. </para> <para> You can create a multi writer using <command>File_Archive::toMulti($dest1, $dest2)</command>. </para> <example> <title>Multi writer</title> <programlisting role="php"><![CDATA[ <?php //Send a directory to the user and to a file File_Archive::extract( File_Archive::read('directory'), File_Archive::toArchive( 'multi.zip', File_Archive::toMulti( File_Archive::toOutput(), File_Archive::toFiles() ) ) ); ?> ]]></programlisting> </example> </refsect1> <refsect1 id="package.fileformats.file-archive.writers.writing"> <title>Writing to a writer</title> <para> It is also possible to write data directly to a writer, without using a reader. To do so, you can use the following interface implemented by any writer: </para> <itemizedlist> <listitem> <para><command>function newFile($URL, $stat)</command></para> <para> Create a new file in the writer. </para> <para> <varname>$URL</varname> is the name of the file, <varname>$stat</varname> is an array of statistics about the data (see the PHP <function>stat</function> function for more information). </para> <para> The stat array may not contain all the information. The only index that must be present is index 7 (size of the data). </para> </listitem> <listitem> <para><command>function writeData($data)</command></para> <para> Append the specified data to the writer. A call to <function>newFile</function> must have been done previously. </para> </listitem> <listitem> <para><command>function close()</command></para> <para> Close the writer, eventually flush the data, write the footer... This function must be called before the end of the file, otherwise some data may not be treated by the writer. </para> </listitem> </itemizedlist> <example> <title>Dynamic creation of a zip file</title> <programlisting role="php"><![CDATA[ <?php require_once "File/Archive.php"; $dest = File_Archive::toArchive("foo.zip", File_Archive::toFiles()); $dest->newFile("even.txt"); for($i=0; $i<100; $i++) $dest->writeData((2*$i)."\n"); $dest->newFile("odd.txt"); for($i=0; $i<100; $i++) $dest->writeData((2*$i+1)."\n"); $dest->close(); ?>]]></programlisting> </example> <note> <para> If you do not specify the stat array in the <function>newFile</function> function, the majority of the archives will have to buffer the data until the end of the file is reached (this is because the size of the file is usually needed to be able to write the header). </para> <para> This may be a memory problem if you want to generate really large files. </para> </note> </refsect1> <refsect1 id="package.fileformats.file-archive.writers.functionsusing"> <title>Functions that use writers</title> <para> All <classname>File_Archive</classname> functions that take a writer as an argument also accept strings and arrays. The strings will be automatically interpreted as a writer using <function>File_Archive::appender</function> function. The arrays will be interpreted as a multi writer. </para> <para> Since the writers are passed by reference, you will have to pass a variable and not the raw string or array. </para> <para> It is thus possible to rewrite the previous example like that: </para> <example> <title>Multi writer</title> <programlisting role="php"><![CDATA[ <?php //Send a directory to the user and to a file File_Archive::extract( $src = 'directory', File_Archive::toArchive( 'multi.zip', array( File_Archive::toOutput(), File_Archive::toFiles() ) ) ); ?>]]></programlisting> </example> </refsect1> </refentry>
« previous php.pear.doc (#10668) next »