Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6698569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:35:36+00:00 2026-05-26T06:35:36+00:00

I have a need to compress large chunk of text before saving it to

  • 0

I have a need to compress large chunk of text before saving it to the database and decompress it back once client requests it.

The method I am using right now seems to work fine when I insert new records using the Rails console and query for the newly inserted record right away. i.e., I can decompress the compressed description successfully.

But I am not able to decompress the compressed description for any of my other records added prior to this date. It is really confusing for me especially being a beginnner to the ROR world.

I am using MySQL as a database.

See my Model below to better understand it.

require "base64"

class Video < ActiveRecord::Base
  before_save :compress_description

  def desc
    unless description.blank?
      return decompress(description)
    end
  end

  private

  def compress_description
    unless description.blank?
      self.description = compress(description)
    end
  end

  def compress(text)
    Base64.encode64(Zlib::Deflate.new(nil, -Zlib::MAX_WBITS).deflate(text, Zlib::FINISH))
  end

  def decompress(text)
    Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(Base64.decode64(text))
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:35:37+00:00Added an answer on May 26, 2026 at 6:35 am

    Ok it’s actually very easy to reproduce your problem. In rails console do the following

    Video.create(:description => "This is a test")
    Video.last.description
    => "C8nILFYAokSFktTiEgA=\n" 
    Video.last.desc
    => "This is a test" 
    Video.last.save #This update corrupts the description
    Video.last.desc
    => "C8nILFYAokSFktTiEgA=\n"
    

    The reason the corruption happens is because you are compressing an already compressed string

    You should probably modify your class as follows and you should be fine

    require 'base64'
    class Video < ActiveRecord::Base
      before_save :compress_description
      after_find :decompress_description
      attr_accessor :uncompressed_description
    
      private
    
      def compress_description
        unless @uncompressed_description.blank?
        self.description = compress(@uncompressed_description)
        end
      end
    
      def decompress_description
        unless description.blank?
          @uncompressed_description = decompress(description)
        end
      end
    
      def compress(text)
        Base64.encode64(Zlib::Deflate.new(nil, -Zlib::MAX_WBITS).deflate(text, Zlib::FINISH))
      end
    
      def decompress(text)
        Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(Base64.decode64(text))
      end
    end
    

    Now use your class as follows

    Video.create(:uncompressed_description => "This is a test")
    Video.last.description
    => "C8nILFYAokSFktTiEgA=\n" 
    Video.last.uncompressed_description
    => "This is a test" 
    Video.last.save
    Video.last.uncompressed_description
    => "This is a test" 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have around 2 million strings with different lengths that I need to compress
I have a need to create a HttpSession (via cookie) whenever a client invokes
I need to pre-compress some very large html/xml/json files (large data dumps) using either
I have a large directory which I need to upload to a new host's
I have the need to compress a one Big file (~450 Mbyte) through the
I have to dump a large database over a network pipe that doesn't have
I need to compress/decompress some Data with a old, in house developed Algorithm. There
I need to compress a file as 7zip using SharpCompress: http://sharpcompress.codeplex.com what I have
I need to store a very big amount of text in mysql database. It
I have need to produce LINE, BAR, and PIE charts in Rails. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.