The Archive_Tar object also allows you to extract the contents of a compressed (or uncompressed) TAR file with its extract() method, which accepts the target directory as argument. Consider the following example, which demonstrates by extracting the contents of the named archive to the temporary directory:
A quick look in "/tmp/xfiles" after running the script above will show you that the files have been successfully extracted.
/tmp/xfiles/uudeview-0.5.18: ... snip ... Note that if the target directory does not exist, Archive_Tar will attempt to create it for you. You can also selectively extract certain files from the source archive with the Archive_Tar object's extractList() method, which additionally accepts a list of the file names to be pulled out of the archive. Here's an example:
The example above would extract only the files "configure" and "install-sh" from the TAR archive into the target directory.
/tmp/xfiles/uudeview-0.5.18: Note that you will have to specify the full path (within the archive) to the files to be extracted in order to successfully use the extractList() function. If the path to any of the files specified in the file list is incorrect, extractList() will simply skip over that file. {mospagebreak title=Absolute Power}For users who need more fine-grained control over the archive creation and extraction process, Archive_Tar offers the createModify(), addModify() and What does this mean? Consider the following simple directory structure:
Now, if you were to create an archive of these files using the create() method, as in the script below,
the resulting TAR file would look (and be extracted) like this:
What the functions above let you do is control the directory structure inside the TAR archive, and thereby determine the output when the archive is exploded. So, if you wanted (for example) to replace the outer directory "tempstuff/" in the archive above with something more descriptive, like "menutools-0.51/scripts/", you could use the createModify() method instead, as in the script below:
The only difference between this script and previous ones: this one uses the createModify() method instead of the create() method, and passes that method two additional arguments. The first of these arguments is the directory path to be added to each file ("menutools-0.51/scripts/" in this example); the second is the directory to be amputated when packing the files into the archive ("tempstuff/" in this example). Now, if you attempt to explode the resulting TAR archive (either using the "tar" command or the Archive_Tar extract() method), the addition of the custom path above will result in the files within the archive being placed in a "menutools-0.51/scripts/" directory in your extraction area.
In a similar manner, when using the extractList() function, there may arise a situation when you don't want the directory structure maintained within the TAR file to be preserved during the extraction of specific files. In such a case, you can tell Archive_Tar to ignore the relative path and directory tree hierarchy associated with each file, by adding a third, optional, parameter to extractList(). In the following example, even though the files within the archive are organized in a particular hierarchical structure, the third argument to
The addModify() method lets you do something similar, but with an existing archive. Look in the class documentation for more examples of how this feature can be used.
blog comments powered by Disqus |
|
|
|
|
|
|
|