You are not logged in.

#1 2012-01-28 02:42:13

inkdinky
Member
Registered: 2011-12-08
Posts: 66

bash: unzip file and move into directory if directory created

I'm writing a script that decompresses zipped/rared files.
The zips typically have content in a one directory or not contained in a directory.
Is there a way to see if the unzip process created a directory and move into it or if one wasn't created to operate on the files extracted in the PWD?
So far the rar files have only had data that gets extracted to PWD, but if I see some with files in directories I'd like for a solution that might work in that case as well.

Offline

#2 2012-01-28 03:31:00

thisoldman
Member
From: Pittsburgh
Registered: 2009-04-25
Posts: 1,172

Re: bash: unzip file and move into directory if directory created

Would zipinfo' help?  It lists zip archive files in a format similar to 'ls -l'.

I only tried this on two zipfiles, and I'm concerned about how foolproof this may or may not be.

Zipinfo lists the files in the opposite order that they were placed into the zipfile, I think.  If the first file placed into the zip was the parent directory, and so contains all the other files, the listing will be something like that shown below, with the container directory always on the next-to-last line of zipinfo's output:

...
-rw-a--     2.0 fat     1057 tx defX 09-Nov-12 17:56 examples/README.txt
drwx---     2.0 fat        0 bx stor 09-Nov-12 16:48 examples/
42 files, 508682 bytes uncompressed, 133398 bytes compressed:  73.8%

So one would have to test if the next-to-last line of zipinfo's output begins with 'd' (directory) or '-' (regular file).

Off the top of my head, with not-so great command-line skills, I came up with:

zipinfo example.zip | tail -n 2 | grep '^d'

Offline

#3 2012-01-29 16:59:22

inkdinky
Member
Registered: 2011-12-08
Posts: 66

Re: bash: unzip file and move into directory if directory created

Yeah thats a possibility. I know that you can also do

unzip -l filename.zip 

and it'll print a listing as well.
I'm really trying to understand how CBR/CBZ comic readers do it as that is the target zip archive that I'm trying to extract and modify.
I'm trying to look at some code now.
And a python/perl solution also would also be a possibility if anyone knows how to do that as well.

Offline

Board footer

Powered by FluxBB