Tag Archives: archive

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

7zip, Zip, and RAR archive support in Ubuntu Server Tips, Tricks and Tutorials 12 DEC 2013

ubuntu-10-logoUnfortunately the Ubuntu server by default doesn’t come with support for .7z, .zip and .rar archive formats installed, meaning that if you want to work with one of these archives then you’re going to have to add it.

Luckily there are packages available to do the work for you, meaning that you simply need to follow the standard install procedure and you should be good to go:

sudo apt-get install p7zip-full
sudo apt-get install unrar-free

Quick and easy wasn’t it? :)

PHP: Easy Function to Create a ZIP File Archive CodeUnit 22 AUG 2011

Borrowing from the always brilliant Mr. David Walsh, here is a great little function to create a zip file archive of a given set of files, ready for download from your site.

First, the zip file create function which will be doing all the heavy lifting:

function create_zip($files = array(), $destination = '', $overwrite = false) {
    //if the zip file already exists and overwrite is false, return false
    if (file_exists($destination) && !$overwrite) {
        return false;
    }
    //vars
    $valid_files = array();
    //if files were passed in...
    if (is_array($files)) {
        //cycle through each file
        foreach ($files as $file) {
            //make sure the file exists
            if (file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }
    //if we have good files...
    if (count($valid_files)) {
        //create the archive
        $zip = new ZipArchive();
        if ($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }
        //add the files
        foreach ($valid_files as $file) {
            $zip->addFile($file, basename($file));
        }
        //debug
        //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
        //close the zip -- done!
        $zip->close();

        //check to make sure the file exists
        return file_exists($destination);
    } else {
        return false;
    }
}

Right, now that we have the function, let us set about putting it to use. First, we’ll need an array of fully qualified files to use, for example:

$filestozip = array(
'/var/www/uploads/file1.jpg',
'/var/www/uploads/file2.jpg',
'/var/www/example3.html'
);

Now we simple run the function, providing it with our array of files to be included, the proposed name of the archive we want to generate, as well as whether or not we should overwrite if it happens to already exist. And if the function returns true, then we can go ahead and link to our newly generated ZIP archive!

$result = create_zip($filestozip,'download.zip',true);
if ($result == true){
echo 'Download!';
}

Nifty.

What is a .cbr or .cbz File? Comic Books | Software & Sites 15 JUL 2011

You might have come across .cbr or .cbz files during one of your many adventures of trawling through the Internet. What are these mysterious files that Windows doesn’t seem to be able to identify?

Simply put, they are what are know as Comic Book Archive files, something that has become a sort of a de facto standard when it comes to putting out scanned comic books onto the web. Basically all a .cbr or .cbz file is, is a stardard ZIP or RAR archive file (the ‘z’ or ‘r’ at the back gives away which one) containing a number of image files.

These file types are automatically opened by so called comic or manga viewers, and are thus a very useful way for keeping comic books consisting of multiple scanned images in a rather organised fashion.

The Wikipedia entry on the matter lists some useful comic book readers in case you are interested in delving deeper into this little world.

And now you know.

Related Link: http://en.wikipedia.org/wiki/Comic_Book_Archive_file

Ubuntu Server: Maximum Compression of a file Using 7za CodeUnit 28 NOV 2010

Just a quick one-liner to remind me how to ultra compress a file using 7za instead of the bog standard gzip which I always seem to automatically turn to because its the only one I can remember out of my head!

Anyway, the command to ultra compress a single file is:

7za a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on archiveName.7z filetocompress.ext

where the -t7z indicates a 7z archive, -m0=lzma indicates lzma compression method, -mx=9 dicates compression level (9 is ultra), -mfb=64 is the number of fast bytes for LZMA, -md=32m allows for a 32mb dictionary size and -ms=on indicates a solid archive.

Using the above I managed to compress a 285mb SQL dump to a insignificant 7.4mb archived file!

Nifty.

Ubuntu Terminal: How to Quickly GZip a File CodeUnit 01 JUL 2010

File archiving is a particularly simple affair and there are quite a few different archive algorithms to pick and choose from. GZip (.gz) has for long been associated with Linux as one of its main archiving algorithms and so today’s quick command line tip highlights how to quickly create a zipped file on your system.

Simply enter the following to gzip your file:

gzip [filenameToZip] -c -v > [zippedFileName]

Conversly, to unzip a file is as simple as entering:

gunzip [zippedFileName] -c -v > [unzippedFileName]

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.