zip command not working

ε祈祈猫儿з 提交于 2019-12-04 17:51:44

问题


I am trying to zip a file using shell script command. I am using following command:

  zip ./test/step1.zip $FILES

where $FILES contain all the input files. But I am getting a warning as follows

    zip warning: name not matched: myfile.dat

and one more thing I observed that the file which is at last in the list of files in a folder has the above warning and that file is not getting zipped.

Can anyone explain me why this is happening? I am new to shell script world.


回答1:


zip warning: name not matched: myfile.dat

This means the file myfile.dat does not exist.

You will get the same error if the file is a symlink pointing to a non-existent file.

As you say, whatever is the last file at the of $FILES, it will not be added to the zip along with the warning. So I think something's wrong with the way you create $FILES. Chances are there is a newline, carriage return, space, tab, or other invisible character at the end of the last filename, resulting in something that doesn't exist. Try this for example:

for f in $FILES; do echo :$f:; done

I bet the last line will be incorrect, for example:

:myfile.dat :

...or something like that instead of :myfile.dat: with no characters before the last :

UPDATE

If you say the script started working after running dos2unix on it, that confirms what everybody suspected already, that somehow there was a carriage-return at the end of your $FILES list.

od -c shows the \r carriage-return. Try echo $FILES | od -c




回答2:


Another possible cause that can generate a zip warning: name not matched: error is having any of zip's environment variables set incorrectly.

From the man page:

ENVIRONMENT
    The following environment variables are read and used by zip as described.

ZIPOPT
    contains default options that will be used when running zip.  The contents of this environment variable will get added to the command line just after the zip command.

ZIP
    [Not on RISC OS and VMS] see ZIPOPT

Zip$Options
    [RISC OS] see ZIPOPT

Zip$Exts
    [RISC OS] contains extensions separated by a : that will cause native filenames with one of the specified extensions to be added to the zip file with basename and extension swapped.

ZIP_OPTS
    [VMS] see ZIPOPT

In my case, I was using zip in a script and had the binary location in an environment variable ZIP so that we could change to a different zip binary easily without making tonnes of changes in the script.

Example:

ZIP=/usr/bin/zip
...
${ZIP} -r folder.zip folder

This is then processed as:

/usr/bin/zip /usr/bin/zip -r folder.zip folder

And generates the errors:

zip warning: name not matched: folder.zip
zip I/O error: Operation not permitted
zip error: Could not create output file (/usr/bin/zip.zip)

The first because it's now trying to add folder.zip to the archive instead of using it as the archive. The second and third because it's trying to use the file /usr/bin/zip.zip as the archive which is (fortunately) not writable by a normal user.

Note: This is a really old question, but I didn't find this answer anywhere, so I'm posting it to help future searchers (my future self included).




回答3:


eebbesen hit the nail in his comment for my case (but i cannot vote for comment). Another possible reason missed in the other comments is file exceeding the file size limit (4GB).




回答4:


I just discovered another potential cause for this. If the permissions of the directory/subdirectory don't allow the zip to find the file, it will report this error. Actually, if you run a chmod -R 444 on the directory, and then try to zip it, you will reproduce this error, and also have a "stored 0%" report, like this:

zip warning: name not matched: borrar/enviar adding: borrar/ (stored 0%)

Hence, try changing the permissions of the file. If you are trying to send them through email, and those email filters (like Gmail's) invent silly filters of not sending executables, don't forget that making permissions very strict when making zip compression can be the cause of the error you are reporting, of "name not matched".




回答5:


spaces are not allowed:

it would fail if there are more than one files(s) in $FILES unless you put them in loop




回答6:


I converted my script for unix environment using dos2unix command and executed my script as ./myscript.sh instead bash myscript.sh.



来源:https://stackoverflow.com/questions/20529851/zip-command-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!