| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Fire.Container.Exception;
- using System.IO;
- namespace System.Fire.Container {
- /// <summary>
- /// The FileInfoX object contains basic informations about a file
- /// </summary>
- public struct FileInfoX {
- private string _name;
- private string _mime;
- private string _extention;
- /// <summary>
- /// Creates a new FileInfoX Opbject
- /// </summary>
- /// <param name="FileName">The FileName</param>
- public FileInfoX(string FileName) {
- this._name = FileName;
- this._mime = System.Fire.Mime.getMimeType(FileName);
- this._extention = "";
- if (FileName.Contains(".") && FileName.LastIndexOf(".") > 0 && FileName.LastIndexOf(".") < FileName.Length) {
- this._extention = FileName.Substring(FileName.LastIndexOf(".") + 1).ToUpper();
- }
- }
- /// <summary>
- /// Get or set the file name
- /// </summary>
- public string Name {
- get {
- return this._name;
- }
- set {
- this._name = value;
- }
- }
- /// <summary>
- /// Get or set the file mime
- /// </summary>
- public string Mime {
- get {
- return this._mime;
- }
- set {
- this._mime = value;
- }
- }
- /// <summary>
- /// Get or set the file extention
- /// </summary>
- public string Extention {
- get {
- return this._extention;
- }
- set {
- this._extention = value;
- }
- }
- }
- public partial class ContainerList {
- private FileStream InfoFile;
- private BinaryReader InfoFileR;
- private BinaryWriter InfoFileW;
- private long FileInfo_Offset;
- private FileInfoX ReadFileInfo(long pos) {
- FileInfoX nfo = new FileInfoX();
- this.InfoFile.Position = pos;
- byte version = this.InfoFileR.ReadByte();
- if (version == 1) {
- byte nameLen = this.InfoFileR.ReadByte();
- byte[] tmp_name = this.InfoFileR.ReadBytes(nameLen);
- nfo.Name = System.Text.Encoding.UTF8.GetString(tmp_name);
- byte mimeLen = this.InfoFileR.ReadByte();
- byte[] tmp_mime = this.InfoFileR.ReadBytes(mimeLen);
- nfo.Mime = System.Text.Encoding.UTF8.GetString(tmp_mime);
- byte extLen = this.InfoFileR.ReadByte();
- byte[] tmp_ext = this.InfoFileR.ReadBytes(extLen);
- nfo.Extention = System.Text.Encoding.UTF8.GetString(tmp_ext);
- }
- return nfo;
- }
- private long WriteFileInfo(FileInfoX nfo) {
- this.InfoFile.Seek(0, SeekOrigin.End);
- long pos = InfoFile.Position;
- byte version = 1;
- this.InfoFileW.Write(version);
- byte[] tmp_name = System.Text.Encoding.UTF8.GetBytes(nfo.Name);
- this.InfoFileW.Write((byte)tmp_name.Length);
- this.InfoFileW.Write(tmp_name);
- byte[] tmp_mime = System.Text.Encoding.UTF8.GetBytes(nfo.Mime);
- this.InfoFileW.Write((byte)tmp_mime.Length);
- this.InfoFileW.Write(tmp_mime);
- byte[] tmp_ext = System.Text.Encoding.UTF8.GetBytes(nfo.Extention);
- this.InfoFileW.Write((byte)tmp_ext.Length);
- this.InfoFileW.Write(tmp_ext);
- return pos;
- }
- private void OpenFileInfo(string Path) {
- bool bHasFileInfo = false;
- if (System.IO.File.Exists(Path)) {
- this.InfoFile = new FileStream(Path, FileMode.Open);
- if (this.InfoFile.Length > 6) {
- this.InfoFileR = new BinaryReader(this.InfoFile);
- this.InfoFileW = new BinaryWriter(this.InfoFile);
- byte[] Magic = this.InfoFileR.ReadBytes(4);
- if (
- Magic[0] == 67 /* C */ &&
- Magic[1] == 111 /* o */ &&
- Magic[2] == 70 /* F */ &&
- Magic[3] == 73 /* I */
- ) {
- ushort version = this.InfoFileR.ReadUInt16();
- bHasFileInfo = true;
- this.FileInfo_Offset = this.InfoFile.Position;
- }
- }
- if (bHasFileInfo == false) {
- this.InfoFile.Close();
- File.Delete(Path);
- }
- }
- if (!bHasFileInfo) {
- this.InfoFile = new FileStream(Path, FileMode.CreateNew);
- this.InfoFileR = new BinaryReader(this.InfoFile);
- this.InfoFileW = new BinaryWriter(this.InfoFile);
- this.InfoFileW.Write(new byte[] { 67, 111, 70, 73 });
- this.InfoFileW.Write(Version);
- this.FileInfo_Offset = this.InfoFile.Position;
- }
- }
- private void CloseFileInfo() {
- this.InfoFile.Close();
- }
- }
- }
|