Advertisement
apieceoffruit

File Versioning

Mar 13th, 2023
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.41 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using static System.Text.Encoding;
  5.  
  6. namespace JasonStorey
  7. {
  8.     /// <summary>
  9.     /// This class provides functions to manage versioning of files. It includes functions to write, check, change and strip headers, as well as to read the file content without the header and to write the header to a file.
  10.     /// </summary>
  11.     public static class FileVersioning
  12.     {
  13.         /// <summary>
  14.         /// Writes a version header to a file.
  15.         /// </summary>
  16.         /// <param name="filePath">The path to the file.</param>
  17.         /// <param name="keyword">The keyword used to calculate the header value.</param>
  18.         /// <param name="version">The version number to write to the header.</param>
  19.         public static void WriteVersionToFile(string filePath,string keyword, int version)
  20.         {
  21.             int asciiNumber = keyword.Sum(c => c);
  22.             using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  23.             var bytesToWrite = new byte[3];
  24.             bytesToWrite[0] = (byte)asciiNumber;
  25.             bytesToWrite[1] = (byte)(version >> 8);
  26.             bytesToWrite[2] = (byte)version;
  27.             fileStream.Write(bytesToWrite, 0, 3);
  28.         }
  29.        
  30.         /// <summary>
  31.         /// Checks if a file has a valid version header and returns the version number.
  32.         /// </summary>
  33.         /// <param name="filePath">The path to the file.</param>
  34.         /// <param name="keyword">The keyword used to calculate the expected header value.</param>
  35.         /// <param name="version">The version number found in the header.</param>
  36.         /// <returns>True if the file has a valid header, false otherwise.</returns>
  37.         public static bool CheckFileVersion(string filePath,string keyword, out int version)
  38.         {
  39.             version = 0;
  40.             if (!File.Exists(filePath))
  41.                 return false;
  42.             using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  43.             byte[] headerBytes = new byte[3];
  44.             int bytesRead = fileStream.Read(headerBytes, 0, 3);
  45.             if (bytesRead < 3)
  46.                 return false;
  47.             int asciiNumber = headerBytes[0];
  48.             if (!asciiNumber.Equals(keyword.Sum(c => c))) return false;
  49.             int versionBytes = (headerBytes[1] << 8) | headerBytes[2];
  50.             version = versionBytes;
  51.             string headerString = ASCII.GetString(headerBytes).Aggregate("", (current, c) => current + c);
  52.             string expectedHeader = asciiNumber.ToString("D3") + versionBytes.ToString("D5");
  53.             return headerString == expectedHeader;
  54.         }
  55.        
  56.         /// <summary>
  57.         /// Changes the version number in the header of a file.
  58.         /// </summary>
  59.         /// <param name="filePath">The path to the file.</param>
  60.         /// <param name="keyword">The keyword used to check the header value.</param>
  61.         /// <param name="newVersion">The new version number to write to the header.</param>
  62.         /// <returns>True if the file has a valid header and the version number was changed, false otherwise.</returns>
  63.         public static bool ChangeFileVersion(string filePath,string keyword, int newVersion)
  64.         {
  65.             if (!CheckFileVersion(filePath,keyword, out _))
  66.                 return false;
  67.             using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Write);
  68.             var versionBytes = BitConverter.GetBytes(newVersion);
  69.             fileStream.Seek(1, SeekOrigin.Begin);
  70.             fileStream.Write(versionBytes, 0, 2);
  71.             return true;
  72.         }
  73.        
  74.        
  75.         /// <summary>
  76.         /// Removes the version information and keyword header from a file.
  77.         /// </summary>
  78.         /// <param name="filePath">The path of the file to modify.</param>
  79.         /// <param name="keyword">The keyword used for the file header.</param>
  80.         /// <returns>True if the file exists </returns>
  81.         public static bool StripHeaderFromFile(string filePath,string keyword)
  82.         {
  83.             if (!CheckFileVersion(filePath,keyword, out _))
  84.                 return false;
  85.             byte[] fileBytes = File.ReadAllBytes(filePath);
  86.             byte[] newFileBytes = new byte[fileBytes.Length - 3];
  87.             Array.Copy(fileBytes, 3, newFileBytes, 0, newFileBytes.Length);
  88.             using var fileStream = new FileStream(filePath, FileMode.Truncate);
  89.             fileStream.Write(newFileBytes, 0, newFileBytes.Length);
  90.             return true;
  91.         }
  92.        
  93.         /// <summary>
  94.         /// Reads the content of a file at the specified file path without the header and returns it as a string.
  95.         /// </summary>
  96.         /// <param name="filePath">The path of the file to be read.</param>
  97.         /// <param name="keyword">The keyword associated with the header of the file.</param>
  98.         /// <param name="expectedVersion">The expected version of the file.</param>
  99.         /// <returns>The content of the file as a string.</returns>
  100.         public static string ReadFileTextWithoutHeader(string filePath,string keyword, int expectedVersion) =>
  101.             ASCII.GetString(ReadFileWithoutHeader(filePath,keyword,expectedVersion));
  102.  
  103.         /// <summary>
  104.         /// Reads the content of a file at the specified file path without the header and returns it as a byte array.
  105.         /// </summary>
  106.         /// <param name="filePath">The path of the file to be read.</param>
  107.         /// <param name="keyword">The keyword associated with the header of the file.</param>
  108.         /// <param name="expectedVersion">The expected version of the file.</param>
  109.         /// <returns>The content of the file as a byte array.</returns>
  110.         public static byte[] ReadFileWithoutHeader(string filePath,string keyword, int expectedVersion)
  111.         {
  112.             if (!CheckFileVersion(filePath, keyword,out var currentVersion) || currentVersion != expectedVersion)
  113.                 return null;
  114.             byte[] fileBytes = File.ReadAllBytes(filePath);
  115.             byte[] contentBytes = new byte[fileBytes.Length - 3];
  116.             Array.Copy(fileBytes, 3, contentBytes, 0, contentBytes.Length);
  117.             return contentBytes;
  118.         }
  119.        
  120.         /// <summary>
  121.         /// Writes a header to a file at the specified file path.
  122.         /// </summary>
  123.         /// <param name="filePath">The path of the file to be written.</param>
  124.         /// <param name="keyword">The keyword to be associated with the header of the file.</param>
  125.         /// <param name="version">The version of the file.</param>
  126.         public static void WriteHeaderToFile(string filePath, string keyword,int version)
  127.         {
  128.             byte[] versionBytes = ASCII.GetBytes(version.ToString());
  129.             byte[] keywordBytes = ASCII.GetBytes(keyword);
  130.             byte[] fileBytes = File.ReadAllBytes(filePath);
  131.             byte[] newFileBytes = new byte[versionBytes.Length + keywordBytes.Length + fileBytes.Length];
  132.             Array.Copy(versionBytes, 0, newFileBytes, 0, versionBytes.Length);
  133.             Array.Copy(keywordBytes, 0, newFileBytes, versionBytes.Length, keywordBytes.Length);
  134.             Array.Copy(fileBytes, 0, newFileBytes, versionBytes.Length + keywordBytes.Length, fileBytes.Length);
  135.             using var fileStream = new FileStream(filePath, FileMode.Create);
  136.             fileStream.Write(newFileBytes, 0, newFileBytes.Length);
  137.         }
  138.        
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement