解压缩:如何传递错误?(Unzip: how to pass error?)

编程入门 行业动态 更新时间:2024-10-21 16:22:59
解压缩:如何传递错误?(Unzip: how to pass error?)

给定一个生成100个.zip文件的脚本,其中一些是空的。 (即使没有任何内容可以压缩,我的脚本会生成100个.zip文件)。

鉴于命令:

unzip *.zip

我在空文件上收到错误:

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.

我怎样才能绕过这些假的.zip文件并沉默这个unzip错误?

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 ?

最满意答案

核心解决方案

使用bash ,你可以循环遍历zip文件而不是使用glob。 这允许防止rm,cp,mv命令的参数列表太长错误,并且即使一个文件失败也会继续。

dir="/path/to/zip-dir" cd "$dir" for zf in *.zip do unzip "$zf" done

在做之前测试

您可以在for循环中添加额外的测试,以在解压缩之前检查文件是否存在且大小是否大于零

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

较短的版本将是: [[ -s "$zf" ]] && unzip "$zf" (仅当存在时解压缩且> 0)。

参考

请参阅man bash (查找-s或CONDITIONAL EXPRESSIONS部分)。 问题rm,cp,mv命令的参数列表太长错误

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

see man bash (look for -s or CONDITIONAL EXPRESSIONS section). Question Argument list too long error for rm, cp, mv commands

更多推荐

本文发布于:2023-07-04 17:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1026909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:解压缩   错误   Unzip   pass   error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!