我通過將文件中的行結尾從DOS轉換爲UNIX格式的C#文件中刪除了Carriage return。這基本上意味着我正在刪除文件格式的回車符。在Windows中創建一個tar歸檔文件重新引入了CR字符
的代碼我使用:
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);
}
}
但經過我轉換,從DOS到Unix的格式,我需要創建所有轉換後的文件的一個tar歸檔。當我使用此代碼創建文件的tar歸檔文件時:
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);
回車符將在所有文件中重新出現。
上面的dos2unix函數能夠去掉回車符。但問題是,當tar創建存檔時,回車再次出現。如何避免這一點?
我該如何解決這個問題?需要一些指導。
我建議安裝cygwin並使用** dos2unix **刪除回車符,** tar **創建歸檔文件。 – Rumbleweed
dos2unix函數能夠刪除回車符。但問題是,當tar創建存檔時,回車再次出現。如何避免這一點。 – lakesh
Tar不會修改文件。 7z是你的罪魁禍首。 – Rumbleweed