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.