40

I´d like to add a file in a zip file, with a different name, and avoiding the creation of a new file with the desired name.

For example, I´d like to add the file myfile.txt to a zip, but renaming it to myfile2.txt.

hakre
  • 193,403
  • 52
  • 435
  • 836
Luis Andrés García
  • 5,852
  • 10
  • 43
  • 57
  • 6
    Don't understand why is it offtopic – pihentagy Oct 02 '15 at 11:43
  • 16
    I also vote that this is not offtopic here. `zip` is a standard development tool to bundle releases. And changing the name of distributed files when bundeling them within a `Makefile` can be considered a very common practice. Hence if there is a solution which do not include copies into temporary files/directories this is a very welcome idiom to use to solve a pretty common programming task. – Tino Oct 13 '15 at 02:22
  • Voting to leave closed as a [work request](https://meta.stackoverflow.com/questions/274630/should-we-add-a-do-my-work-for-me-close-reason). – ivan_pozdeev Mar 31 '17 at 22:16
  • 1
    It seems quite arbitrary that this was closed as off-topic. There are plenty of questions about how to use the zip command on SO that haven't been closed as off-topic. I'm voting to reopen. Judging from all the upvotes, this is a good and useful question. – Erik B Nov 09 '21 at 12:25

1 Answers1

41

You can use zipnote which should come with the zip package.

First build the zip archive with the myfile.txt file:

zip archive.zip myfile.txt

Then rename myfile.txt inside the zip archive with:

printf "@ myfile.txt\n@=myfile2.txt\n" | zipnote -w archive.zip

(Thanks to Jens for suggesting printf instead of echo -e.)

A short explanation of "@ myfile.txt\n@=myfile2.txt\n":

From zipnote -h: "@ name" can be followed by an "@=newname" line to change the name

And \n separates the two commands.

Community
  • 1
  • 1
mkrnr
  • 842
  • 8
  • 17
  • 1
    Minor nitpick: `echo -e` is an unportable SysVism. I'd use `printf "@ myfile.txt\n@=myfile2.txt\n" | ...`. – Jens May 23 '13 at 13:44
  • Good point. Modified my answer. – mkrnr May 23 '13 at 14:01
  • I don't know why but I can make it work. See my question - http://stackoverflow.com/questions/22974139/how-to-use-zipnote-command – martin Apr 09 '14 at 21:37
  • 2
    Note: if you want to do multiple filename changes, you need to follow each chunk by `@ (comment above this line)\n` – qaisjp Feb 07 '16 at 12:17
  • >The temporary file format is rather specific and zipnote is rather picky about it. It should be easier to change file names in a script. Does not yet support large (> 2 GB) or split archives. – user877329 Nov 18 '16 at 10:25
  • 5
    how about changing a directory name? – Freedom Apr 27 '18 at 02:16
  • @Freedom Rather late, but I found that using relative paths like `"@ myDir/file.txt\n@=anotherDir/file.txt\n"` works. Not entirely sure if this is portable, although checking a random ZIP file I made without using the `zip` command revealed comments of the type `"@ path/to/file.ext"`. – logo_writer Jun 28 '19 at 09:06
  • zip 3.1 appears to be pretty uncommon (the one in Debian/Ubuntu is 3.0, and yes, it's 11 years old)... – David Given Oct 20 '19 at 22:42
  • @mkrnr @Jens This is working for normal filenames but it's not working for unicode chars like `Ю`. I need to replace that with `_` because some zip program on server(that i can't control) is converting all `_` to `Ю` after compressing(as shown in bash with `zipinfo -1 file.zip` locally) or in GUI showing as 009E in a box like █ (yes, it's \u009e) – Joy Aug 09 '22 at 13:07