1

I am removing the Carriage return in a file by converting the line endings in a file from DOS to UNIX format in C#. This basically means that I am removing the Carriage Return in the file format.

The code I use:

private void Dos2Unix(string fileName)
{
    const byte CR = 0x0D;
    const byte LF = 0x0A;
    byte[] data = File.ReadAllBytes(fileName);
    using (FileStream fileStream = File.OpenWrite(fileName))
    {
        BinaryWriter bw = new BinaryWriter(fileStream);
        int position = 0;
        int index = 0;
        do
        {
            index = Array.IndexOf<byte>(data, CR, position);
            if ((index >= 0) && (data[index + 1] == LF))
            {
                // Write before the CR
                bw.Write(data, position, index - position);
                // from LF
                position = index + 1;
            }
        }
        while (index > 0);
        bw.Write(data, position, data.Length - position);
        fileStream.SetLength(fileStream.Position);
    }
}

But after I converted from DOS to Unix format, I need to create a tar archive of all the converted files. When I create a tar archive of the files using this code:

batchFileContents[1] = String.Format("\"C:\\Program Files (x86)\\7-Zip\\7z.exe\" a -ttar -so archive.tar \"{0}\"* | " +
                        "\"C:\\Program Files (x86)\\7-Zip\\7z.exe\" a -si \"{1}\"", inputDirectory, nameOfFile);

File.WriteAllLines("execute.bat", batchFileContents);

The carriage return character reappears in all the files.

dos2unix function above is able to remove the carriage return. but the problem is that when tar to create the archive, the carriage return appears again. how to avoid this?

How do I solve this problem? Need some guidance.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • I recommend installing cygwin and using **dos2unix** to remove carriage return, and **tar** to create the archive. – Rumbleweed Feb 05 '16 at 03:12
  • dos2unix function is able to remove the carriage return. but the problem is that when tar to create the archive, the carriage return appears again. how to avoid this. – lakshmen Feb 05 '16 at 03:13
  • Tar does not modify files. 7z is your culprit. – Rumbleweed Feb 07 '16 at 14:55

2 Answers2

0

Why use 7zip to create the tar archive? Ultimately you can't control the behavior of a third party program.

You could try https://code.google.com/archive/p/tar-cs/ which is a library to create tar archives from C# directly, and it's opensource. If it ends up adding a carriage return, you can try another library or look into the source your self to see why.

caesay
  • 16,932
  • 15
  • 95
  • 160
  • But my question is when I do the tar in windows, it will still add the carriage return. How to avoid this? – lakshmen Feb 05 '16 at 03:17
  • 1
    A carriage return doesn't just get added automatically, something has to put it there after you've removed it. You're using `7z.exe` to create the tar archive, what I'm saying is to use something else to create the tar achive that might not add a carriage return like 7zip does in this case. – caesay Feb 05 '16 at 03:26
-1

First, avoid reinventing the (square) wheel:

Now, the tar format is by definition just files glued together with minimal metadata. There's no data conversion involved. So, your problem lies elsewhere - maybe you're archiving wrong files or inspecting an unfaithful representation of archived data.

Community
  • 1
  • 1
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152