Have you ever seen this?
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/zlib/rdoc/Zlib/Deflate.html#method-c-deflate

Zlib::Deflate.deflate(string, level) is all you need, you don't have to copy standard function's content.