A little job I had to do today was to write a script (unix) to zip a file before transmitting it to a Windows system. No problem, we have zip installed…
zip file.zip file.txt
Then they decided that it was in ASCII Latin1 and they wanted it in UTF-8. Again, no problem
iconv -f ISO-8859-1 -t UTF-8 file.txt | zip file.zip -
The snag is that zip includes not only the compressed file in the output, but also headers giving details of the file name etc. In the case above the input comes from a pipe and there is no file name so it creates an entry called -
(just a hyphen) which is not very useful. The unix unzip
command has an option to get around this.
unzip -p file.zip > file.utf8
or you can use funzip
if your implementation includes it.
funzip file.zip > file.utf8
I have no idea what they can do on Windows. I may have to do it the slow way and create an intermediate file with all the housekeeping, security and management that involves 🙁
Exercise for the reader: If you just unzip file.zip
from above it creates a file called -
. How do you do anything with it?
Hi
I just had the same problem. What you need to do is use mkfifo or mknod to create a named pipe whose name is the one you want in your zip archive.
so
# create a pipe
mkfifo file.txt
# start up zip in the background which reads from the pipe
zip archive.zip file.txt &
# run your program
a_program.exe > file.txt
Thanks very much Mark. I don’t think that I have ever used mkfifo before in 20 years of working with unix. It should be noted that you need to add a “fg” command to the end of your commands or some other method to terminate the background process. We eventually solved the problem a different way but that would have worked.
The actual problem I posed was how to deal with the file called – once you have managed to create it.
I have the exact problem you mention as an exercise. I’m sending the output from a mysqldump command to zip, but the name inside the archive is a hyphen. Could you please tell me how I can rename it in the same chain?
Hi Harry, the answer to my exercise is set out below but I originally left it out because there is no need to get into that position. unzip -p or funzip will extract the file from a zip whatever it is called and Mark’s solution will create a zip with the proper file name inside. However, if for some reason you do end up with a file called “
-
” then what you need to do isls -i -
The
ls
command will accept the hyphen as a file name and give you the inode number. Thenfind . -inum <number> -exec mv {} <newname> \;
Thanks for the reply. I ended up doing it in two commands as I couldn’t get it to work in just one. Here is the command that was giving me headaches (run from a php file with exec($command):
$command = “/opt/lampp/bin/mysqldump -h “. DB_HOST . ” -u ” . DB_USER . ” –password=” . DB_PASS . $flag . DB_NAME . ” $tables | zip > $zipFile”;
I ended up dumping the result in an sql file and archiving it afterwords. I can’t see how I can integrate your solution in my problem. If you have any idea, please let me know. I should mention that I am fresh to the unix environment.
Yes, I agree. Your problem is that running from php you would have difficulty setting up, using and then breaking the background process from Mark’s solution. And, anyway, it is all rather complex and opaque. If you can create and delete a temporary file, that is a much clearer method.