Tag Archives: extract

Ubuntu Server: How to Extract a .tar.gz Archive File Tips, Tricks and Tutorials 30 OCT 2014

An old one, but for my own reference purposes, worth noting down here in these pages. In order to extract the contents of a .tar.qz archive file, turn to the tar command (“man tar” for options as per usual):

tar -xvzf archivefile.tar.gz -C /home/craig/downloads

In this example we’ve extracted the contents of archivefile.tar.gz to the /home/craig/downloads directory. (If we left out the -C directive then the operation would simply have extracted the archive contents to the working directory.)

To explain the archivefile.tar.gz file itself, essentially tar collected all the files into one package followed by a call to gzip which then compressed the tar file. (This is often referred to as a tarball in the Linux world.)

To extract the files making up this tarball, we used the following parameters in our call to tar:

x: tar can collect files or extract them. x does the latter.
v: makes tar talk a lot. Verbose output shows you all the files being extracted.
z: tells tar to decompress the archive using gzip
f: this must be the last flag of the command, and the tar file must be immediately after. It tells tar the name and path of the compressed file.

Obviously -v isn’t necessarily, but it is helpful to see what is going on. So in summary, to extract a .tar.gz file, run:

tar -xvzf [archiveFile].tar.gz

Noted.

ubuntu-logo-banner

PHP Function to Extract an E-mail from a String Programming 31 MAR 2013

kung-fu-php-logoGiven a string, it would often be quite useful to extract any e-mail addresses that it might contain. Luckily for us, PHP makes this fairly trivial through the use of its powerful filter_var functionality.

Essentially the plan of attack is to take the string, tokenize it by breaking it up on the space character (or whatever other delimiters you wish to use), and then loop through each one of those tokens and check if it matches a recognised email address pattern.

Putting all of this together, we would get a function which looks a little like this:

function email_address_extraction($message){
    foreach(preg_split('/ /', $message) as $token) {
        $email = filter_var(filter_var($token, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);
        if ($email !== false) {
            $emails[] = $email;
        }
    }
    return $emails;
}

To test:

$testmessage = "hello world.  can jon.kabal@image.com, craig.lot+blog@google.com, and roar@hype.com come over   and play?";
$result = email_address_extraction($testmessage);
print_r($result);
$testmessage = " craiglot@doe.co.za";
$result = email_address_extraction($testmessage);
print_r($result);

Nifty.

PHP: Extract a Zip File’s Contents CodeUnit 09 DEC 2009

PHP-logoThe other day I needed to automate the unzipping and processing of files contained within a zip archive. Using the existing PHP Zip library and some help off the web, this is what I was left with:

(Note, you need to enable php_zip.dll extension within php.ini in order to use the code below)

$zip = zip_open($zipfilename);
if ($zip) {
while ($zip_entry = zip_read($zip)) {
$fp = fopen($targetdir . zip_entry_name($zip_entry), "w");
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp,"$buf");
zip_entry_close($zip_entry);
fclose($fp);
}
}
zip_close($zip);

Nifty.

PHP Code to Extract Attachments from Mail Files CodeUnit 08 DEC 2009

PHP-logoFor a recent project I needed to parse .mai mail files to locate any Excel or Zip files that may have been sent through as attachments. The following block of code is the result of my work:


Obviously by extending the content types being searched on with a simple or statement, you can alter the script to extract just about any type of attachment, making it quite useful indeed.

It’s fast and appears to work pretty nicely, so hopefully it can be of some use to you then! :)