---
title: "Compressing Files With Python, Perl, Ruby and Php"
description: "is one of the most common tasks any programmer would have done. The below is a simple comparison of compression paradigms."
url: "https://h3manth.com/scribe/compressing-files-python-perl-ruby-and-php/"
canonical_url: "https://h3manth.com/scribe/compressing-files-python-perl-ruby-and-php/"
markdown_url: "https://h3manth.com/scribe/blog/compressing-files-python-perl-ruby-and-php.md"
publishDate: "2011-08-24T00:00:00.000Z"
author: "Hemanth HM"
category: "python"
tags: ["ruby","python","bash","web"]
word_count: 188
reading_time_minutes: 1
x_markdown_tokens: 546
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Compressing Files With Python, Perl, Ruby and Php

> is one of the most common tasks any programmer would have done. The below is a simple comparison of compression paradigms.

## 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 :

  * [comparative study](http://www.h3manth.com/content/downloading-files-comparative-study)
  * [Methods to post to a form](http://h3manth.com/content/methods-submit-form-post-using-curl-perl-python-ruby-lynx)

**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 :**
    
    
    #!/usr/bin/python
    
    import zipfile
    
    """ Open a file called album.zip to write """ 
    with zipfile.ZipFile('album.zip', 'w') as zipit:
        """ zipit! """
        for song in songs:
            zipit.write(song)
    

**Lets compress it in perl :**
    
    
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use Archive::Zip;
    
    my $compressor = Archive::Zip->new();
    
    foreach my $song (@songs)
    {
           $compressor->addFile($song);
    }
    
    $compressor->writeToFileNamed('album.zip');
    

**Ok it's ruby's turn :******
    
    
     #!/usr/bin/ruby
    
    require 'rubygems'
    require 'zip/zip'
    
    
    Zip::ZipFile.open('album.zip', Zip::ZipFile::CREATE) {
            |zipit|
            zipit.get_output_stream(song) { 
                |f| f.puts open(url.chomp).read 
            }
    }
    

**PHP you may zip it!**
    
    
    <?php
    
    $zip = new ZipArchive();
    $zip->open('album.zip', ZIPARCHIVE::CREATE);
    foreach($songs as $song)
    {
        $zip->addFile($song,$song);
    }
    $zip->close();
    
    ?>
    

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