Store file in zip archive with different name (linux command shell)

Solution 1:

Does creating a hard link to file.txt count?

ln file.txt file2.txt

Create file2.txt which points to the exact same inode as file.txt, without actually doubling the space

Solution 2:

See https://stackoverflow.com/questions/16710341/linux-zip-command-add-a-file-with-different-name

The solution below is the exact copy of the answer of @mkrnr on stackoverflow

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.

Solution 3:

Hy there, this is my first answer so I hope I've done everything correct :-)

Here's my solution to your problem, a nice one-liner:

cp file.txt file2.txt | zip -mqj archive.zip file2.txt

Hope I could help!