I want to compress the contents of stdin using zip
, for instance:
echo 'foo bar' | zip > file.zip
This works ok, but when unzipping, the uncompressed file name is -
I was wondering how I could specify a file name for stdin?
I want to compress the contents of stdin using zip
, for instance:
echo 'foo bar' | zip > file.zip
This works ok, but when unzipping, the uncompressed file name is -
I was wondering how I could specify a file name for stdin?
What you can do is pipe the file in normally, then rename it in the zip
echo 'foo bar' | zip > file.zip
printf "@ -\n@=filename.txt\n" | zipnote -w file.zip
Use a fifo (a named pipe) instead of piping stdin directly!
mkfifo text.txt # create fifo
echo 'foo bar' > text.txt & # pipe data to fifo, in background
zip --fifo file.zip text.txt # note the `--fifo` argument to zip
rm text.txt # cleanup
Result:
$ unzip -l file.zip
Archive: file.zip
Length Date Time Name
--------- ---------- ----- ----
8 2020-11-29 12:30 text.txt
--------- -------
8 1 file
Note that the fifo is a named pipe. Just like the anonymous pipe - the famous |
- no data is stored on your hard drive, it is streamed directly from writer to reader.
Write echo output to the desired filename, then use the -m flag to zip and remove the original file in one step.
echo 'foo bar' > filename.txt && zip -m filename.txt.zip filename.txt
echo 'foo bar' | zip -@ /path/to/zipfile.zip
should do it while keeping filename integrity