Home > database >  How can I know if its possible to unzip a file or not with wget?
How can I know if its possible to unzip a file or not with wget?

Time:09-15

wget http://www.vim.org/scripts/download_script.php?src_id=11834 -O temp.zip; unzip temp.zip

Works but what if the file I am downloading is not a zip so it needs to retain the original name and not temp.zip. For example instead of temp.zip its called temp.7z So its not possible to unzip it in bash. So if the file is 7z then do not make wget rename it to temp.zip after downloading. And I'd thought of something like this but you cant get the filename to what you download with wget.

filename = wget http://www.vim.org/scripts/download_script.php?src_id=11834
unzip filename || echo "Must be a .zip file in order to unzip it!"

But this obviously does not work since wget dont bother downloading the file if you have not given it a filename to save it as or a directory (i think) and will give this error:

unzip:  cannot find or open wget, wget.zip or wget.ZIP.

CodePudding user response:

Save downloaded file with a temp name and test file type with file command

t="$(file --brief --mime-type file.tmp)"

case "$t" in
    'application/zip')
        echo "zip file"
        ;;
    'application/x-7z-compressed')
        echo "7z file"
        ;;
    *)
        echo "unknown file type"
        ;;
esac
  • Related