Compressing files with python, perl, ruby and php

Compressing files

is one of the most common tasks any programmer would have done. The below is a simple comparison of compression paradigms of selected programming languages i have really liked!

This post is much influenced by :

Senario : A list called 'songs' with file paths to different songs in the HDD which needs to be compressed to a single zip file.

Ok let the fight begin!

First up is python :


import zipfile

""" Open a file called to write """ 
with zipfile.ZipFile('', 'w') as zipit:
    """ zipit! """
    for song in songs:

Lets compress it in perl :


use strict;
use warnings;
use Archive::Zip;

my $compressor = Archive::Zip->new();

foreach my $song (@songs)


Ok it's ruby's turn :


require 'rubygems'
require 'zip/zip''', Zip::ZipFile::CREATE) {
        zipit.get_output_stream(song) { 
            |f| f.puts open(url.chomp).read 

PHP you may zip it!


$zip = new ZipArchive();
$zip->open('', ZIPARCHIVE::CREATE);
foreach($songs as $song)


Indeed there will be better ways of doing the same! Do share it!

Share this