<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: gzip code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 11 Oct 2008 10:48:05 GMT</pubDate>
    <description>DZone Snippets: gzip code</description>
    <item>
      <title>Decompress several filetypes with a single script</title>
      <link>http://snippets.dzone.com/posts/show/5785</link>
      <description>// Decompresses Z, gz, bz2, zip, rar, tar, and 7z with a single 'decompress' command&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;&lt;br /&gt;# decompress - will decompress a file, regardless of compression type.&lt;br /&gt;&lt;br /&gt;Z="compress -d"&lt;br /&gt;gz="gunzip"&lt;br /&gt;bz="bunzip2"&lt;br /&gt;zip="unzip -qo"&lt;br /&gt;rar="unrar x -id -y"&lt;br /&gt;tar="tar xf"&lt;br /&gt;7z="p7zip -d"&lt;br /&gt;&lt;br /&gt;if [ $# -eq 0 ]; then&lt;br /&gt;    echo "Usage: decompress file or files to decompress"&gt;&amp;2&lt;br /&gt;    exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;for name&lt;br /&gt;do&lt;br /&gt;	if [ ! -f "$name" ] ; then&lt;br /&gt;		echo "$0: file $name not found. Skipped." &gt;&amp;2&lt;br /&gt;		continue&lt;br /&gt;	fi&lt;br /&gt;&lt;br /&gt;	if [ "$(echo $name | egrep '(\.Z$|\.gz$|\.bz2$|\.zip$|\.rar$|\.tar$|\.tgz$|\.7z$)')" = "" ] ; then&lt;br /&gt;		echo "Skipped file ${name}: it's already decompressed." &lt;br /&gt;      		continue&lt;br /&gt;	fi&lt;br /&gt;&lt;br /&gt;	extension=${name##*.}&lt;br /&gt;&lt;br /&gt;	case "$extension" in&lt;br /&gt;		Z ) echo "Filetype is Z. Decompressing..."&lt;br /&gt;		    $Z "$name"&lt;br /&gt;		    ;;&lt;br /&gt;		gz ) echo "Filetype is gz. Decompressing..."&lt;br /&gt;		     $gz "$name"&lt;br /&gt;		     ;;&lt;br /&gt;		bz2 ) echo "Filetype is bz2. Decompressing..."&lt;br /&gt; 		     $bz "$name"&lt;br /&gt;		     ;;&lt;br /&gt;		zip ) echo "Filetype is zip. Decompressing..."&lt;br /&gt;		      $zip "$name"&lt;br /&gt;		      ;;&lt;br /&gt;		rar ) echo "Filetype is rar. Decompressing..."&lt;br /&gt;		      $rar "$name"&lt;br /&gt;		      ;;&lt;br /&gt;		tar ) echo "Filetype is tar. Decompressing..."&lt;br /&gt;              	      $tar "$name"&lt;br /&gt;              	      ;;&lt;br /&gt;		tgz ) echo "Filetype is tgz. Decompressing..."&lt;br /&gt;              	      $tar "$name"&lt;br /&gt;                     ;;&lt;br /&gt;               7z ) echo "Filetype is 7z. Decompressing..."&lt;br /&gt;                     $7z "$name"&lt;br /&gt;	esac&lt;br /&gt;&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;exit 0&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 18 Jul 2008 02:07:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5785</guid>
      <author>zdenton (Zach Denton)</author>
    </item>
    <item>
      <title>gzip pipe</title>
      <link>http://snippets.dzone.com/posts/show/5644</link>
      <description>#! /usr/bin/python&lt;br /&gt;&lt;br /&gt;from gzip import GzipFile&lt;br /&gt;from StringIO import StringIO&lt;br /&gt;&lt;br /&gt;class GZipPipe(StringIO) :&lt;br /&gt;    """This class implements a compression pipe suitable for asynchronous &lt;br /&gt;    process.&lt;br /&gt;&lt;br /&gt;    Only one buffer of data is read/compressed at a time.&lt;br /&gt;    The process doesn't read the whole file at once : This improves performance&lt;br /&gt;    and prevent hight memory consumption for big files."""&lt;br /&gt;&lt;br /&gt;    # Size of the internal buffer&lt;br /&gt;    CHUNCK_SIZE = 1024 &lt;br /&gt;&lt;br /&gt;    def __init__(self, source = None, name = "data") :&lt;br /&gt;        """Constructor&lt;br /&gt;        &lt;br /&gt;        @param source   Source data to compress (as a stream/File/Buffer - anything with a read() method)&lt;br /&gt;        @param name     Name of the data within the zip file"""&lt;br /&gt;        &lt;br /&gt;        # Source file&lt;br /&gt;        self.source = source&lt;br /&gt;&lt;br /&gt;        # OEF reached for source ?&lt;br /&gt;        self.source_eof = False&lt;br /&gt;&lt;br /&gt;        # Buffer&lt;br /&gt;        self.buffer = ""&lt;br /&gt;&lt;br /&gt;        StringIO.__init__(self)&lt;br /&gt;&lt;br /&gt;        # Inherited constructor&lt;br /&gt;&lt;br /&gt;        # Init ZipFile that writes to us (the StringIO buffer)&lt;br /&gt;        self.zipfile = GzipFile(name, 'wb', 9, self)&lt;br /&gt;    &lt;br /&gt;    def write(self, data) :&lt;br /&gt;        """The write mzthod shouldn't be called from outside.&lt;br /&gt;        A GZipFile was created with this current object as a output buffer anbd it &lt;br /&gt;        fills it whenever we write to it (calling the read method of this object will do it for you)&lt;br /&gt;        """&lt;br /&gt;        &lt;br /&gt;        self.buffer += data&lt;br /&gt;&lt;br /&gt;    def read(self, size = -1) :&lt;br /&gt;        """Calling read() on a zip pipe will suck data from the source stream.&lt;br /&gt;&lt;br /&gt;        @param  size Maximum size to read - Read whole compressed file if not specified.&lt;br /&gt;        @return Compressed data"""&lt;br /&gt;&lt;br /&gt;        # Feed the zipped buffer by writing source data to the zip stream&lt;br /&gt;        while ((len(self.buffer) &lt; size) or (size == -1)) and not self.source_eof :&lt;br /&gt;           &lt;br /&gt;            # No source given in input&lt;br /&gt;            if self.source == None: break&lt;br /&gt;&lt;br /&gt;            # Get a chunk of source data&lt;br /&gt;            chunk = self.source.read(GZipPipe.CHUNCK_SIZE)&lt;br /&gt;            &lt;br /&gt;            # Feed the source zip file (that fills the compressed buffer)&lt;br /&gt;            self.zipfile.write(chunk)&lt;br /&gt;            &lt;br /&gt;            # End of source file ?&lt;br /&gt;            if (len(chunk) &lt; GZipPipe.CHUNCK_SIZE) :&lt;br /&gt;                self.source_eof = True&lt;br /&gt;                self.zipfile.flush()&lt;br /&gt;                self.zipfile.close()&lt;br /&gt;                break&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        # We have enough data in the buffer (or source file is EOF): Give it to the output&lt;br /&gt;        if size == 0:&lt;br /&gt;            result = ""&lt;br /&gt;        if size &gt;= 1 :&lt;br /&gt;            result = self.buffer[0:size]&lt;br /&gt;            self.buffer = self.buffer[size:]&lt;br /&gt;        else : # size &lt; 0 : All requested&lt;br /&gt;            result = self.buffer&lt;br /&gt;            self.buffer = ""&lt;br /&gt;&lt;br /&gt;        return result&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Sun, 15 Jun 2008 08:50:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5644</guid>
      <author>cdvddt (Raphael Jolivet)</author>
    </item>
    <item>
      <title>nginx gzip config</title>
      <link>http://snippets.dzone.com/posts/show/5385</link>
      <description>&lt;code&gt;&lt;br /&gt;  # output compression saves bandwidth &lt;br /&gt;  gzip              on;&lt;br /&gt;  gzip_proxied      any;&lt;br /&gt;  gzip_http_version 1.1;&lt;br /&gt;  #gzip_min_length   1100;&lt;br /&gt;  gzip_comp_level   5;&lt;br /&gt;  #gzip_buffers      4 8k;&lt;br /&gt;  gzip_types        text/plain text/html text/xml text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/atom+xml;&lt;br /&gt;  #gzip_vary        on;&lt;br /&gt;  #gzip_disable     "MSIE [1-6]\.";&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 18 Apr 2008 14:09:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5385</guid>
      <author>indiehead (John)</author>
    </item>
    <item>
      <title>unix, pack folder and contents with tar gz</title>
      <link>http://snippets.dzone.com/posts/show/3075</link>
      <description>&lt;code&gt;&lt;br /&gt;tar -zcvf packagename.tar.gz folder/&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 04 Dec 2006 16:28:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3075</guid>
      <author>assbach (assbach)</author>
    </item>
    <item>
      <title>Enabling Zip compression with PHP</title>
      <link>http://snippets.dzone.com/posts/show/1048</link>
      <description>// if you want to reduce the bandwidth bill and increase page load time for long pages - this PHP snippet will turn on gzip compression for those that support it.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function compress_handler($in_output)&lt;br /&gt;{&lt;br /&gt;  return gzencode($in_output);&lt;br /&gt;}&lt;br /&gt;if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== FALSE)&lt;br /&gt;{&lt;br /&gt;  ob_start('compress_handler');&lt;br /&gt;  header('Content-Encoding: gzip');&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;  ob_start();&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 03 Jan 2006 19:52:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1048</guid>
      <author>alex.bosworth ()</author>
    </item>
    <item>
      <title>Using gzip module</title>
      <link>http://snippets.dzone.com/posts/show/625</link>
      <description>Modified from this funny thread in c.l.p&lt;br /&gt;http://groups.google.co.uk/group/comp.lang.python/msg/e53361331c23fd76&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import gzip&lt;br /&gt;&lt;br /&gt;zfile = gzip.GzipFile('somefile.gz')&lt;br /&gt;content = zfile.read()&lt;br /&gt;zfile.close()&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 Sep 2005 22:22:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/625</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
