Search code examples
bashshelldownloadzipwget

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


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.

Solution

  • (...)wget dont bother downloading the file if you have not given it a filename to save it as or a directory (i think)(...)

    This is not true, wget downloads file using name generated from given URL. In this particular case

    wget http://www.vim.org/scripts/download_script.php?src_id=11834
    

    download file as download_script.php@src_id=11834, which as you might observe is derived from part after last /, but rather not what you want to be. However in cases of link like this, there is often Content-Disposition tag informing web browser about name of file, as wget man page informs you might use --content-disposition flag in order to make wget use it, when you do

    wget --content-disposition http://www.vim.org/scripts/download_script.php?src_id=11834
    

    file will be downloaded as NERD_tree.zip