 Hey everybody it's Brian and welcome to the 112th tutorial with Qt and C++ and GUI programming. Be sure to visit my website www.voidromes.com for the source code to this and all the other tutorials. I shouldn't say all. I'm missing some and you know I had a server crash if you've been following along you know the story. So if you're following along in this or any of the tutorials and you notice the source is missing off my site please zip it up and send it to me and I'll post it for everybody else to use. Alright man that's a mouthful. I'm going to quit saying all that. If you've been following along you know this is the third tutorial in a four part series. Yeah we've built Zlib. We've built QAzip. If you haven't done either of those steps watch the other videos. You have an original folder which has files which we're going to be working with in this tutorial and a Libs folder and I want to populate that. Alright we populated it in previous tutorials but I just want to make sure everybody's on the same page here. So you're going to need your, if you're on not windows, libz.a, libz.so and libz.so.one. Throw those in the Libs folder. If you're on Windows your life is a little bit easier because you could just go in and grab the actual DLL and the .lib and .def and throw those in your Libs folder here. Now why is there a difference between DLL and SO? Everybody's probably going to ask me, well, ask Bill Gates. I don't know. To be brutally honest it's just a naming convention. They are just libraries. They do almost exactly the same thing just as the different code base. Alright so now that you've got your library set up we've compiled QAzip. We've compiled Zlib. Let's actually go in here and look at this stuff. Let's give QAzip another build just to make sure it builds. Now we want to grab out of our build directory all of the SOs or if you're on Windows you're going to see a DLL and a .a and maybe like a .def, you're going to grab that also. Throw those also in the Libs folder because what we're going to be doing in this tutorial is using that library, I should say these libraries, the libz and the QAzip. If you're on Windows those are going to say .dll's. We're going to be using those in this tutorial to actually compress and decompress and have some fun with zip files. Alright so let's go file, new file of project, alright I want to put this in zipvids and we're just going to call this, let's call it zip1, let's call it myzip1 and just put your stuff wherever you usually put it. I always can't go when I say that, I'm sorry, very immature but alright now right out of the box we just have a very basic command line app, it compiles, it runs, it doesn't do anything special. So what we need to do here is we need to actually link to that QAzip in Libz. One thing you should know right off the bat here is you're going to need those libraries whether they're SOs or DLLs you're going to need those in the same directory, whoops, in the same directory as your program or you're going to want to put them in somewhere where the operating system can find them in the path variable typically that would be like USR slash lib or it would be in C Windows system 32 or C Windows slash system somewhere in the path variable where it will find up. The easiest way is just put it in the same directory as your program. Now just because you put them in there doesn't mean it can actually use them yet. That's what we're doing in this tutorial. So crack open the project file, we're going to do a little bit of coding here. We're going to set some variables and we're going to say QUazipCoder, help if I can spell code and you're going to want the actual path to that and we're going to say ZlibCoder, grab this QAZip, QAZip, you're going to want to grab the path to this guy and then for good measure we're also grabbing the path to Zlib. So we've got our two variables in there and to be brutally honest you don't really need this you could just do your Libs folder, we're going to say Unix and then we're going to have a Win32, get rid of that output window there so we can see what we're doing here. Let's say Libs plus equal and we're just going to grab this ZlibCoder, sometimes I need like background music or something like maybe circus music, anyways you get what I'm saying here. Alright now one thing you should notice that this actually right here doesn't need to be that. It could and probably should be our Libs folder here because what we're doing really is we're linking to this thing so we can copy that and you can just change it if you want to. It really depends on your style, I like linking to the individual directories, some people like linking all into one folder, it doesn't really matter a whole lot. Alright now we need to actually include some source files here. Include path plus equal and we're going to grab this guy and then we're going to say headers plus equal, sorry my cat just made some weird grunty noise I couldn't figure out what the heck he was doing. Start at H. Now you're not going to really in a production environment want to include wild car because what we're saying here is headers are plus equal to this folder QAZip because we're using that variable and then we're doing slash start at H so all the headers are going to get included and we can prove that if we just hit save and it will evaluate it and then suddenly ta-da all the headers for the QAZip are included now you're not going to want to do that in a production environment because if some coworker drops a header file in there that you don't want your project it's going to automatically get included and you could spend days trying to figure out what just happened it's also a very big security threat because well you know same thing I could just inject code into your program just by adding a file but because I don't want to sit here for an hour adding files we're just going to do this give it a good save okay so if you follow along so far we've made two variables the QAZip coder and Zlib coder and the paths are going to change depending on your system we have a declaration here declaration G's a branch here where we're saying if we're on Unix grab this library if we're on Windows grab that library all right so what we're going to want to do at this point is actually just give it a good build make sure everything builds fine if it doesn't build at this point you're going to have to figure out why and this is always the nerve racking part of working with code I just breathe the use either huge sigh of relief right there if you get like a undefined reference to check your library paths many many times you won't have the library in the right path especially windows is very fickle about that that's why I said just make sure it's in the same directory as your program here you see how we've got all this stuff out here if your libraries are in the same directory it should be able to link to them just fine all right now we are going to actually write some code no if we run this nothing happens you know we've just got a standard little little window where nothing really happens so we're going to start writing some code we want to do a few things here first we are going to set up some strings we'll say q string wow I cannot type and through the magic of copy and paste I'm going to save a little bit of time here and we're going to call this somebody once asked me Brian is English your second language no I'm just a horrible speller I can write code all day long but don't ask me to write a thesis oh my gosh all right so what we need to do here is we're going to set up some variables of what we're going to be working with so let's actually go back here I'm going to just save some time by copying and pasting oh what should we name this test dot zip new dur and then single file we're just going to say original dur plus slash drunk dot jpg it's actually kind of a funny picture I found you'll see it later made me chuckle all right so we've got that zip file original dur new dur and single file and we are going to write some code to compress a directory and I'm going to be writing a sizable amount of code here so I may actually split this into multiple videos I'm not sure yet list the contents of a zip file and decompress an archive to a directory and decompress a single file whoops all right now before we can do any of that we're going to have to add some includes here all right so let's just say if you're wondering what jl compresses it's part of the qa zip it is this guy right here and what he's done is this jl compress is actually much easier to work with than working with the individual files I mean qa zip has a a ton of functionality built right into it but it's can be kind of a bear to work with if you don't really know what you're doing so we're just going to stick with something easy for this tutorial and we've got the headers right here so we don't even really need qa zip open anymore we can actually close that all right so first things first we're going to set up our functions here I love copy and paste so what we're doing here is we're setting up a compressed directory where we're going to say a zip file and a directory decompress where we're going to say zip file and directory and we're going to compress files which is a zip file in a q string list called files sorry I was trying to juggle between you guys and my notes here it wasn't going too well then we're going to decompress files and with this we're also going to need an extra one q string the directory that we're going to do and then we need to list the contents here all right I feel like I'm going to sneeze at any minute don't you hate that feeling like when your nose starts tickling and you're like oh come on let's actually include q file here all right so first things first we're going to compress a directory so we're going to take an entire directory and compress it down into one zip archive here so we're going to say if jail compress compressor and you can see how it's very easy to work with they just want a compressed file and then a directory so we're going to say zip file directory and we're just adding a little bit of logic here I can hear my cat whining always breaks my heart it sounds like a child crying could not create all right so what we're really doing here is we're just saying compress the directory we're saying zip file and directory we're calling jail compress handing at the name of the file we want to create the directory we want to compress so let's actually do that right now go down here zip file and we want to compress original dir so what we're doing here is let me bring up my file structure here we're going to create a file called test zip and we're going to take this folder original and compress it down so let's give this a good run see what happens and it says created test as it so let's actually go out there there it is right there test does it and you see it's got our files in there and they're compressed so what we're going to do now is list the contents of that sorry kitty come on go go bye bye q file file and we're going to say zip file oops and we would want to check that the existence of it go away cat sorry guys cat was messing with me so if the file doesn't exist we just want to exit out of there otherwise what we want to do is we want to get file list and then just list it out we're just going to use the for each macro there so let's actually take this and let's say zip file so we're going to compress the directory and then list the contents let's run this guy created and there's the contents our three files so as you can see this is actually pretty easy to work with here i'm going to comment these out because we don't want to mess up our other ones but we're going to decompress the archive to a directory and then decompress a single file here whoop did we miss one yeah we did miss one we missed uh compressing a single file all right so let's do this back up and do a compress a single file here so we're going to say jail you guys did compressed files and we're just going to give it the file name for the file and then we need to give it a list of files now we're just handing it a q string list so we can quite literally just add one item to that q string list if we want and i'm going to through the magic of copy and paste here so let's actually add it right here sorry about that i'm going to make a new q string list and we're going to insert into there the single file variable there we go so if we run this now you see how it created the zip file we can go out there and there's only one file in there now instead of all of them because we only added the one now right now we're going to work on decompressing i'm trying to kind of fly through this because the uh the recorder i use gets a little finicky if these videos start going past 20 30 minutes so we're going to decompress the entire directory here we're going to say q string list list equal you guessed it jl compress and we're going to extract files i'm sorry extract dir because we're doing entire directory here we're going to give it the name of the zip file and then the name of the directory we want to extract to and then we're just going to do a for each q string item in list we're just going to list out that we extracted them right so we'll decompress this guy what are these warnings here oh unused parameter don't care about that give it the name of the zip file and then give it the name of the new directory because we're going to make an entirely new directory here so what we're going to do now is extract this guy to a folder called new dir or i'm sorry yeah new dir right here so i'm going to take that whole content and extract it and you can see we extracted drunk dodgy peg let's go see if that's actually true here there should be there it is new dir and you can see oh alcohol the granddaddy of regret look at her face she's just like oh dude and he's just laughing that's what friends are for a uh this gentleman boy what a lovely night he's going to have an even better morning to a all right now we're just going to finish this whole thing up we're going to decompress a single file here come on kitty go bye bye kitty sorry guys this cat's just like fall over me right now we're going to extract files and notice this takes the compressed file the q string list and then the directory so as you can see i mean once you've gotten to this point you can very easily just work with this library and it's just dirt simple i mean it's ridiculously simple to work with so we're going to decompress files and let's actually do the beauty of copy and paste here give it a directory and we want to extract drunk dot jpeg let's actually compress the entire directory again so what we're going to do for this last example here is we're going to compress the entire directory so we have more than one file list the contents and then extract a single file so let's actually get rid of new dirt get rid of test and then run this and if all goes well zip does not have a current file oh we made a boo boo so you can see we created we got three files but does not have current file uh i'll bet you anything it's because we have yep capital D lowercase d if we change that lesson learned extracted so we have created we've listed we've extracted if we go out sure enough that files there um case some of you are wondering kind of in this on a positive note what these other files are meanwhile in oregon guy chasing a fish across the road and very clever top secret bottom secret all right well that's all um i hope you found this series educational entertaining um there is one more video i'm going to do where instead of using the actual qua zip library we're going to actually use his code embedded into our program so we don't have to juggle around two libraries just one that's all thanks for watching i hope you found this educational entertaining