 Hey everybody, this is Brian. Welcome to the 114th video with Qt and C++. Alright, if you've been following along, I did a four-part series on Qt and Zip Files. And I remember this blog, I brought this up. Basically, you can use these internal classes in Qt, which are not official part of the API, the Qt Zip Reader and Qt Zip Writer. And I just, I could not leave it alone. I wanted to get it to work. I actually emailed the author of this blog and he emailed me back and said, hey, what error message did you get? And that made me think, oh yeah, silly me. It was just a stupid undefined reference too, which means the linker couldn't find it. So, revisited it. And for this tutorial, we're going to actually do it. So you're going to need to go out and grab Zlib. Zlib.net, you should know that from the previous four tutorials. And go out to gettorious.org. And you're going to need to go to, there's a whole lot of source code out here, because this is the actual source code for Qt. You're going to need to go to source, GUI, and text. And in there are some files here. There are Qzip, Qzip Reader, Qzip Writer. Those are the three files we need to grab. You just simply open them up and I've got them here. The easiest way rather than cloning the whole repository and all that is just right-click, save link as on Roblob. And that will actually download the file. Once you have those files, as you can see I've downloaded them here. I've got Qzip, Qzip Reader, Qzip Writer. You notice the underscore P and that's for private, meaning they haven't published that to the official API. I've also got Zlib, the current version. I've extracted and renamed it to just Zlib. And inside that is the source code and inside that, in case you're running Windows, I actually went out and got the Windows version of this. Remember Windows comes pre-compiled so you don't have to really mess around with that. That'll save you a little bit of headache if you're on Windows. Also I have a folder that we're going to be working with, with some images in it. And let's just get right into this tutorial. All right, so I've got just a basic command line application. Give it a good build. Nothing fancy about it. And this is the output of that, just a simple command line application. Nothing real special. What we need to do next is if you're in not Windows, I have to always say not Windows. If you're in Linux, Unix, BSD, we need to actually build Zlib. So let's actually copy this. If you're in Windows you can skip this step. All right, we need to of course configure and then make. And that will actually build Zlib. All right, now that we've got a good build, we can go out here, sort by type. And let's grab the A, the SO, and the SO1. And this is the output directory where our program is actually going to be built. We're just going to paste these in here. Now, if you're in Windows, you can skip that whole step. You can just go to this Windows folder that I put in there and grab these three files, the lib, def, and DLL, and throw those in there instead. Very important step. All right, let's go back to our source here. We're going to need to do a little surgery on these three files that we've downloaded from Getorius. Remember, these are not officially supported, so if it doesn't work, yeah, we're kind of stuck. First thing we're going to do is rename these. We're going to take out the Q and the underscore P. I need like theme music in the background. I don't know, like maybe the night writer sound. You guys know what I'm talking about. All right, we're just going to add some existing files here. And we're going to add our three files we just modified. Now we need to do a little bit of surgery on here because we still have these names in here. Q zip reader. All right, so we need to actually go in here and find and replace. Ah, my mouse got hung up. So we're going to replace Q zip reader with just zip reader. Save that. Zip writer, we're going to do pretty much the same thing. Replace Q zip writer with just zip writer. Save that. And then we're going to open up zip.cpp and same deal. And be sure to take out those underscores in the header. If you're on a platform that is very finicky, you'll need to make sure that these header files are exactly the way they're spelled, upper and lower case. All right, one final step. We'll need to actually go out here to Zlib and grab the... Where is it? Zlib H. And of course now I can't find it. Z config, anybody, anybody. Oh, we're crying out loud. Sort by name here. There it is. Geez. That was a pain. Paste those into our project. And we'll need to add in Zlib, the Zlib header and the Zlib config to our project here. Now we need to perform a little bit of surgery on our project file. And if you were following along in that blog, let's go back here. This is what he was saying. Use this little snippet of information. I just, I really couldn't get that to work. I don't know why I played around with it. But I was getting all sorts of undefined reference to, which just means simply it can't find it. So I'm not proposing that I have a better way. I'm just saying I'm not smart enough to figure out why his isn't working. So what we're going to do is we're going to say win 32. And we're just going to include some stuff here. So we're going to say include path, plus equal, print working directory, Zlib. All right. And then we're going to say, Libs plus equal. So what we're doing here is we're saying if we're on Unix, then include the path, which is our current working directory, and the Zlib folder, and then include the library, that Zlib library. We're just going to copy and paste this. And we're saying if we're on Windows, we want to do it slightly differently. We want to say it was at ZDLL. And I guess we could actually simplify this a little bit further and just include that. There we go. Nice and beautiful. So what we're saying is include that Zlib sub folder. And then if we're on Unix, include the Z library. If you're on Windows, include ZDLL, which actually we need to fix that a little bit more too. Isn't that what we called it Windows? Yep. Windows with uppercase. There we go. Now remember, in your build directory, you should have those objects out there. So if you're on Windows, you're going to need these three files in your build directory with your EXE. All right. Let's actually give a good build and see what happens here. This is always the excruciating part. Hey, we got a good build. What do you know? So what we've done just now is we've seen that we can actually link it together. It works just fine. So now we need to start writing our code. And we're going to say include Qtabug, include ZipReader, ZipWriter. And we're going to make a couple of functions here. So we're going to say compression test takes a zip file. I cannot spell today. The zip file. And we're going to copy and paste that. We're going to make a similar one called decompress test. All right. So now we've got our two functions here. Very simple. We're just going to compress and decompress a file here. So let's actually go down here and make this work a little bit. Q string. Zip file. And this is the name of the zip file we're going to be creating. And let's just grab our project directory here. Where are we at here? Zip test. I want to make a zip file right in here. So we're going to just copy this. I'm just going to call it test.zip. Really professional, right? All right. So really all we're doing is we're making a Q string and then we're going to compress and decompress. So this is where the magic is going to happen in these two functions up here. So let's start fleshing out our compression. So first thing we need to do is zipWriter. And this wants a file name. So we need to give it the zip file name that we're going to be using. Now we can do a couple little things here. We can either add a single file or add a directory. So let's add a... Let's do a directory first. Add directory. Wow. Pretty easy. And what was the name of our images directory again? That's right. Images. So we're going to just paste that in there. And then of course we were going to just close the zip. And for our decompress we're going to actually just read the files that are in there for now. zipWriter. See zip. zipFile. And then we're going to say for each... What was it again? Oh yeah. This is zipWriter. They have a special structure in there. It's not file error. It's fileInfo. Sorry. And we want to get fileInfoList. You notice that's a Q list of fileInfo. So that's actually pulling it out of the zipWriter class. And then we'll just say QDebug. Ooh, notice a small little bug here. Item. That would have been interesting. And then from that structure we can actually get the filePath, the CRC32. We can get as a symbolic link. Is it valid? We can get the size, the permissions, et cetera, et cetera. But we're just going to do filePath. So now when we run this we're going to call compressTest, which is going to compress an entire directory down into an archive file called test.zip. And then we're going to decompress it. But instead of actually decompressing anything, we're just going to read the contents at this point. Save and run. What happened there? Let me go out and look at this thing. It made it. Yes, there it is. Did not like that. Okay. I'm an idiot. Helps if you actually read the documentation here. I pause it just to figure out what was going on. If you look at the source code of zipWriter, I thought that it was like the QA zip where it'll add an entire directory. No, it just creates a directory inside of the zip file and then you can place files into it. So helps if you read the documentation. All right. So moving right along, you can actually create a directory inside there. And let's look at some of these other options we have here. We can add a directory, add file, symbolic link. We can create permissions, things like that. Actually, it's pretty, pretty snazzy when you look at this. I mean, it's all free. So let's actually show how to compress a file. We're going to say zipWriter, czip, zip file. And we're going to say qstring. Let's call this single file. Let's go grab a file name of one of our images here. And let's add fail1. Make sure that's actually spelled correctly here. Yep, fail1.jpg. All right. So got our name of our file. Now we want to make a q file. I'm going to set this to read only. And then, of course, whenever we open some, we want to close it. Now we're going to say, notice how this one's a file name and a byte array. So what we're doing is we're adding the name of a file. So we're just going to call this test.jpg. Notice how you can change the name of the file if you really felt like it. And we're going to say file read all. So we're just going to hand it the byte array. So we save and run this. It runs. Let's kill the program. Go back out here. Sure enough, there's test.jpg. Now, if we were to extract this manually, let me actually move this up so you can see I'm extracting it. You ever have one of those days? Epic fail. See the look on her face? She's so happy right now. Must be a dog lover. Anyways, let's move that to the trash. All right, we still got our test.zip with our jpeg in it. Now we're going to actually do this the right way. Now that we've done a little bit of debugging here. Yes, and sure enough, there's test.jpg because we are reading the decompressed test. Now we're going to actually decompress that file. And we're going to say, we never close that. And we are going to just kind of examine this a little bit. We can get the count. We can get the actual device to the QIO device, the pointer to it. The entry info at, meaning we get that file info structure. We can see if the zip file even exists. We can extract all to a destination directory. That's pretty simple. I'm just going to leave that in here. Just give it a Q string and it extracts everything. But for this one, we're going to be a little more complex here. We're going to actually extract a single file. To do that, we need the file data. Notice how it hands back a Q byte array. We need to get the entry info at. So we're going to say, file info. We're going to call this finfo. And we're just going to get the first file because it's just going to be an array. And then we're going to just say, Q. Actually, let's do this. Let's actually write it out there. Q file, file, and let's name this. Oh my goodness. This is going to just drive me nuts. Sometimes copy and paste isn't so magical. There we go. We're going to call this fail 2. We're going to write this out to our images folder. If we go out there and look right now, there is no fail 2. There's just fail 1. We're going to just simply write the bytes out to the disk. Actually, first we got to open. Sorry, a little ADD today. We're going to write that byte array out there. So what we need to do is we're going to say, whoops, dyslexia, and we're going to get the file data. Notice how it wants a Q string or the file name. So what we're going to do is we're going to actually grab that fmfo and hand it that name or the actual file path. Because remember, it treats this archive like a mini directory structure. So it wants the fully quantified path to that. That's why we're grabbing the entry at so we can get the full path name. And then we're just handing it the path name. So just do review. We're going to compress a file. We're going to iterate through that and see what files are in the archive. We could just do extract all if we wanted to extract the entire thing, but we're going to extract a single file. And we're going to extract that to fail 2.jpeg. And we just simply open the file, write the bytes from the file data from the zip file. That's not confusing. And then close the file and the zip file. Let's see if this works. We got a good build. No error messages. And sure enough, fail 2 exists. It opens up. Works just fine. Just in case you're wondering, we also have Barbie, who they didn't give me a Barbie, so I gave them a Barbie queue. Get it? Look at that girl's face. And my personal favorite, you got me ribbons. Oh my God, oh my God. I love cats. What can I say? Dogs are awesome too, but cats just rule. All right, so just to recap, what we covered here today is how to use the unofficial and unsupported and undocumented QueueZip Reader and QueueZip Writer, rename them, reconfigure them, add them to our class. And we've also built Zlib and linked it into our project, and then we've covered just the bare basics on how to use this. I'm going to zip all this up, maybe if my mouse works, and throw this up on my website, www.voidrealms.com. You can find it up there in a few minutes after I get done uploading it. I hope you found this educational and entertaining. If you use this, this should come with a very big disclaimer that this may cease working at a moment's notice for any unknown reason. It's not an officially supported class. We kind of did some dirty little workarounds. Thanks for watching.