ContainerList_FileInfoX.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Fire.Container.Exception;
  7. using System.IO;
  8. namespace System.Fire.Container {
  9. /// <summary>
  10. /// The FileInfoX object contains basic informations about a file
  11. /// </summary>
  12. public struct FileInfoX {
  13. private string _name;
  14. private string _mime;
  15. private string _extention;
  16. /// <summary>
  17. /// Creates a new FileInfoX Opbject
  18. /// </summary>
  19. /// <param name="FileName">The FileName</param>
  20. public FileInfoX(string FileName) {
  21. this._name = FileName;
  22. this._mime = System.Fire.Mime.getMimeType(FileName);
  23. this._extention = "";
  24. if (FileName.Contains(".") && FileName.LastIndexOf(".") > 0 && FileName.LastIndexOf(".") < FileName.Length) {
  25. this._extention = FileName.Substring(FileName.LastIndexOf(".") + 1).ToUpper();
  26. }
  27. }
  28. /// <summary>
  29. /// Get or set the file name
  30. /// </summary>
  31. public string Name {
  32. get {
  33. return this._name;
  34. }
  35. set {
  36. this._name = value;
  37. }
  38. }
  39. /// <summary>
  40. /// Get or set the file mime
  41. /// </summary>
  42. public string Mime {
  43. get {
  44. return this._mime;
  45. }
  46. set {
  47. this._mime = value;
  48. }
  49. }
  50. /// <summary>
  51. /// Get or set the file extention
  52. /// </summary>
  53. public string Extention {
  54. get {
  55. return this._extention;
  56. }
  57. set {
  58. this._extention = value;
  59. }
  60. }
  61. }
  62. public partial class ContainerList {
  63. private FileStream InfoFile;
  64. private BinaryReader InfoFileR;
  65. private BinaryWriter InfoFileW;
  66. private long FileInfo_Offset;
  67. private FileInfoX ReadFileInfo(long pos) {
  68. FileInfoX nfo = new FileInfoX();
  69. this.InfoFile.Position = pos;
  70. byte version = this.InfoFileR.ReadByte();
  71. if (version == 1) {
  72. byte nameLen = this.InfoFileR.ReadByte();
  73. byte[] tmp_name = this.InfoFileR.ReadBytes(nameLen);
  74. nfo.Name = System.Text.Encoding.UTF8.GetString(tmp_name);
  75. byte mimeLen = this.InfoFileR.ReadByte();
  76. byte[] tmp_mime = this.InfoFileR.ReadBytes(mimeLen);
  77. nfo.Mime = System.Text.Encoding.UTF8.GetString(tmp_mime);
  78. byte extLen = this.InfoFileR.ReadByte();
  79. byte[] tmp_ext = this.InfoFileR.ReadBytes(extLen);
  80. nfo.Extention = System.Text.Encoding.UTF8.GetString(tmp_ext);
  81. }
  82. return nfo;
  83. }
  84. private long WriteFileInfo(FileInfoX nfo) {
  85. this.InfoFile.Seek(0, SeekOrigin.End);
  86. long pos = InfoFile.Position;
  87. byte version = 1;
  88. this.InfoFileW.Write(version);
  89. byte[] tmp_name = System.Text.Encoding.UTF8.GetBytes(nfo.Name);
  90. this.InfoFileW.Write((byte)tmp_name.Length);
  91. this.InfoFileW.Write(tmp_name);
  92. byte[] tmp_mime = System.Text.Encoding.UTF8.GetBytes(nfo.Mime);
  93. this.InfoFileW.Write((byte)tmp_mime.Length);
  94. this.InfoFileW.Write(tmp_mime);
  95. byte[] tmp_ext = System.Text.Encoding.UTF8.GetBytes(nfo.Extention);
  96. this.InfoFileW.Write((byte)tmp_ext.Length);
  97. this.InfoFileW.Write(tmp_ext);
  98. return pos;
  99. }
  100. private void OpenFileInfo(string Path) {
  101. bool bHasFileInfo = false;
  102. if (System.IO.File.Exists(Path)) {
  103. this.InfoFile = new FileStream(Path, FileMode.Open);
  104. if (this.InfoFile.Length > 6) {
  105. this.InfoFileR = new BinaryReader(this.InfoFile);
  106. this.InfoFileW = new BinaryWriter(this.InfoFile);
  107. byte[] Magic = this.InfoFileR.ReadBytes(4);
  108. if (
  109. Magic[0] == 67 /* C */ &&
  110. Magic[1] == 111 /* o */ &&
  111. Magic[2] == 70 /* F */ &&
  112. Magic[3] == 73 /* I */
  113. ) {
  114. ushort version = this.InfoFileR.ReadUInt16();
  115. bHasFileInfo = true;
  116. this.FileInfo_Offset = this.InfoFile.Position;
  117. }
  118. }
  119. if (bHasFileInfo == false) {
  120. this.InfoFile.Close();
  121. File.Delete(Path);
  122. }
  123. }
  124. if (!bHasFileInfo) {
  125. this.InfoFile = new FileStream(Path, FileMode.CreateNew);
  126. this.InfoFileR = new BinaryReader(this.InfoFile);
  127. this.InfoFileW = new BinaryWriter(this.InfoFile);
  128. this.InfoFileW.Write(new byte[] { 67, 111, 70, 73 });
  129. this.InfoFileW.Write(Version);
  130. this.FileInfo_Offset = this.InfoFile.Position;
  131. }
  132. }
  133. private void CloseFileInfo() {
  134. this.InfoFile.Close();
  135. }
  136. }
  137. }