Listing Zip Archive Contents With Terminal and Automator

You can peer into a zip archive and see which files are in there using the zipinfo Terminal command. You can refine the results to show you only the files with fgrep. You can take this same command and use it as a Shell Script in an Automator service for easy access. In this tutorial you'll also learn about using Shell Scripts with files as input arguments in Automator.

Video Transcript
So let's say you want to compress a folder full of files, make it a little smaller and kind of archive it, so it's out of the way. Maybe it's from a project that you've finished. So here I've got a folder here, the Docs folder, and I'm going to go into it and I can see there's a bunch of subfolders, there's a bunch of files. A whole bunch of stuff in this folder.

So I want to compress this. I'm going to use the Control key and click and then select Compress and I get a Zip Archive here. You can see it's the same name as the folder .zip. So maybe then I go and delete this folder. So great. I've got a Zip Archive. So how do I know what's inside it. The easy way to find out is to double click it. It will unarchive itself. It will expand and then I can see everything in there.

Now that's not really practical if it's huge. Say it's many gigabytes in size. You just want to see if there's one little text file or something in there. So is there a way to look in it to see what files are there. Well, you can get a lot of third party apps that will do this. That will actually give you a listing and even allow you to extract a single file.

But you can do it without any third party apps using the Terminal or Automator. So let's use the Terminal first. So I'm going to use Spotlight to launch the Terminal. So Command space and then Terminal and Return. That will bring up the Terminal window here. Let me clear it. The command to actually look inside of a Zip Archive is simply zipinfo. This is a little application in there that's going to then take a file name and show you the contents of it.
So instead of typing the full path to this file here I'm going to simply drag and drop it into the Terminal window and it inserts the full path there. So now I can hit Return and I get all this information. Now it's a huge listing and notice that most file names are there twice. You'll see a regular version and then a version that has this MACOSX/docs/pages, you know, this whole thing with a little dot in front of the file name. This isn't the file itself. This is bits of information for the Finder about this file. Like say the icon and maybe some other information. I don't really need all that. I just want to know the files. I don't want to know all that extra bits of information in there.

So a better way to do this command, I'm going to use the up arrow to repeat the command, but I'm going to pipe it. So the little pipe character that's Shift and then the character above Return on American keyboards and then fgrep, that will send it through another program called fgrep, -V meaning don't include anything that's got underscore underscore MACOSX. Now when I do this it's going to do the same command, pipe it through fgrep, and remove all the those lines. So now I get the same thing here but it's nice and neat.

So I can see all the files that are inside of the Zip compressed archive. I also see the number of bytes so I can see which files are large. I can see dates for them as well which might be useful information. At the top here notice this is the command here at the top but I can see that it gives the name of it and it also tells me the size and the number of entries. So 128. At the bottom, the last line, is again 128 files and it says the number of bytes uncompressed and the number of bytes compressed and the savings. So only 2% savings here. It's not a great archive but it does actually get all those files into one place and kind of remove them from my searches. Still pretty valuable to be able to do it.

Now you can also do this in Automator. Let's take a look at that. So I'm going to run Automator and I'm going to create a new Automator workflow. Let's do it as a Service. That way it will be easy to select a file and then use it with the script. I'm going to have it receive files in the Finder because it only makes sense to do this in the Finder and I'm going to have it run a Shell Script because basically that's what, you know, I did here. That's a little Shell Script in Terminal. So I'm going to use the same thing.

I'm going to Pass input, instead of standard input here, stdin stands for standard input, I'm going to have it as arguments. It gives me this little sample script here. So it says whatever arguments get passed in, it's going to loop through them and echo them. Just kind of output them. Which is, you know, not exactly what I want but it does give me a framework. Because I don't want it to echo them I want it to do zipinfo for them. So that's a good start there. I also, in addition to zipinfo, I want to pipe it again through fgrep, for the same reason I want to get rid of all the files, all the entries, that have this piece of text in them because I don't need those.

Now one of the problems we have to deal with here is that running this in Automator it has no place to output it to. So we have to actually send the data that's output to something. So I'm going to again use the pipe command and pipe to, open dash f dash a, which will take what came from there and open it in and we'll do TextEdit. You can do Safari too. That works. So it's going to create a new TextEdit document with the results. Now I'll do Command S to save and I'll save it as zipinfo. Since I created it as a service I can now go back to the Finder and if I Control click here and I go to Services I'll see zipinfo appear there and I can select it. It will run the script, take the output, throw it into a TextEdit document which then I can view. Then, of course, TextEdit is nice because now I can search it if I want. Things like that. When I'm done I can close it and dismiss it.

So a quick and easy way now that I can go to any Zip file and actually look at the contents.

Comments: 2 Responses to “Listing Zip Archive Contents With Terminal and Automator”

    3/7/18 @ 3:36 pm

    Does this work with disk image files?

    3/7/18 @ 3:37 pm

    Chris: No, as zipinfo only handles .zip files.

Leave a New Comment Related to "Listing Zip Archive Contents With Terminal and Automator"




0/500 (500 character limit -- please state your comment succinctly and do not try to get around this limit by posting two comments)