1

Given a script generating 100 .zip files, of which some are empty. (Even if there is nothing to zip, my script generate 100 .zip files).

Given the command :

unzip *.zip

I get an error on empty files :

unzip:  cannot find zipfile directory in one of ./data/Archive_03.zip or
        ./data/Archive_03.zip.zip, and cannot find ./data/Archive_03.zip.ZIP, period.

How can I bypass these fake .zip files and silent this unzip error ?

Hugolpz
  • 17,296
  • 26
  • 100
  • 187
  • You can hide errors with `unzip *.zip 2>/dev/null`, but don't know if it suffices to you. – fedorqui Mar 27 '14 at 13:10
  • Nope. Still crashing. – Hugolpz Mar 27 '14 at 13:13
  • Not programming related. Try SuperUser. – DevSolar Mar 27 '14 at 13:47
  • 1
    The core of your problem is that, by default, `unzip` interprets only the first parameter (e.g. `Archive_01.zip`) to refer to an archive. Any subsequent parameter (e.g. `Archive_02.zip`, `Archive_03.zip` etc.) is interpreted to refer to a file *inside* `Archive_01.zip` that should be extracted from it. The answers show you how to avoid the problem. – DevSolar Mar 28 '14 at 13:33

2 Answers2

3

Core solution

Using bash, you can just loop over the zip files instead of using a glob. This allows to prevent the Argument list too long error for rm, cp, mv commands and continue even if one file fail.

dir="/path/to/zip-dir"
cd "$dir"

for zf in *.zip
do
  unzip "$zf"
done

Testing before doing

You can add an extra test, in the for loop, to check if the file exists and has a size greater than zero before unzipping:

if [[ -s "$zf" ]];
then
  unzip "$zf" # greater exist & size>0
else
  printf "file doesn't exists or null-size: %s\n" "$zf"
fi

a shorter version will be: [[ -s "$zf" ]] && unzip "$zf" (unzip only if exists and >0).

Reference

Community
  • 1
  • 1
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
1

Option 1: use it with care - http://www.manpagez.com/man/1/unzip/

unzip -fo *.zip

Option 2: This may not work, it works for .gz extension.

tar -zxvf test123.tar.gz

z -- unzip
x -- extract the file
v -- verbose
f -- forcefully done
Ronak Patel
  • 3,819
  • 1
  • 16
  • 29