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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:15:56+00:00 2026-05-25T23:15:56+00:00

Can someone please explain to me how the zlib library works in Nodejs? I’m

  • 0

Can someone please explain to me how the zlib library works in Nodejs?

I’m fairly new to Nodejs, and I’m not yet sure how to use buffers and streams.

My simple scenario is a string variable, and I want to either zip or unzip (deflate or inflate, gzip or gunzip, etc’) the string to another string.

I.e. (how I would expect it to work)

var zlib = require('zlib');
var str = "this is a test string to be zipped";
var zip = zlib.Deflate(str); // zip = [object Object]
var packed = zip.toString([encoding?]); // packed = "packedstringdata"
var unzipped = zlib.Inflate(packed); // unzipped = [object Object]
var newstr = unzipped.toString([again - encoding?]); // newstr = "this is a test string to be zipped";

Thanks for the helps 🙂

  • 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-25T23:15:57+00:00Added an answer on May 25, 2026 at 11:15 pm

    Update: Didn’t realize there was a new built-in ‘zlib’ module in node 0.5. My answer below is for the 3rd party node-zlib module. Will update answer for the built-in version momentarily.

    Update 2: Looks like there may be an issue with the built-in ‘zlib’. The sample code in the docs doesn’t work for me. The resulting file isn’t gunzip’able (fails with "unexpected end of file" for me). Also, the API of that module isn’t particularly well-suited for what you’re trying to do. It’s more for working with streams rather than buffers, whereas the node-zlib module has a simpler API that’s easier to use for Buffers.


    An example of deflating and inflating, using 3rd party node-zlib module:

    // Load zlib and create a buffer to compress
    var zlib = require('zlib');
    var input = new Buffer('lorem ipsum dolor sit amet', 'utf8')
    
    // What's 'input'?
    //input
    //<Buffer 6c 6f 72 65 6d 20 69 70 73 75 6d 20 64 6f 6c 6f 72 20 73 69 74 20 61 6d 65 74>
    
    // Compress it
    zlib.deflate(input)
    //<SlowBuffer 78 9c cb c9 2f 4a cd 55 c8 2c 28 2e cd 55 48 c9 cf c9 2f 52 28 ce 2c 51 48 cc 4d 2d 01 00 87 15 09 e5>
    
    // Compress it and convert to utf8 string, just for the heck of it
    zlib.deflate(input).toString('utf8')
    //'x???/J?U?,(.?UH???/R(?,QH?M-\u0001\u0000?\u0015\t?'
    
    // Compress, then uncompress (get back what we started with)
    zlib.inflate(zlib.deflate(input))
    //<SlowBuffer 6c 6f 72 65 6d 20 69 70 73 75 6d 20 64 6f 6c 6f 72 20 73 69 74 20 61 6d 65 74>
    
    // Again, and convert back to our initial string
    zlib.inflate(zlib.deflate(input)).toString('utf8')
    //'lorem ipsum dolor sit amet'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please explain when and why use commit? http://msdn.microsoft.com/en-us/library/ms190295.aspx . 1) Update in
Can someone please explain how Read/Show works.. I cannot find any tutorials on it.
Can someone please explain to me how this responsive approach works? This was done
Can someone please explain to me how paging works in SmartGWT? I see it
Can someone please explain what's the real use of a Tuple ?
Can someone please explain why this doesn't work? MyClass myClass1 = new MyClass(); object
Can someone please explain to me, why every time I use the FlowLayout Layout
can someone please explain what is meant by: :new and :old in trigger code.
Can someone please explain me why its not possible to put a '\0' character
Can someone please explain the following Argument Exception : The structure must not be

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.