# Gzip compression of .rb and .rhtml files

**URL:** <https://www.ruby-forum.com/t/gzip-compression-of-rb-and-rhtml-files/87065>\
**Category:** Rails\
**Created:** [February 27, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/gzip-compression-of-rb-and-rhtml-files/87065 "2007-02-27T22:59:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [February 27, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/gzip-compression-of-rb-and-rhtml-files/87065/1 "2007-02-27T22:59:42Z")

</div>

I haven’t been able to find the answer to my question by Googling,  
possibly because “gzip” is everywhere on the web.

The context of my question is: deploying a Ruby on Rails application  
onto a system with limited (flash) storage. I am aware of the various  
compressed filesystems available, but I’m considering other  
approaches.

Is it possible to deploy a Rails application with all/some of it’s  
source .rb, .rhtml, .css and .js files gzipped to save space?

e.g. If foo.rb can’t be found, foo.rb.gz should be read instead. So  
long as the .rb and .rhtml files do need to be accessed randomly,  
there should be no need to gunzip the file in place.

These are text files, so 3:1 compression may be possible.

I have a feeling (as functions are first class objects in Ruby) that  
it may be possible to redefine a few IO functions to achieve this.  
Any hints would be welcome.

Regards,

Chris.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [February 28, 2007, 12:02am UTC](https://www.ruby-forum.com/t/gzip-compression-of-rb-and-rhtml-files/87065/2 "2007-02-28T00:02:39Z")

</div>

Chris D. wrote:

> I haven’t been able to find the answer to my question by Googling,  
> possibly because “gzip” is everywhere on the web.
> 
> The context of my question is: deploying a Ruby on Rails application  
> onto a system with limited (flash) storage. I am aware of the various  
> compressed filesystems available, but I’m considering other  
> approaches.
> 
> Is it possible to deploy a Rails application with all/some of it’s  
> source .rb, .rhtml, .css and .js files gzipped to save space?

Try looking at changing methods

def read\_template\_file(template\_path, extension)

or

def render\_template(template\_extension, template, file\_path = nil,  
local\_assigns = {}) #:nodoc:

in

actionpack/lib/action\_view/base.rb

> e.g. If foo.rb can’t be found, foo.rb.gz should be read instead. So  
> long as the .rb and .rhtml files do need to be accessed randomly,  
> there should be no need to gunzip the file in place.

You lost me with that one.

I’m looking at ActionPack version 1.12.5 here. You may have a different  
version.

Maybe it’s a one-liner.

Stephan

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [February 28, 2007, 1:11pm UTC](https://www.ruby-forum.com/t/gzip-compression-of-rb-and-rhtml-files/87065/3 "2007-02-28T13:11:06Z")

</div>

On Feb 27, 11:02 pm, Stephan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > source .rb, .rhtml, .css and .js files gzipped to save space?  
> > in
> 
> actionpack/lib/action\_view/base.rb
> 
> > e.g. If foo.rb can’t be found, foo.rb.gz should be read instead. So  
> > long as the .rb and .rhtml files do need to be accessed randomly,  
> > there should be no need to gunzip the file in place.
> 
> You lost me with that one.

If you just read a short file into memory, you can just gunzip the  
contents as you read them, not changing anything on disk.

If you need random access (read or write), you would need to gunzip  
the file to temporary storage (perhaps the disk), perform the  
operations, then gzip the file up again when finished.

> I’m looking at ActionPack version 1.12.5 here. You may have a different  
> version.
> 
> Maybe it’s a one-liner.
> 
> Stephan
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Thanks for your reply,

Chris.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [February 28, 2007, 1:48pm UTC](https://www.ruby-forum.com/t/gzip-compression-of-rb-and-rhtml-files/87065/4 "2007-02-28T13:48:12Z")

</div>

I think you’ll find the Output Compression plugin [  
[http://wiki.rubyonrails.org/rails/pages/Output+Compression+Plugin](http://wiki.rubyonrails.org/rails/pages/Output+Compression+Plugin)] does  
what  
you’re looking for. I was playing around with it and looking at the  
results  
in Firebug and it looked good.

RSL

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [February 28, 2007, 2:52pm UTC](https://www.ruby-forum.com/t/gzip-compression-of-rb-and-rhtml-files/87065/5 "2007-02-28T14:52:57Z")

</div>

On 28 Feb 2007, at 13:47, Russell N. wrote:

> I think you’ll find the Output Compression plugin [http://  
> [wiki.rubyonrails.org/rails/pages/Output+Compression+Plugin](http://wiki.rubyonrails.org/rails/pages/Output+Compression+Plugin)] does  
> what you’re looking for. I was playing around with it and looking  
> at the results in Firebug and it looked good.

If you want output compression for serving files, you can just use  
Apache’s deflate module:

# Deflate

AddOutputFilterByType DEFLATE text/html text/plain text/css text/  
javascript

# … text/xml application/xml application/xhtml+xml text/javascript

BrowserMatch ^Mozilla/4 gzip-only-text/html  
BrowserMatch ^Mozilla/4.0[678] no-gzip  
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

However, the original poster was looking for a way to compress his  
files on the server itself, decompressing them in memory before  
sending it through the ruby interpreter.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [February 28, 2007, 5:53pm UTC](https://www.ruby-forum.com/t/gzip-compression-of-rb-and-rhtml-files/87065/6 "2007-02-28T17:53:18Z")

</div>

On 28 Feb 2007, at 17:31, Stephan W. wrote:

> Why wouldn’t you use a compression-file-system?  
> My impression is that the original poster is trying to host a Rails  
> application on a very small shared hosting account (or a free hosting  
> account). The total size of a fairly large Rails project rapidly  
> exceeds 50 MB. My advise if this is indeed the case would be to just  
> pay a little bit more for a decent hosting provider instead of trying  
> to solve a problem that in the end will cost you more (timewise) then  
> it will cost you to get a better hosting account.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [February 28, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/gzip-compression-of-rb-and-rhtml-files/87065/7 "2007-02-28T17:31:35Z")

</div>

Chris D. wrote:

> On Feb 27, 11:02 pm, Stephan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > > source .rb, .rhtml, .css and .js files gzipped to save space?  
> > > in
> > 
> > actionpack/lib/action\_view/base.rb
> > 
> > > e.g. If foo.rb can’t be found, foo.rb.gz should be read instead. So  
> > > long as the .rb and .rhtml files do need to be accessed randomly,  
> > > there should be no need to gunzip the file in place.
> > 
> > You lost me with that one.
> 
> If you just read a short file into memory, you can just gunzip the  
> contents as you read them, not changing anything on disk.
> 
> If you need random access (read or write), you would need to gunzip  
> the file to temporary storage (perhaps the disk), perform the  
> operations, then gzip the file up again when finished.

I see now.

Actually you may be able to redefine the File methods that come into  
play, where File.exists? returns also true if filename + .gz exists, and  
File.read will read from filename + .gz if filename doesn’t exist.

This would then be on the Ruby level, not Rails.

Don’t know all the methods that would need redefining. Don’t know how  
the Ruby interpreter loads files (through the File class, or not?)

Why wouldn’t you use a compression-file-system?

Stephan

> > I’m looking at ActionPack version 1.12.5 here. You may have a different  
> > version.
> > 
> > Maybe it’s a one-liner.
> > 
> > Stephan
> > 
> > –  
> > Posted viahttp://www.ruby-forum.com/.
> 
> Thanks for your reply,
> 
> Chris.
