Search code examples
windowsmacoszipperforcecarriage-return

Zipping a text file on my mac destroys carriage control on Windows


I have a file on my Mac which has been run through unix2dos to provide Windows style carriage return. For example, here's the top part of the file:

$ od -c ../README.txt   | head
...
0000020                                                   R   E   A   D
0000040   M   E       f   i   l   e      \r  \n  \r  \n   F   o   r
0000060   c   o   m   p   l   e   t   e       i   n   s   t   r   u   c

Then I zip the file on the Mac using the built-in zip program in Lion. It is zip version 3.0.

But when I open this zip file on Windows, the \r characters have been removed.

$ od -c README.txt | head
...
0000020                                                   R   E   A   D
0000040   M   E       f   i   l   e      \n  \n   F   o   r       c   o

So in Notepad it appears smashed.

How can I prevent this from occurring? The concern is that since notepad is the default editor for .txt files on Windows, people will see the smashed version of the file.


Solution

  • Argh! The root cause was that my source management system (Perforce) was representing the file differently on Windows vs Mac. The first od that I showed above was done on Windows. I assumed Mac would be the same but it was not; it was the same as the second one.

    To resolve this, just before building my zip file on Mac, I used awk to make the file Windows-friendly:

    cat <sourcefile> | awk 'sub("$", "\r")' > README.txt 
    

    (You could use unix2dos but I don't have that installed on my build machine.)