Swifting
Swifting

Reputation: 479

Extracting byte sizes of files within a zip file

I have a archive.zip containing some files, per file I want to know the amount of bytes it has without extracting the zip. Per example, I need to look if the file has less then 100 bytes, and if so do some stuff. So I would end up with something like:

BYTELIST=???
for bytes in ${BYTELIST}; do
    if [[ ${bytes} -lt 100 ]]; then
        echo "Hello" 
    fi
done

For only the names of the files in the zip I would do:

NAMELIST=$(zipinfo -1 archive.zip)

Is there an equivalent for the bytes? I know you can do "zipinfo -l" for all the fields. But how would we manipulate this to only get a list of the bytes?

Upvotes: 0

Views: 512

Answers (2)

Ipor Sircer
Ipor Sircer

Reputation: 3141

zipinfo archive.zip |grep ^-|tr -s " " "\t"|cut -f4,9

Upvotes: 1

João Miranda
João Miranda

Reputation: 124

Since zipinfo doesn't give you that information alone, you'll have to process what you have available.

Example of information returned:

$ zipinfo archive.zip 
Archive:  archive.zip
Zip file size: 486 bytes, number of entries: 3
-rw-r--r--  3.0 unx        6 tx stor 17-Feb-16 15:18 file1.txt
-rw-r--r--  3.0 unx       12 tx defN 17-Feb-16 15:19 file2.txt
-rw-r--r--  3.0 unx       18 tx defN 17-Feb-16 15:19 file3.txt
3 files, 36 bytes uncompressed, 26 bytes compressed:  27.8%

The bytes are in the 4th column, so, for a list of the bytes, you can do:

$ zipinfo archive.zip | grep "^\-" | sed 's/  */ /g' | cut -f4 -d ' '
6
12
18

In case you need other columns just select them, like the name of the corresponding files:

$ zipinfo archive.zip | grep "^\-" | sed 's/  */ /g' | cut -f4,9 -d ' '
6 file1.txt
12 file2.txt
18 file3.txt

Upvotes: 1

Related Questions