LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   replacing a file in a zip from command line (http://www.linuxquestions.org/questions/linux-general-1/replacing-a-file-in-a-zip-from-command-line-420052/)

davee 02-27-2006 09:43 AM

replacing a file in a zip from command line
 
I'm using zip and unzip to set parameters on the fly in some java war files.

I can use the following:

unzip -p archive.war WEB-INF/web.xml | sed -e 's/ConfigFileLocation/helloworld/g' | zip -r archive.war -

The unzip works 100%
The sed works 100%

but...
The zip adds the edit to the file as a file called '-'

Using anything but '-' as the final parameter to zip gives the following:

zip error: Nothing to do!

Any ideas?

Davee

P.S. A war file is a correctly formatted zip file, by the way. :-)

gilead 02-27-2006 01:11 PM

I don't think you need the -r flag. It means recursive and you're not recursing a directory structure. The man page gives the example:
Code:

tar cf - . | zip backup -
So, you could try:
Code:

zip archive.war -
I haven't tried it yet though.

davee 02-28-2006 02:54 AM

Hi Gilead,

Thanks for that - I thought the same; but -r means 'replace' (-R is resursive). Without the -r I get the same error:

zip error: Nothing to do! (archive.war)

The only time I don't seem to get this error is wth 'zip -r archive.war -', which, as I say creates a file in the image called '-'

Any other ideas? This is really bugging me now- I can't believe unzip can work on the fly but zip cannot...

gilead 02-28-2006 01:06 PM

I think we're using different zip programs then. From the man zip page on my system:
Code:

      -r    Travel the directory structure recursively; for example:

                    zip -r foo foo

              In this case, all the files and directories in foo are saved in a zip  archive  named  foo.zip,
              including  files  with  names  starting  with ".", since the recursion does not use the shell's
              file-name substitution mechanism.  If you wish to include only a specific subset of  the  files
              in  directory  foo and its subdirectories, use the -i option to specify the pattern of files to
              be included.  You should not use -r with the name ".*", since that  matches  ".."  which  will
              attempt to zip up the parent directory (probably not what was intended).

      -R    Travel the directory structure recursively starting at the current directory; for example:

                    zip -R foo '*.c'

              In  this  case,  all  the  files matching *.c in the tree starting at the current directory are
              stored into a zip archive named foo.zip.  Note for PKZIP users: the equivalent command is

                    pkzip -rP foo *.c

Actually, thinking about this some more, it's a more complicated problem that I originally thought. With the unzip -p archive.war WEB-INF/web.xml you're extracting to stdout so that sed -e 's/ConfigFileLocation/helloworld/g' can modify a stream. What you want to do next is replace the contents of the file WEB-INF/web.xml in the archive with that stream. But you have no file, you have a stream with no file name.

I haven't tried it again, but it may be that you have to create the file (and the path to it?) and then replace the file in the archive with that. Having the path preserved complicates it slightly more than a flat archive would - but you can't get away from that structure with the .war file.

Sorry I couldn't solve it.


All times are GMT -5. The time now is 11:14 AM.