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 8502425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:28:55+00:00 2026-06-11T01:28:55+00:00

I am looking at compressing some very large strings (text fields) in Ruby before

  • 0

I am looking at compressing some very large strings (text fields) in Ruby before inserting them into database blob fields. Compression by itself is easy, I can just use Zlib.

However, I am also looking at instances when I may have similar copies of strings. Eg. I might have something already stored in the database – stringA. A modification gives me stringB. I want to store a compressed version of the difference between stringA and stringB, so that if I have stringA and the compressed diff, I can get stringB back.

Is there a suitable library for this?

Ideally, it would be a single step binary diff compression. I don’t really want a human-readable text diff (which might waste more space). It only needs to be machine readable. Therefore please don’t suggest that I compress using diff -u oldFile newFile > mods.diff and patch < mods.diff.

Answer

Edit: Thank you Mark Adler for part of the answer (didn’t know there was a set_dictionary method). I want to do this in Ruby, thus, the relevant method name is set_dictionary. However, trying to get this done is much more difficult than without the dictionary.

Without using a dictionary, we can do:

A = "My super string to be compressed. Compress me now to " \
    "save the space used to store this super string."
cA = Zlib::Deflate.deflate(A)
# => "x\234U\214\301\r\200 \020\004[\331\nh\302\267E n\224\a\034\271;4v..."

Zlib::Inflate.inflate(cA)
# => "My super string to be compressed. Compress me now to save the..."

But to use a dictionary, you need to make sure to pass Zlib::FINISH to deflate to flush the output, and allow a Zlib::NeedDict exception before adding the dictionary when inflating:

B = "A super string with differences, let's see how much " \
    "extra space the differences will take in this super string!"
zlib_deflate = Zlib::Deflate.new
zlib_deflate .set_dictionary(A)
dB = zlib_deflate .deflate(B, Zlib::FINISH)
# => "x\2733\324$\230sD\265\242<\263$C!%3--\265(5/9\265XG!'\265D\035\250..."

zlib_inflate = Zlib::Inflate.new
zlib_inflate.inflate(dB) # Exception thrown
# => Exception: Zlib::NeedDict: need dictionary
zlib_inflate.set_dictionary(A)
zlib_inflate.inflate(dB)
# => "A super string with differences, let's see how much extra space the..."
  • 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-06-11T01:28:57+00:00Added an answer on June 11, 2026 at 1:28 am

    You can do this with zlib as well. Use the deflateSetDictionary() function to provide stringA as the dictionary when compressing stringB. On the other end you already have stringA when decompressing stringB, so use inflateSetDictonary() with stringA before decompressing stringB.

    zlib will then find parts of stringB that match stringA and point to those parts in stringA.

    You can do better still by providing stringA and stringB concatenated as the dictionary when compressing stringC. And so on. The dictionary can be up to 32K bytes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for some recommendations about compressing data in .NET, aside from using
I was looking over some of our JavaScript compression and noticed that no strings
I'm looking for some filesystem, or archiving file format, with transparent compressing/decompressing support, and
I'm looking to learn some theory about data/audio/video compression, and would be glad to
I am looking for a simple method of zipping and compressing with delphi. I
Looking at some assembly code for x86_64 on my Mac, I see the following
Looking for a control that allows to select one text value at a time
Looking at some of the code System.Linq I've come across some examples of Buffer<TSource>
Using a very large raw binary data file, I will be creating a large
I'm looking to compress some lectures which are in MP3 format. Currently, they're each

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.