Storing compressed blobs in Sqlite3 with Datamapper for Ruby

If you’re trying to store a blob (in my case, compressed text) into a Sqlite3 database with Datamapper and you’re running into this error:

Zlib::BufError: buffer error

Try base 64 encoding it, as hinted by this file: https://gist.github.com/721922

require 'zlib'
require 'base64'

class Foo
  include DataMapper::Resource

  property :compressed_file_content, Binary, accessor: :private

  def file_content=(str)
    @inflated_file_content = str
    deflator = Zlib::Deflate.new()
    self.compressed_file_content = Base64::encode64(deflator.deflate(@inflated_file_content, Zlib::FINISH))
    deflator.close
  end

  def file_content
    if @inflated_file_content
      @inflated_file_content
    else
      inflator = Zlib::Inflate.new
      @inflated_file_content = inflator.inflate(Base64::decode64(compressed_file_content))
      inflator.finish
      inflator.close

      @inflated_file_content
    end
  end
end

It’s as simple as that. No need to use SQLite3::Blob, for example.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.