Check out a free preview of the full Digging Into Node.js course
The "gzip Uncompress with zlib" Lesson is part of the full, Digging Into Node.js course featured in this preview video. Here's what you'd learn in this lesson:
Kyle continues adding gzip functionality to the program, enabling uncompression with the zlib Node module. Then, results are displayed for executing the program with different command line arguments.
Transcript from the "gzip Uncompress with zlib" Lesson
[00:00:00]
>> Kyle Simpson: Okay, so we are able to compress. The next logical step would be to uncompress our input stream, decompress our input stream. So let's go and add an uncompress. I don't know whether we should call it uncompress or decompress, but I wanna to call it uncompress. Uncompress as a boolean flag.
[00:00:22]
Add it to our printHelp output.
>> Kyle Simpson: And then we know in a our process file that we're gonna be handled an input stream which may have been Gzip. So before our transformation stream, we need to insert a step that does the G unzipping. So if args.uncompress has been set that we want to do some decompression, we're gonna say, let gunzipStream equal to zlib.createGunzip.
[00:01:18]
I do not know why the capitalization worked that way. That looks so weird to me, it looks like Gunzip, but whatever that's how it's spelled. So that makes our decompression, our Gzip uncompression transform stream. And then the same pattern, we're gonna say outStream is equal to outStream.pipe into our gunzipStream.
[00:01:50]
>> Kyle Simpson: Let's go try this. First of all, we'll just have a dash dash out. And the file that we wanna use is not the lorem, we wanna use our current out.txt.gz. So there's our gzip compressed thing. So we need to say, --uncompress and then --out, which should print the lorem ipsum uncompressed to our standard out.
[00:02:23]
So first we will,
>> Kyle Simpson: Make our out.txt.gz.
>> Kyle Simpson: There's our 266K out.txt.gz, and then I will cut out.txt.gz into ex2.js.
>> Kyle Simpson: And we will uncompress. And we'll pull it from the input. And we'll write it to the output. And then, we're seeing the whole thing. So now we're able to input, we're able to unzip on the inbound.
[00:03:06]
We're able to zip on the outbound. We're able to mix and match those as we see fit. And the inputs can either be from a file system or from our standard in. Last thing I forgot about our ex, just to make sure we don't confuse ourselves is we should be updating each time we make a new file, updating that to say ex2s instead of ex1.
[00:03:30]
So that's it for the exercise two section.
>> Speaker 2: The output of the uncompressed, I'm sorry of the compressed gzip output.
>> Kyle Simpson: It's the gzip binary data.
>> Speaker 2: Binary.
>> Kyle Simpson: Yes, it's binary data. You're asking in here? These are all streams, right? So inputs are coming in as binary buffers and outputs are going out as binary buffers.
[00:03:56]
The only time that it's ever in string form is temporarily when we transform it from lowercase to uppercase. But then as soon as it goes through its next step, it's now back to being a binary buffer. As a matter of fact, when we do a this.push and we push this thing in, it turns it back into a buffer.
[00:04:12]
So internally the streams are just moving buffers along from pipe to pipe to pipe.
>> Speaker 3: This .push it's not overriding the .push method on a raise because this here refers to the-
>> Kyle Simpson: You're talking about right here?
>> Speaker 3: Yep.
>> Kyle Simpson: Yeah, the transformed stream is not actually an array.
[00:04:32]
>> Speaker 3: Yes, so I mean yeah.
>> Kyle Simpson: It just happens to look kind of like an array since we're calling .push on it. But this is just an object that they define a push method on.
>> Speaker 3: Cool.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops