{"id":215,"date":"2012-12-31T16:36:14","date_gmt":"2012-12-31T22:36:14","guid":{"rendered":"http:\/\/sha.nnoncarey.com\/blog\/?p=215"},"modified":"2012-12-31T16:37:45","modified_gmt":"2012-12-31T22:37:45","slug":"storing-compressed-blobs-in-sqlite3-with-datamapper-for-ruby","status":"publish","type":"post","link":"https:\/\/sha.nnoncarey.com\/blog\/archives\/215","title":{"rendered":"Storing compressed blobs in Sqlite3 with Datamapper for Ruby"},"content":{"rendered":"<p>If you&#8217;re trying to store a blob (in my case, compressed text) into a Sqlite3 database with Datamapper and you&#8217;re running into this error:<\/p>\n<blockquote><p>Zlib::BufError: buffer error<\/p><\/blockquote>\n<p>Try base 64 encoding it, as hinted by this file: https:\/\/gist.github.com\/721922<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nrequire &#039;zlib&#039;\r\nrequire &#039;base64&#039;\r\n\r\nclass Foo\r\n  include DataMapper::Resource\r\n\r\n  property :compressed_file_content, Binary, accessor: :private\r\n\r\n  def file_content=(str)\r\n    @inflated_file_content = str\r\n    deflator = Zlib::Deflate.new()\r\n    self.compressed_file_content = Base64::encode64(deflator.deflate(@inflated_file_content, Zlib::FINISH))\r\n    deflator.close\r\n  end\r\n\r\n  def file_content\r\n    if @inflated_file_content\r\n      @inflated_file_content\r\n    else\r\n      inflator = Zlib::Inflate.new\r\n      @inflated_file_content = inflator.inflate(Base64::decode64(compressed_file_content))\r\n      inflator.finish\r\n      inflator.close\r\n\r\n      @inflated_file_content\r\n    end\r\n  end\r\nend\r\n<\/pre>\n<p>It&#8217;s as simple as that. No need to use SQLite3::Blob, for example.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re trying to store a blob (in my case, compressed text) into a Sqlite3 database with Datamapper and you&#8217;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 &#039;zlib&#039; require &#039;base64&#039; class Foo include DataMapper::Resource property :compressed_file_content, Binary, accessor: :private def file_content=(str) @inflated_file_content = &hellip; <a href=\"https:\/\/sha.nnoncarey.com\/blog\/archives\/215\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Storing compressed blobs in Sqlite3 with Datamapper for Ruby&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-215","post","type-post","status-publish","format-standard","hentry","category-ruby"],"_links":{"self":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":5,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts\/215\/revisions\/220"}],"wp:attachment":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}