using System; using System.Drawing; using System.Text; using System.IO; using System.Fire.FilePack.Exception; using System.IO.Compression; namespace System.Fire.FilePack { public class FilePack : IDisposable { private UInt16 keyCount; private string _filePath; private FilePackElements.FPKeyWords[] KeyWords; private bool bOpened; private BinaryReader _stream; private FileStream _streamBase; private bool disposed = false; private string _Password = ""; private FilePackElements.eSet _settings; private FilePackElements.FileExtension[] Extensions; private DateTime _date; private FilePackElements.FPComment[] _comments; private byte _commentCount = 0; /// /// Signatur Ascii Array /// public static byte[] Signature = new byte[] { 70, 105, 108, 101, 80, 97, 99, 107, 66, 80 }; /// /// Signature Version /// public FilePackElements.eVersion SignatureVersion = FilePackElements.eVersion.Version_1_0; private bool bConsole = false; /// Limit if File Items public const UInt16 FileLimit = 32767; /// Limit for the maximum comment count public const byte MaxCommentCount = 100; // Header Structur // Bytes Typ Name // 10 Byte() Signature (Fix) // 1 Byte Version // 1 eSet Einstellungen 1 // 1 Byte Anzahl "Comment" Felder // 5 Byte() Datum // [1] Byte Prüfsumme - Wenn Passwort gesetzt // [1] Byte Prüfsumme + (Länge MOD 5) + ASC(PW{1}) - Wenn Passwort gesetzt // [1] Byte CheckSumme - Wenn Passwort gesetzt // 2 Int16 Anzahl Einträge // n FPKeyWords(n) Einträge // 1 Byte Anzahl Dateiendungen // n FileExtantion Dateiendungen #region "Class Functions" /// Erstellt ein neues FilePack Objekt /// Pfad und Name der FilePack-Datei public FilePack(string FilePath) { System.Diagnostics.Debug.WriteLine("Create Filepack(\"" + FilePath + "\")"); this.Create(FilePath, ""); } /// Erstellt ein neues FilePack Objekt /// Pfad und Name der FilePack-Datei /// Setzt ein Passwort zum entschlüsseln geschützter Archive public FilePack(string FilePath, string Password) { System.Diagnostics.Debug.WriteLine("Create Filepack(\"" + FilePath + "\", \"" + Password + "\")"); this.Create(FilePath, Password); } /// Erstellt ein neues FilePack Objekt /// Pfad und Name der FilePack-Datei /// Consoleneinträge werden erstellt public FilePack(string FilePath, bool UseConsole) { System.Diagnostics.Debug.WriteLine("Create Filepack(\"" + FilePath + "\", " + (UseConsole ? "True" : "False") + ")"); this.Create(FilePath, ""); this.bConsole = UseConsole; } /// Erstellt ein neues FilePack Objekt /// Pfad und Name der FilePack-Datei /// Setzt ein Passwort zum entschlüsseln geschützter Archive /// Consoleneinträge werden erstellt public FilePack(string FilePath, string Password, bool UseConsole) { System.Diagnostics.Debug.WriteLine("Create Filepack(\"" + FilePath + "\", \"" + Password + "\", " + (UseConsole ? "True" : "False") + ")"); this.Create(FilePath, Password); this.bConsole = UseConsole; } /// Erstellt ein neues FilePack Objekt /// Dateistream public FilePack(FileStream FS) { this.Create(FS, ""); } /// Erstellt ein neues FilePack Objekt /// Dateistream /// Setzt ein Passwort zum entschlüsseln geschützter Archive public FilePack(FileStream FS, string Password) { this.Create(FS, Password); } /// Erstellt ein neues FilePack Objekt /// Dateistream /// Consoleneinträge werden erstellt public FilePack(FileStream FS, bool UseConsole) { this.Create(FS, ""); this.bConsole = UseConsole; } /// Erstellt ein neues FilePack Objekt /// Dateistream /// Setzt ein Passwort zum entschlüsseln geschützter Archive /// Consoleneinträge werden erstellt public FilePack(FileStream FS, string Password, bool UseConsole) { this.Create(FS, Password); this.bConsole = UseConsole; } ~FilePack() { this.Dispose(false); } /// /// Gibt sämmtliche von FilePack verwendete Ressourcen frei /// public virtual void Dispose() { Dispose(true); GC.SuppressFinalize(this); bOpened = false; } /// /// Gibt die von FilePack und den untergeordneten Klassen verwednete Ressourcen, nicht verwalteten Ressourcen und optional auch die verwalteten Ressourcen frei. /// /// true, um sowohl die verwalteten als auch die nicht verwalteten Ressourcen freizugeben. false um ausschließlich die verwalteten Ressourcen freizugeben. public virtual void Dispose(bool disposing) { if (disposed == false) { if (disposing) { if (this._stream != null) this._stream.Close(); if (this._streamBase != null) this._streamBase.Close(); if (this._streamBase != null) this._streamBase.Dispose(); this._stream = null; this._streamBase = null; disposed = true; } this.Close(); this._filePath = ""; } } public void Cons(string str) { if (this.bConsole) Console.WriteLine(str); } public static void Cons(string str, bool bConsole) { if (bConsole) Console.WriteLine(str); } #endregion #region "This Property" public string this[string Name] { get { return this.getText(Name); } } public string this[FilePackElements.FPKeyWords Keyword] { get { return this.getText(Keyword); } } #endregion #region "Propertys" /// Gibt eine Auflistung der Keywords zurück /// Gibt eine Auflistung der Keywords im FPKeywords Object zurück public FilePackElements.FPKeyWords[] Keyword() { return this.KeyWords; } /// Gibt das dem Index entsprechende Keywords zurück /// Index des Keywords /// Gibt das dem Index entsprechende Keyword im FPKeywords Object zurück public FilePackElements.FPKeyWords Keyword(short Index) { return this.KeyWords[Index]; } /// Gibt das dem Index entsprechende Keywords zurück /// Index des Keywords /// Gibt das dem Index entsprechende Keyword im FPKeywords Object zurück public FilePackElements.FPKeyWords Keyword(int Index) { return this.KeyWords[Index]; } /// Gibt das dem Namen entsprechende Keywords zurück /// Name des Keywords /// Gibt das dem Namen entsprechende Keyword im FPKeywords Object zurück public FilePackElements.FPKeyWords Keyword(string Name) { return this.getKeywordByName(Name); } /// Gibt die Anzahl der geladenen Keywords zurück /// Gibt die Anzahl der geladenen Keywords zurück public ushort Length { get { return this.keyCount; } } /// Gibt das Erstellungsdatum des Archives zurück /// Gibt das Erstellungsdatum des Archives als Date-Object zurück public DateTime CreateDate { get { return this._date; } } /// Gibt die Einstellungen des geladenen Archivs zurück /// Gibt die Einstellungen des geladenen Archivs als eSet Objekt zurück public FilePackElements.eSet Settings { get { return this._settings; } } public FilePackElements.FileExtension Extension(byte ID) { byte uext = (byte)(this._settings | FilePackElements.eSet.Extantions); if (uext == (byte)this._settings) return this.Extensions[ID]; else return new FilePackElements.FileExtension(""); } #endregion #region "Load" /// Öffnet ein FilePack Archiv /// Pfad zu dem FilePack Archiv public void Load(string FilePath) { this.Create(FilePath, ""); } /// Öffnet ein FilePack Archiv /// Pfad zu dem FilePack Archiv /// Setzt ein Passwort zum entschlüsseln geschützter Archive public void Load(string FilePath, string Password) { this.Create(FilePath, Password); } /// Öffnet ein FilePack Archiv /// Datei-Stream des FilePack Archives public void Load(FileStream Stream) { this.Create(Stream, ""); } /// Öffnet ein FilePack Archiv /// Datei-Stream des FilePack Archives /// Setzt ein Passwort zum entschlüsseln geschützter Archive public void Load(FileStream Stream, string Password) { this.Create(Stream, Password); } /// /// /// /// /// /// File not found private void Create(string FilePath, string Password) { System.Diagnostics.Debug.WriteLine("Create called: " + FilePath + (Password != "" ? ":" + Password : "")); if (this.bOpened == true) this.Close(); if (File.Exists(FilePath)) { this.Cons("Open archive. Using password: " + (Password == "" ? "No" : "Yes")); this._streamBase = new FileStream(FilePath, FileMode.Open, FileAccess.Read); this._stream = new BinaryReader(this._streamBase, System.Text.Encoding.ASCII); this._filePath = FilePath; this._Password = Password; if (_streamBase.Length < 17) { this._filePath = ""; this._Password = ""; this._stream.Close(); this._streamBase.Close(); this._streamBase.Dispose(); throw new Exception_Filesize(this._filePath, this._streamBase.Length); } else { byte[] s = this._stream.ReadBytes(10); if (checkSignature(s)) { SignatureVersion = (FilePackElements.eVersion)this._stream.ReadByte(); this.bOpened = true; System.Diagnostics.Debug.WriteLine("File Signature: " + SignatureVersion.ToString()); switch (SignatureVersion) { case FilePackElements.eVersion.Version_1_0: createVersion1(); break; default: this._filePath = ""; this._Password = ""; this._stream.Close(); this._streamBase.Close(); this._streamBase.Dispose(); throw new Exception_UnsportVersion(this._filePath, this._streamBase.Length, (byte)SignatureVersion); } } else throw new Exception_Signature(this._filePath, this._streamBase.Length); } } else throw new IO.FileNotFoundException("Datei konnte nicht gefunden werden\r\n" + FilePath, FilePath); } private void Create(FileStream Stream, string Password) { if (this.bOpened == true) this.Close(); this.Cons("Open archive. Using password: " + (Password == "" ? "No" : "Yes")); this._streamBase = Stream; this._stream = new BinaryReader(this._streamBase, Text.Encoding.ASCII); this._filePath = Stream.Name; this._Password = Password; if (_streamBase.Length < 17) { this._filePath = ""; this._Password = ""; this._stream.Close(); this._streamBase.Close(); this._streamBase.Dispose(); throw new Exception_Filesize(this._filePath, this._streamBase.Length); } else { byte[] s = this._stream.ReadBytes(10); if (checkSignature(s)) { SignatureVersion = (FilePackElements.eVersion)this._stream.ReadByte(); this.bOpened = true; switch (SignatureVersion) { case FilePackElements.eVersion.Version_1_0: createVersion1(); break; default: this._filePath = ""; this._Password = ""; this._stream.Close(); this._streamBase.Close(); this._streamBase.Dispose(); throw new Exception_UnsportVersion(this._filePath, this._streamBase.Length, (byte)SignatureVersion); } } else throw new Exception_Signature(this._filePath, this._streamBase.Length); } } private void createVersion1() { this._settings = (FilePackElements.eSet)this._stream.ReadByte(); this._commentCount = this._stream.ReadByte(); this._date = FilePack.bitsToDate(this._stream.ReadBytes(5)); System.Diagnostics.Debug.WriteLine("Header Info 1 - Settings: " + this._settings.ToString() + "; commentCount: " + this._commentCount + "; date: " + this._date.ToLongDateString()); if (checkPW1()) { this.keyCount = this._stream.ReadUInt16(); this.KeyWords = new FilePackElements.FPKeyWords[this.keyCount]; System.Diagnostics.Debug.WriteLine("Header Info 2 - keyCount: " + this.keyCount); for (int i = 0; i < this.keyCount; i++) this.KeyWords[i] = new FilePackElements.FPKeyWords(this._stream); FilePackElements.eSet useExt = (this._settings | FilePackElements.eSet.Extantions); byte extCount = 0; if (useExt == this._settings) { extCount = this._stream.ReadByte(); this.Extensions = new FilePackElements.FileExtension[extCount]; for (int j = 0; j < extCount; j++) this.Extensions[j] = new FilePackElements.FileExtension(this._stream); } System.Diagnostics.Debug.WriteLine("Header Info 3 - useExt: " + (useExt == this._settings ? "True" : "False") + "; extCount: " + extCount); long off = this._streamBase.Position; for (int i = 0; i < this.keyCount; i++) { this.KeyWords[i].Offset = off; off += this.KeyWords[i].Length; } this.Cons("Data Count: " + this.keyCount); if (this._commentCount > 0) { this._comments = new FilePackElements.FPComment[this._commentCount - 1]; for (int i = 0; i < this._commentCount; i++) this._comments[i] = new FilePackElements.FPComment(this._stream); } } } /// /// Schließt das Archiv und gibt den Stream frei /// /// public void Close() { if (this._stream != null) this._stream.Close(); if (this._streamBase != null) this._streamBase.Close(); if (this._streamBase != null) this._streamBase.Dispose(); this._stream = null; this._streamBase = null; this.keyCount = 0; this._filePath = ""; this.bOpened = false; this._Password = ""; if(this.KeyWords!=null) for (int i = 0; i < this.KeyWords.Length; i++) this.KeyWords[i] = null; this.KeyWords = null; if (this.Extensions != null) { if (this.Extensions.Length > 0) for (int i = 0; i < this.Extensions.Length; i++) this.Extensions[i] = null; this.Extensions = null; } this.Cons("Archive closed"); } #endregion #region "Get Data" #region "getImage" /// Gibt die Daten anhand der ID als Image-Object zurück /// Die ID des Eintrags in der Datei /// Gibt die Daten anhand der ID als Image-Object zurück /// System.Drawing.Image public Image getImage(int ID) { FilePackElements.FPKeyWords fpk = this.KeyWords[ID]; if(fpk!=null) { byte[] Data = fpk.getBytes(); FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if(DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); StreamReader sr = new IO.StreamReader(gz); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if(crypt == this._settings) Data = this.Decode(Data, this._Password); return (Image)new ImageConverter().ConvertFrom(Data); } return null; } /// Gibt die Daten anhand des Keyword-Objektes als Image-Object zurück /// Das Keyword-Objektes für die Identifizierung /// Gibt die Daten anhand des Keyword-Objektes als Image-Object zurück /// System.Drawing.Image public Image getImage(FilePackElements.FPKeyWords fpk) { if (fpk != null) { byte[] Data = fpk.getBytes(); FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); StreamReader sr = new IO.StreamReader(gz); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return (Image)new ImageConverter().ConvertFrom(Data); } return null; } /// Gibt die Daten anhand des Namen als Image-Object zurück /// Der Keyword-Name /// Gibt die Daten anhand des Namen als Image-Object zurück /// System.Drawing.Image public Image getImage(string Name) { FilePackElements.FPKeyWords fpk = this.getKeywordByName(Name, FilePackElements.DataType.dtImage); if (fpk != null) { byte[] Data = fpk.getBytes(); FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); StreamReader sr = new IO.StreamReader(gz); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return (Image)new ImageConverter().ConvertFrom(Data); } return null; } #endregion #region "getText" public string getText(int ID) { FilePackElements.FPKeyWords fpk = this.KeyWords[ID]; if (fpk != null) { byte[] Data = fpk.getBytes(); Encoding enc = System.Text.Encoding.UTF8; FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return enc.GetString(Data); } return null; } public string getText(FilePackElements.FPKeyWords fpk) { if (fpk != null) { byte[] Data = fpk.getBytes(); Encoding enc = System.Text.Encoding.UTF8; FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return enc.GetString(Data); } return null; } public string getText(string Name) { FilePackElements.FPKeyWords fpk = this.getKeywordByName(Name, FilePackElements.DataType.dtImage); if (fpk != null) { byte[] Data = fpk.getBytes(); Encoding enc = System.Text.Encoding.UTF8; FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return enc.GetString(Data); } return null; } public string getText(int ID, Encoding enc) { FilePackElements.FPKeyWords fpk = this.KeyWords[ID]; if (fpk != null) { byte[] Data = fpk.getBytes(); FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return enc.GetString(Data); } return null; } public string getText(FilePackElements.FPKeyWords fpk, Encoding enc) { if (fpk != null) { byte[] Data = fpk.getBytes(); FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return enc.GetString(Data); } return null; } public string getText(string Name, Encoding enc) { FilePackElements.FPKeyWords fpk = this.getKeywordByName(Name, FilePackElements.DataType.dtImage); if (fpk != null) { byte[] Data = fpk.getBytes(); FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return enc.GetString(Data); } return null; } #endregion #region "getData" /// Gibt die Daten anhand der ID als String zurück /// Die ID des Eintrags in der Datei /// Gibt die Daten anhand der ID als String zurück /// System.String public string getData(int ID) { FilePackElements.FPKeyWords fpk = this.KeyWords[ID]; if (fpk != null) { byte[] Data = fpk.getBytes(); Encoding enc = System.Text.Encoding.ASCII; FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return enc.GetString(Data); } return null; } /// Gibt die Daten anhand des Keyword-Objektes als String zurück /// Das Keyword-Objektes für die Identifizierung /// Gibt die Daten anhand des Keyword-Objektes als String zurück /// System.String public string getData(FilePackElements.FPKeyWords fpk) { if (fpk != null) { byte[] Data = fpk.getBytes(); Encoding enc = System.Text.Encoding.ASCII; FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return enc.GetString(Data); } return null; } /// Gibt die Daten anhand des Namen als String zurück /// Der Keyword-Name /// Gibt die Daten anhand des Namen als String zurück /// System.String public string getData(string Name) { FilePackElements.FPKeyWords fpk = this.getKeywordByName(Name, FilePackElements.DataType.dtImage); if (fpk != null) { byte[] Data = fpk.getBytes(); Encoding enc = System.Text.Encoding.ASCII; FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return enc.GetString(Data); } return null; } #endregion #region "getByteArray" /// Gibt die Daten anhand der ID als Byte-Array zurück /// Die ID des Eintrags in der Datei /// Gibt die Daten anhand der ID als Byte-Array zurück /// System.Byte public byte[] getByteArray(int ID) { FilePackElements.FPKeyWords fpk = this.KeyWords[ID]; if (fpk != null) { byte[] Data = fpk.getBytes(); FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return Data; } return null; } /// Gibt die Daten anhand des Keyword-Objektes als Byte-Array zurück /// Das Keyword-Objektes für die Identifizierung /// Gibt die Daten anhand des Keyword-Objektes als Byte-Array zurück /// System.Byte public byte[] getByteArray(FilePackElements.FPKeyWords fpk) { if (fpk != null) { byte[] Data = fpk.getBytes(); FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return Data; } return null; } /// Gibt die Daten anhand des Namen als Byte-Array zurück /// Der Keyword-Name /// Gibt die Daten anhand des Namen als Byte-Array zurück /// System.Byte public byte[] getByteArray(string Name) { FilePackElements.FPKeyWords fpk = this.getKeywordByName(Name, FilePackElements.DataType.dtImage); if (fpk != null) { byte[] Data = fpk.getBytes(); FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return Data; } return null; } #endregion #region "getStream" /// Gibt die Daten anhand der ID als Stream zurück /// Die ID des Eintrags in der Datei /// Gibt die Daten anhand der ID als Stream zurück /// System.IO.Stream public Stream getStream(int ID) { FilePackElements.FPKeyWords fpk = this.KeyWords[ID]; if (fpk != null) { byte[] Data = fpk.getBytes(); FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return new IO.MemoryStream(Data); } return null; } /// Gibt die Daten anhand des Keyword-Objektes als Stream zurück /// Das Keyword-Objektes für die Identifizierung /// Gibt die Daten anhand des Keyword-Objektes als Stream zurück /// System.IO.Stream public Stream getStream(FilePackElements.FPKeyWords fpk) { if (fpk != null) { byte[] Data = fpk.getBytes(); FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return new IO.MemoryStream(Data); } return null; } /// Gibt die Daten anhand des Namen als Stream zurück /// Der Keyword-Name /// Gibt die Daten anhand des Namen als Stream zurück /// System.IO.Stream public Stream getStream(string Name) { FilePackElements.FPKeyWords fpk = this.getKeywordByName(Name, FilePackElements.DataType.dtImage); if (fpk != null) { byte[] Data = fpk.getBytes(); FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return new IO.MemoryStream(Data); } return null; } #endregion #region "getAssembly" /// Gibt die Daten anhand der ID als Stream zurück /// Die ID des Eintrags in der Datei /// Gibt die Daten anhand der ID als Stream zurück /// System.IO.Stream public System.Reflection.Assembly getAssembly(int ID) { FilePackElements.FPKeyWords fpk = this.KeyWords[ID]; if (fpk != null) { byte[] Data = fpk.getBytes(); FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return System.Reflection.Assembly.Load(Data); } return null; } /// Gibt die Daten anhand des Keyword-Objektes als Stream zurück /// Das Keyword-Objektes für die Identifizierung /// Gibt die Daten anhand des Keyword-Objektes als Stream zurück /// System.IO.Stream public System.Reflection.Assembly getAssembly(FilePackElements.FPKeyWords fpk) { if (fpk != null) { byte[] Data = fpk.getBytes(); FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return System.Reflection.Assembly.Load(Data); } return null; } /// Gibt die Daten anhand des Namen als Stream zurück /// Der Keyword-Name /// Gibt die Daten anhand des Namen als Stream zurück /// System.IO.Stream public System.Reflection.Assembly getAssembly(string Name) { FilePackElements.FPKeyWords fpk = this.getKeywordByName(Name, FilePackElements.DataType.dtImage); if (fpk != null) { byte[] Data = fpk.getBytes(); FilePackElements.eSet DataGZip = this._settings | FilePackElements.eSet.Data_GZip; if (DataGZip == this._settings) { IO.MemoryStream mem = new IO.MemoryStream(Data); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Decompress); gz.Write(Data, 0, Data.Length); StreamReader sr = new IO.StreamReader(gz); Data = mem.ToArray(); gz.Flush(); sr.Close(); gz.Close(); mem.Close(); sr.Dispose(); gz.Dispose(); mem.Dispose(); } FilePackElements.eSet crypt = this._settings | FilePackElements.eSet.Crypt; if (crypt == this._settings) Data = this.Decode(Data, this._Password); return System.Reflection.Assembly.Load(Data); } return null; } #endregion #endregion #region "Comments" public enum TextCommentType : byte { TextComment = 1, ShortTextComment = 7, Homepage = 2, Email = 4, Company = 5, Address = 8 } public FilePackElements.FPComment addComment(string Text, TextCommentType Type) { Array.Resize(ref this._comments, this._commentCount + 1); this._comments[this._commentCount] = new FilePackElements.FPComment(); this._comments[this._commentCount].Type = (FilePackElements.FPCommentType)((byte)Type); this._comments[this._commentCount].Caption = Text; this._commentCount += 1; return this._comments[this._commentCount - 1]; } public FilePackElements.FPComment addComment(UInt16 FileID, string Text) { Array.Resize(ref this._comments, this._commentCount + 1); this._comments[this._commentCount] = new FilePackElements.FPComment(); this._comments[this._commentCount].Type = FilePackElements.FPCommentType.FileComment; this._comments[this._commentCount].Caption = Text; this._comments[this._commentCount].FileID = FileID; this._commentCount += 1; return this._comments[this._commentCount - 1]; } public FilePackElements.FPComment addComment(Bitmap Img) { Array.Resize(ref this._comments, this._commentCount + 1); this._comments[this._commentCount] = new FilePackElements.FPComment(); this._comments[this._commentCount].Type = FilePackElements.FPCommentType.Image; this._comments[this._commentCount].Picture = Img; this._commentCount += 1; return this._comments[this._commentCount - 1]; } public FilePackElements.FPComment addComment(UInt16 FileID, Bitmap Img) { Array.Resize(ref this._comments, this._commentCount + 1); this._comments[this._commentCount] = new FilePackElements.FPComment(); this._comments[this._commentCount].Type = FilePackElements.FPCommentType.FileImage; this._comments[this._commentCount].Picture = Img; this._comments[this._commentCount].FileID = FileID; this._commentCount += 1; return this._comments[this._commentCount - 1]; } public FilePackElements.FPComment addComment(Icon Ico) { Array.Resize(ref this._comments, this._commentCount + 1); this._comments[this._commentCount] = new FilePackElements.FPComment(); this._comments[this._commentCount].Type = FilePackElements.FPCommentType.Icon; this._comments[this._commentCount].Icon = Ico; this._commentCount += 1; return this._comments[this._commentCount - 1]; } public FilePackElements.FPComment addComment(UInt16 FileID, Icon Ico) { Array.Resize(ref this._comments, this._commentCount + 1); this._comments[this._commentCount] = new FilePackElements.FPComment(); this._comments[this._commentCount].Type = FilePackElements.FPCommentType.FileIcon; this._comments[this._commentCount].Icon = Ico; this._comments[this._commentCount].FileID = FileID; this._commentCount += 1; return this._comments[this._commentCount - 1]; } #endregion #region "Exists" /// Überprüft ob ein Keyword existiert /// Name des Keywords /// Gibt bei erfolg true zurück, false falls das Keyword nicht existiert. public bool Exsists(string Keyword) { for (int i = 0; i < this.keyCount; i++) { if (this.KeyWords[i].Keyword == Keyword) return true; } return false; } /// Überprüft ob ein Array mit Keywords existiert /// Array mit Namen der Keywords /// Gibt bei erfolg true zurück, false falls ein oder mehrere Keywords nicht existieren. public bool Exsists(string[] Keywords) { for (int i = 0; i < this.keyCount; i++) { bool bFound = false; for (int j = 0; j < Keywords.Length; j++) { if (this.KeyWords[i].Keyword == Keywords[j]) { bFound = true; break; // TODO: might not be correct. Was : Exit For } } if (bFound == false) return false; } return false; } #endregion #region "Crypt" private bool checkPW1() { byte s = (byte)(this._settings | FilePackElements.eSet.UsePassword); if (s == (byte)this._settings) { long sumx = 0; int sumy = 0; System.Text.Encoding enc = System.Text.Encoding.UTF8; byte[] xpw = enc.GetBytes(this._Password); for (int i = 0; i <= xpw.Length - 1; i++) { sumx = (sumx + xpw[i]) % 200; sumy = xpw[i] % 11; } byte sum = this._stream.ReadByte(); byte sum2 = this._stream.ReadByte(); byte sum3 = this._stream.ReadByte(); sumx = (sumx % 200) + 25; if (sumx != sum) { throw new Exception_PassCheckSum(this._filePath, this._streamBase.Length, (byte)SignatureVersion, sum, sumx, 1); //return false; } sumx += (xpw.Length % 5) + xpw[0] % 200; if (sumx != sum2) { throw new Exception_PassCheckSum(this._filePath, this._streamBase.Length, (byte)SignatureVersion, sum2, sumx, 2); //return false; } sumy = (((sumy + xpw[xpw.Length - 1]) % 50) + ((sumy + xpw[xpw.Length - 1]) % 47) + ((sumy + xpw[xpw.Length - 1]) % 127) + 30); if (sumy != sum3) { throw new Exception_PassCheckSum(this._filePath, this._streamBase.Length, (byte)SignatureVersion, sum3, sumy, 3); //return false; } } return true; } private static void writePW1(ref IO.BinaryWriter S, string PW) { long sumx = 0; int sumy = 0; System.Text.Encoding enc = System.Text.Encoding.UTF8; byte[] xpw = enc.GetBytes(PW); for (int i = 0; i <= xpw.Length - 1; i++) { sumx = (sumx + xpw[i]) % 200; sumy = xpw[i] % 11; } sumx = (sumx % 200) + 25; S.Write(Convert.ToByte(sumx)); sumx += (xpw.Length % 5) + xpw[0] % 200; S.Write(Convert.ToByte(sumx)); sumy = (((sumy + xpw[xpw.Length - 1]) % 50) + ((sumy + xpw[xpw.Length - 1]) % 47) + ((sumy + xpw[xpw.Length - 1]) % 127) + 30); S.Write(Convert.ToByte(sumy)); } private static byte[] Encode(byte[] b, string Password = "") { if (Password == "") { for (int i = 0; i < b.Length; i++) { byte j = (byte)((b[i] + i + b.Length) % 256); b[i] = j; } return b; } else { byte[] pw = Convert.FromBase64String(Password); int pwLen = pw.Length; for (int i = 0; i < b.Length; i++) { int j = ((b[i] + i + b.Length + pw[i % pwLen]) + pwLen) % 256; b[i] = (byte)j; } return b; } } private byte[] Decode(byte[] b, string Password = "") { if (Password=="") { for (int i = 0; i < b.Length; i++) { byte j = (byte)(((b[i] - (i % 256) - (b.Length % 256)) + 512) % 256); b[i] = j; } } else { byte[] pw = Convert.FromBase64String(Password); int pwLen = pw.Length; for (int i = 0; i < b.Length; i++) { int j = (((b[i] - (i % 256) - (b.Length % 256) - pw[i % pwLen]) - pwLen) + 2048) % 256; b[i] = (byte)j; } } return b; } #endregion #region "Count Property" /// Gibt die Anzahl Einträge zurück /// Gibt die Anzahl Einträge zurück /// public int Count() { return this.KeyWords.Length; } /// Gibt die Anzahl Einträge des angegebenen Typen zurück /// Gibt den Typen der Daten an, nach denen gezählt werden soll /// Gibt die Anzahl Einträge des angegebenen Typen zurück /// public int Count(FilePackElements.DataType Type) { int nr = 0; foreach (FilePackElements.FPKeyWords kw in this.KeyWords) if (kw.Type == Type) nr += 1; return nr; } /// Gibt die Anzahl Einträge mit der angegebenen Dateiendung zurück /// Gibt die Dateiendung an, nach der gezählt werden soll /// Gibt die Anzahl Einträge mit der angegebenen Dateiendung zurück /// public int Count(string Extensions) { int nr = 0; foreach (FilePackElements.FPKeyWords kw in this.KeyWords) if (this.Extensions[kw.FileExtension].Extensions == Extensions) nr += 1; return nr; } #endregion #region "Other Functions" /// /// Gibt das entsprechende Keyword-Objekt als System.BlackPro.FilePackBP.FilePackElements.FPKeyWords zurück /// /// Name des Keywords als String /// Gibt das entsprechende Keyword-Objekt als System.BlackPro.FilePackBP.FilePackElements.FPKeyWords zurück public FilePackElements.FPKeyWords getKeywordByName(string Name) { for (int i = 0; i < this.keyCount; i++) { if (this.KeyWords[i].Keyword == Name) return this.KeyWords[i]; } return null; } /// /// Gibt das entsprechende Keyword-Objekt als System.BlackPro.FilePackBP.FilePackElements.FPKeyWords zurück /// /// Name des Keywords als String /// Datentyp des Keywords als System.BlackPro.FilePackBP.FilePack.DataType /// Gibt das entsprechende Keyword-Objekt als System.BlackPro.FilePackBP.FilePackElements.FPKeyWords zurück public FilePackElements.FPKeyWords getKeywordByName(string Name, FilePackElements.DataType Type) { for (int i = 0; i < this.keyCount; i++) { if (this.KeyWords[i].Keyword == Name & this.KeyWords[i].Type == Type) return this.KeyWords[i]; } return null; } /// /// Prüft die Signatur des Archivs /// /// Die Signatur als Byte-Array /// Gibt bei erfolgreicher Signaturprüfung True zurück public static bool checkSignature(byte[] b) { if (b == null) return false; if (b.Length == 0) return false; if (b.Length == Signature.Length) { for (int i = 0; i < b.Length; i++) { if (b[i] != Signature[i]) return false; } return true; } else { return false; } } private static char Chr(int b) { return Convert.ToChar(b); } private static char Chr(byte b) { return Convert.ToChar(b); } private static byte Asc(char c) { return Convert.ToByte(c); } private static string IIF(bool b, string t, string f) { if (b) { return t; } else { return f; } } private static byte IIF(bool b, byte t, byte f) { if (b) { return t; } else { return f; } } private static int IIF(bool b, int t, int f) { if (b) { return t; } else { return f; } } #endregion #region "Export" /// /// Exportiert die Einträge des Archives in das glaiche Verzeichnis /// public void exportToDirectory() { if (!string.IsNullOrEmpty(this._filePath)) { FileInfo fi = new FileInfo(this._filePath); this.exportToDirectory(fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + "\\"); } } /// /// Exportiert die Einträge des Archives in das glaiche Verzeichnis /// /// Gibt an, ob existierende Dateien überschrieben werden sollen public void exportToDirectory(bool Overwrite) { if (!string.IsNullOrEmpty(this._filePath)) { FileInfo fi = new FileInfo(this._filePath); this.exportToDirectory(fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + "\\", Overwrite); } } /// /// Exportiert die Einträge des Archives in das angegebene Verzeichnis /// /// Das Verzeichnis in das die Daten exportiert werden sollen public void exportToDirectory(string Dir) { byte canExport = (byte)(this._settings | FilePackElements.eSet.ExportLocked); if (canExport != (byte)this._settings) { if (Directory.Exists(Dir) == false) Directory.CreateDirectory(Dir); foreach (FilePackElements.FPKeyWords kw in this.KeyWords) { string ext = this.Extensions[kw.FileExtension].Extensions; string FPath = Dir + (Dir.EndsWith(IO.Path.DirectorySeparatorChar.ToString()) ? "" : IO.Path.DirectorySeparatorChar.ToString()) + kw.Keyword + (!string.IsNullOrEmpty(ext) ? "." + ext : ""); if (FPath.Contains(IO.Path.DirectorySeparatorChar.ToString())) { FileInfo fi = new FileInfo(FPath); if (fi.Directory.Exists == false) fi.Directory.Create(); //Directory.CreateDirectory(fi.Directory) } if (IO.File.Exists(FPath) == false) { this.Cons("Export Data '" + kw.Keyword + (!string.IsNullOrEmpty(ext) ? "." + ext : "") + "' (" + kw.Length.ToString("#,##0") + " Bytes)"); File.WriteAllBytes(FPath, this.getByteArray(kw)); } else { this.Cons("File already exists '" + kw.Keyword + (!string.IsNullOrEmpty(ext) ? "." + ext : "") + "' (" + kw.Length.ToString("#,##0") + " Bytes)"); } } } else { throw new System.Fire.FilePack.Exception.Exception_ExportLocked(this._filePath, this._streamBase.Length, (byte)SignatureVersion); } } /// /// Exportiert die Einträge des Archives in das angegebene Verzeichnis /// /// Das Verzeichnis in das die Daten exportiert werden sollen /// Gibt an, ob existierende Dateien überschrieben werden sollen public void exportToDirectory(string Dir, bool Overwrite) { byte canExport = (byte)(this._settings | FilePackElements.eSet.ExportLocked); if (canExport != (byte)this._settings) { if (Directory.Exists(Dir) == false) Directory.CreateDirectory(Dir); foreach (FilePackElements.FPKeyWords kw in this.KeyWords) { string ext = this.Extensions[kw.FileExtension].Extensions; this.Cons("Export Data '" + kw.Keyword + (!string.IsNullOrEmpty(ext) ? "." + ext : "") + "' (" + kw.Length.ToString("#,##0") + " Bytes)"); if (IO.File.Exists(Dir + (Dir.EndsWith(IO.Path.DirectorySeparatorChar.ToString()) ? "" : IO.Path.DirectorySeparatorChar.ToString()) + kw.Keyword + (!string.IsNullOrEmpty(ext) ? "." + ext : "")) & Overwrite == true) { IO.File.Delete(Dir + (Dir.EndsWith(IO.Path.DirectorySeparatorChar.ToString()) ? "" : IO.Path.DirectorySeparatorChar.ToString()) + kw.Keyword + (!string.IsNullOrEmpty(ext) ? "." + ext : "")); } else if (IO.File.Exists(Dir + (Dir.EndsWith(IO.Path.DirectorySeparatorChar.ToString()) ? "" : IO.Path.DirectorySeparatorChar.ToString()) + kw.Keyword + (!string.IsNullOrEmpty(ext) ? "." + ext : "")) == false) { File.WriteAllBytes(Dir + (Dir.EndsWith(IO.Path.DirectorySeparatorChar.ToString()) ? "" : IO.Path.DirectorySeparatorChar.ToString()) + kw.Keyword + (!string.IsNullOrEmpty(ext) ? "." + ext : ""), this.getByteArray(kw)); } } } else { throw new System.Fire.FilePack.Exception.Exception_ExportLocked(this._filePath, this._streamBase.Length, (byte)SignatureVersion); } } #endregion #region "Create" /// /// Erstellt ein Archiv im Parent-Directory anhand des Verzeichnises DirPath /// /// Gibt das Verzeichnis an, das archiviert werden soll. /// Gibt die Version des Archives an. public static void createFromDirectory(string DirPath, ref FilePackElements.eVersion Version) { string Destination = createFilepathFromDir(DirPath); createFromDirectory1(DirPath, Destination, Version, new FilePackElements.CreateSetting(true), false); } /// /// Erstellt ein Archiv im Parent-Directory mit Einstellungen anhand des Verzeichnises DirPath /// /// Gibt das Verzeichnis an, das archiviert werden soll. /// Gibt die Version des Archives an. /// Die Einstellungen unter denen das Archiv erstellt werden soll public static void createFromDirectory(string DirPath, FilePackElements.eVersion Version, FilePackElements.CreateSetting Settings) { string Destination = createFilepathFromDir(DirPath); createFromDirectory1(DirPath, Destination, Version, Settings, false); } /// /// Erstellt ein Archiv im DestinationFile anhand des Verzeichnises DirPath /// /// Gibt das Verzeichnis an, das archiviert werden soll. /// Gibt den Pfad des Zielarchives an. /// Gibt die Version des Archives an. public static void createFromDirectory(string DirPath, string DestinationFile, FilePackElements.eVersion Version) { createFromDirectory1(DirPath, DestinationFile, Version, new FilePackElements.CreateSetting(true), false); } /// /// Erstellt ein Archiv im DestinationFile anhand des Verzeichnises DirPath /// /// Gibt das Verzeichnis an, das archiviert werden soll. /// Gibt den Pfad des Zielarchives an. /// Gibt die Version des Archives an. /// Die Einstellungen unter denen das Archiv erstellt werden soll public static void createFromDirectory(string DirPath, string DestinationFile, FilePackElements.eVersion Version, FilePackElements.CreateSetting Settings) { createFromDirectory1(DirPath, DestinationFile, Version, Settings, false); } /// /// Erstellt ein Archiv im Parent-Directory anhand des Verzeichnises DirPath /// /// Gibt das Verzeichnis an, das archiviert werden soll. /// Gibt die Version des Archives an. /// true, gibt die Ausgabe in der Console wieder public static void createFromDirectory(string DirPath, FilePackElements.eVersion Version, bool Console) { string Destination = createFilepathFromDir(DirPath); createFromDirectory1(DirPath, Destination, Version, new FilePackElements.CreateSetting(true), Console); } /// /// Erstellt ein Archiv im Parent-Directory mit Einstellungen anhand des Verzeichnises DirPath /// /// Gibt das Verzeichnis an, das archiviert werden soll. /// Gibt die Version des Archives an. /// Die Einstellungen unter denen das Archiv erstellt werden soll /// true, gibt die Ausgabe in der Console wieder public static void createFromDirectory(string DirPath, FilePackElements.eVersion Version, FilePackElements.CreateSetting Settings, bool Console) { string Destination = createFilepathFromDir(DirPath); createFromDirectory1(DirPath, Destination, Version, Settings, Console); } /// /// Erstellt ein Archiv im DestinationFile anhand des Verzeichnises DirPath /// /// Gibt das Verzeichnis an, das archiviert werden soll. /// Gibt den Pfad des Zielarchives an. /// Gibt die Version des Archives an. /// true, gibt die Ausgabe in der Console wieder public static void createFromDirectory(string DirPath, string DestinationFile, FilePackElements.eVersion Version, bool Console) { createFromDirectory1(DirPath, DestinationFile, Version, new FilePackElements.CreateSetting(true), Console); } /// /// Erstellt ein Archiv im DestinationFile anhand des Verzeichnises DirPath /// /// Gibt das Verzeichnis an, das archiviert werden soll. /// Gibt den Pfad des Zielarchives an. /// Gibt die Version des Archives an. /// Die Einstellungen unter denen das Archiv erstellt werden soll /// true, gibt die Ausgabe in der Console wieder public static void createFromDirectory(string DirPath, string DestinationFile, FilePackElements.eVersion Version, FilePackElements.CreateSetting Settings, bool Console) { createFromDirectory1(DirPath, DestinationFile, Version, Settings, Console); } private static string createFilepathFromDir(string DirPath) { if (DirPath.EndsWith(IO.Path.DirectorySeparatorChar.ToString())) DirPath = DirPath.Substring(0, DirPath.Length - (IO.Path.DirectorySeparatorChar.ToString().Length)); return DirPath + ".bfp"; } private struct sFileInfo { private string _Extension; private byte _ExtensionIndex; private string _FileName; private string _AbsolutePath; private string _Name; private long _FileSize; private FilePackElements.DataType _Type; private string _Temp; public string Extension { get { return this._Extension; } set { this._Extension = value; } } public string Temp { get { return this._Temp; } set { this._Temp = value; } } public byte ExtensionIndex { get { return this._ExtensionIndex; } set { this._ExtensionIndex = value; } } public string FileName { get { return this._FileName; } set { this._FileName = value; } } public string AbsolutePath { get { return this._AbsolutePath; } set { this._AbsolutePath = value; } } public string Name { get { return this._Name; } set { this._Name = value; } } public long FileSize { get { return this._FileSize; } set { this._FileSize = value; } } public FilePackElements.DataType Type { get { return this._Type; } set { this._Type = value; } } } private static void createFromDirectory1(string DirPath, string Destination, FilePackElements.eVersion Version, FilePackElements.CreateSetting Settings, bool bCons) { if (File.Exists(Destination)) { throw new System.Fire.FilePack.Exception.Exception_ArchiveExists(Destination); //return; } IO.DirectoryInfo di = new IO.DirectoryInfo(DirPath); IO.FileStream fs = new IO.FileStream(Destination, FileMode.CreateNew); IO.BinaryWriter bw = new IO.BinaryWriter(fs); FilePackElements.eSet xsettings = 0; System.Text.Encoding enc = System.Text.Encoding.UTF8; if (Settings.DataGZip) xsettings = xsettings | FilePackElements.eSet.Data_GZip; if (Settings.Crypt) xsettings = xsettings | FilePackElements.eSet.Crypt; if (Settings.LockExport) xsettings = xsettings | FilePackElements.eSet.ExportLocked; if (Settings.TextGZip) xsettings = xsettings | FilePackElements.eSet.Text_GZip; if (Settings.UsePassword) xsettings = xsettings | FilePackElements.eSet.UsePassword; if (Settings.Extantions) xsettings = xsettings | FilePackElements.eSet.Extantions; FilePack.Cons("Create archive '" + di.Name + "' using password: " + (!string.IsNullOrEmpty(Settings.Password) & Settings.UsePassword ? "Yes" : "No"), bCons); bw.Write(System.Fire.FilePack.FilePack.Signature); bw.Write(Convert.ToByte(FilePackElements.eVersion.Version_1_0)); bw.Write(Convert.ToByte(xsettings)); bw.Write(Convert.ToByte(0)); // 0 Kommentar-Felder bw.Write(FilePack.dateToBytes()); if (Settings.UsePassword & !string.IsNullOrEmpty(Settings.Password)) FilePack.writePW1(ref bw, Settings.Password); string[] f = { }; if (DirPath.EndsWith(IO.Path.DirectorySeparatorChar.ToString()) == false) DirPath += IO.Path.DirectorySeparatorChar; getFileList(DirPath, ref f); string[] ext = { }; sFileInfo[] FileInfos = { }; int FileInfoCount = 0; // File Count ushort fc = 0; foreach (string s in f) if (s != DirPath) { FileInfo fi = new FileInfo(s); string itemName = fi.FullName.Substring(DirPath.Length, fi.FullName.Length - DirPath.Length - fi.Extension.Length); if (itemName.Length < 240) fc += 1; } bw.Write(fc); foreach (string s in f) { if (s != DirPath) { FileInfo fi = new FileInfo(s); byte extIndex = 0; if (Array.IndexOf(ext, fi.Extension.ToLower().Substring(1)) > -1) { for (byte i = 0; i < ext.Length; i++) { if (ext[i] == fi.Extension.ToLower().Substring(1)) { extIndex = i; break; // TODO: might not be correct. Was : Exit For } } } else { extIndex = (byte)ext.Length; Array.Resize(ref ext, extIndex + 1); ext[extIndex] = fi.Extension.ToLower().Substring(1); } string itemName = fi.FullName.Substring(DirPath.Length, fi.FullName.Length - DirPath.Length - ext[extIndex].Length - 1); // Pfad der geschrieben wird FilePackElements.DataType type = FilePack.getDataType(ext[extIndex]); // Maximal 240 Dateiendungen if (itemName.Length < 240) { Array.Resize(ref FileInfos, FileInfoCount + 1); FileInfos[FileInfoCount] = new sFileInfo(); FileInfos[FileInfoCount].AbsolutePath = fi.FullName; FileInfos[FileInfoCount].Extension = ext[extIndex]; FileInfos[FileInfoCount].ExtensionIndex = extIndex; FileInfos[FileInfoCount].FileName = fi.Name; FileInfos[FileInfoCount].FileSize = fi.Length; FileInfos[FileInfoCount].Name = itemName; FileInfos[FileInfoCount].Temp = System.IO.Path.GetTempFileName(); FileInfos[FileInfoCount].Type = type; FilePack.Cons("Packe '" + fi.Name + "'", bCons); // Erstelle eine Temporäre Datei mit den Inhalten byte[] data = File.ReadAllBytes(fi.FullName); if ((Settings.DataGZip & (type == FilePackElements.DataType.dtBinaryData | type == FilePackElements.DataType.dtByteArray | type == FilePackElements.DataType.dtVideo | type == FilePackElements.DataType.dtUnsuportImage | type == FilePackElements.DataType.dtImage)) | (Settings.TextGZip & type == FilePackElements.DataType.dtString)) { IO.MemoryStream mem = new IO.MemoryStream(); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Compress); gz.Write(data, 0, data.Length); data = mem.ToArray(); gz.Close(); mem.Close(); gz.Dispose(); mem.Dispose(); } if (Settings.Crypt) { if (Settings.UsePassword) { data = FilePack.Encode(data, Settings.Password); } else { data = FilePack.Encode(data, ""); } FileInfos[FileInfoCount].FileSize = data.Length; } File.WriteAllBytes(FileInfos[FileInfoCount].Temp, data); // Trage die Daten in die ArchivDatei ein // Keyword Daten schreiben //Dim NameLen() As Byte = BitConverter.GetBytes(CByte(enc.GetBytes(FileInfos(FileInfoCount).Name).Length)) byte[] FileSize = BitConverter.GetBytes(Convert.ToInt64(FileInfos[FileInfoCount].FileSize)); //Dim FileType() As Byte = BitConverter.GetBytes(CByte(FileInfos(FileInfoCount).Type)) byte[] FileNameBytes = enc.GetBytes(FileInfos[FileInfoCount].Name); fs.WriteByte((byte)enc.GetBytes(FileInfos[FileInfoCount].Name).Length); // NameLen //bw.Write(NameLen) bw.Write(FileSize); // Länge fs.WriteByte(Convert.ToByte(FileInfos[FileInfoCount].Type)); // Type //bw.Write(FileType) ' Typ if (Settings.Extantions) { fs.WriteByte(Convert.ToByte(FileInfos[FileInfoCount].ExtensionIndex)); } else { fs.WriteByte(Convert.ToByte(0)); } bw.Write(FileNameBytes); FileInfoCount += 1; } } } if (Settings.Extantions) { bw.Write(Convert.ToByte(ext.Length)); // Dateiendung (Anzahl) foreach (string e in ext) { bw.Write(Convert.ToByte(e.Length + 70)); bw.Write(enc.GetBytes(e)); } } FilePack.Cons("Schreibe Daten", bCons); if (FileInfos.Length > 0) { foreach (sFileInfo s in FileInfos) { bw.Write(File.ReadAllBytes(s.Temp)); File.Delete(s.Temp); } } bw.Close(); fs.Close(); fs.Dispose(); } /// /// Erstellt ein FilePack auf Basis eines Verzeichnisses. /// /// Das Verzeichnis, dass Archiviert werden soll /// Die Zieldatei /// public void Save(string DirPath, string Destination) { this.Save(DirPath, Destination, false); } /// /// Erstellt ein FilePack auf Basis eines Verzeichnisses. /// /// Das Verzeichnis, dass Archiviert werden soll /// Die Zieldatei /// Soll die Statusmeldungen in der Output Console angezeigt werden /// public void Save(string DirPath, string Destination, bool bCons) { if (File.Exists(Destination)) { throw new System.Fire.FilePack.Exception.Exception_ArchiveExists(Destination); //return; } IO.DirectoryInfo di = new IO.DirectoryInfo(DirPath); IO.FileStream fs = new IO.FileStream(Destination, FileMode.CreateNew); IO.BinaryWriter bw = new IO.BinaryWriter(fs); System.Text.Encoding enc = System.Text.Encoding.UTF8; byte bpw = (byte)(this._settings | FilePackElements.eSet.UsePassword); bool bUsePW = (bpw == (byte)this._settings) && this._Password != ""; bool bCrypt = (this._settings | FilePackElements.eSet.Crypt) == this._settings; bool bDataGZip = (this._settings | FilePackElements.eSet.Data_GZip) == this._settings; bool bTextGZip = (this._settings | FilePackElements.eSet.Text_GZip) == this._settings; bool bExtantions = (this._settings | FilePackElements.eSet.Extantions) == this._settings; FilePack.Cons("Create archive '" + di.Name + "' using password: " + (bUsePW ? "Yes" : "No"), bCons); FilePack.Cons("Using Filepack Version: " + this.SignatureVersion.ToString(), bCons); bw.Write(Signature); bw.Write(Convert.ToByte(this.SignatureVersion)); bw.Write(Convert.ToByte(this._settings)); bw.Write(Convert.ToByte(this._commentCount)); // n Kommentar-Felder bw.Write(FilePack.dateToBytes()); // Datum eintragen if (bUsePW) writePW1(ref bw, this._Password); string[] f = { }; if (DirPath.EndsWith(IO.Path.DirectorySeparatorChar.ToString()) == false) DirPath += IO.Path.DirectorySeparatorChar; getFileList(DirPath, ref f); string[] ext = { }; sFileInfo[] FileInfos = { }; int FileInfoCount = 0; bw.Write(Convert.ToByte(this.keyCount)); // Keyword Count foreach (string s in f) { if (s != DirPath) { FileInfo fi = new FileInfo(s); byte extIndex = 0; if (Array.IndexOf(ext, fi.Extension.ToLower().Substring(1)) > -1) { for (byte i = 0; i < ext.Length; i++) { if (ext[i] == fi.Extension.ToLower().Substring(1)) { extIndex = i; break; // TODO: might not be correct. Was : Exit For } } } else { extIndex = (byte)ext.Length; Array.Resize(ref ext, extIndex + 1); ext[extIndex] = fi.Extension.ToLower().Substring(1); } string itemName = fi.FullName.Substring(DirPath.Length, fi.FullName.Length - DirPath.Length - ext[extIndex].Length - 1); // Pfad der geschrieben wird FilePackElements.DataType type = getDataType(ext[extIndex]); // Maximal 240 Dateiendungen if (itemName.Length < 240) { Array.Resize(ref FileInfos, FileInfoCount + 1); FileInfos[FileInfoCount] = new sFileInfo(); FileInfos[FileInfoCount].AbsolutePath = fi.FullName; FileInfos[FileInfoCount].Extension = ext[extIndex]; FileInfos[FileInfoCount].ExtensionIndex = extIndex; FileInfos[FileInfoCount].FileName = fi.Name; FileInfos[FileInfoCount].FileSize = fi.Length; FileInfos[FileInfoCount].Name = itemName; FileInfos[FileInfoCount].Temp = System.IO.Path.GetTempFileName(); FileInfos[FileInfoCount].Type = type; FilePack.Cons("Packe '" + fi.Name + "'", bCons); // Erstelle eine Temporäre Datei mit den Inhalten byte[] data = File.ReadAllBytes(fi.FullName); if ((bDataGZip & (type == FilePackElements.DataType.dtBinaryData | type == FilePackElements.DataType.dtByteArray | type == FilePackElements.DataType.dtVideo | type == FilePackElements.DataType.dtUnsuportImage | type == FilePackElements.DataType.dtImage)) | (bTextGZip & type == FilePackElements.DataType.dtString)) { IO.MemoryStream mem = new IO.MemoryStream(); System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Compress); gz.Write(data, 0, data.Length); data = mem.ToArray(); gz.Close(); mem.Close(); gz.Dispose(); mem.Dispose(); } if (bCrypt) { if (bUsePW) { data = FilePack.Encode(data, this._Password); } else { data = FilePack.Encode(data, ""); } FileInfos[FileInfoCount].FileSize = data.Length; } File.WriteAllBytes(FileInfos[FileInfoCount].Temp, data); // Trage die Daten in die ArchivDatei ein // Keyword Daten schreiben //Dim NameLen() As Byte = BitConverter.GetBytes(CByte(enc.GetBytes(FileInfos(FileInfoCount).Name).Length)) byte[] FileSize = BitConverter.GetBytes(Convert.ToInt64(FileInfos[FileInfoCount].FileSize)); //Dim FileType() As Byte = BitConverter.GetBytes(CByte(FileInfos(FileInfoCount).Type)) byte[] FileNameBytes = enc.GetBytes(FileInfos[FileInfoCount].Name); fs.WriteByte((byte)enc.GetBytes(FileInfos[FileInfoCount].Name).Length); // NameLen //bw.Write(NameLen) bw.Write(FileSize); // Länge fs.WriteByte(Convert.ToByte(FileInfos[FileInfoCount].Type)); // Type //bw.Write(FileType) ' Typ if (bExtantions) { fs.WriteByte(Convert.ToByte(FileInfos[FileInfoCount].ExtensionIndex)); } else { fs.WriteByte(Convert.ToByte(0)); } bw.Write(FileNameBytes); FileInfoCount += 1; } } } if (bExtantions) { bw.Write(Convert.ToByte(ext.Length)); // Dateiendung (Anzahl) foreach (string e in ext) { bw.Write(Convert.ToByte(e.Length + 70)); bw.Write(enc.GetBytes(e)); } } FilePack.Cons("Schreibe Daten", bCons); if (FileInfos.Length > 0) { foreach (sFileInfo s in FileInfos) { bw.Write(File.ReadAllBytes(s.Temp)); File.Delete(s.Temp); } } bw.Close(); fs.Close(); fs.Dispose(); } private static FilePackElements.DataType getDataType(string FileExt) { switch (FileExt) { // Typ case "jpg": return FilePackElements.DataType.dtImage; case "jpeg": return FilePackElements.DataType.dtImage; case "gif": return FilePackElements.DataType.dtImage; case "png": return FilePackElements.DataType.dtImage; case "bmp": return FilePackElements.DataType.dtImage; case "dds": return FilePackElements.DataType.dtUnsuportImage; case "tif": return FilePackElements.DataType.dtUnsuportImage; case "cr2": return FilePackElements.DataType.dtUnsuportImage; case "tiff": return FilePackElements.DataType.dtUnsuportImage; case "dwf": return FilePackElements.DataType.dtUnsuportImage; case "dwg": return FilePackElements.DataType.dtUnsuportImage; case "dxf": return FilePackElements.DataType.dtUnsuportImage; case "3ds": return FilePackElements.DataType.dtUnsuportImage; case "mov": return FilePackElements.DataType.dtVideo; case "mpg": return FilePackElements.DataType.dtVideo; case "mp4": return FilePackElements.DataType.dtVideo; case "wmv": return FilePackElements.DataType.dtVideo; case "avi": return FilePackElements.DataType.dtVideo; case "mpeg": return FilePackElements.DataType.dtVideo; case "txt": return FilePackElements.DataType.dtString; case "log": return FilePackElements.DataType.dtString; case "wav": return FilePackElements.DataType.dtAudio; case "mp3": return FilePackElements.DataType.dtAudio; case "ogg": return FilePackElements.DataType.dtAudio; case "wma": return FilePackElements.DataType.dtAudio; case "js": return FilePackElements.DataType.dtSourceCode; case "vbs": return FilePackElements.DataType.dtSourceCode; case "html": return FilePackElements.DataType.dtSourceCode; case "css": return FilePackElements.DataType.dtSourceCode; case "c": return FilePackElements.DataType.dtSourceCode; case "cpp": return FilePackElements.DataType.dtSourceCode; case "h": return FilePackElements.DataType.dtSourceCode; case "vb": return FilePackElements.DataType.dtSourceCode; case "cs": return FilePackElements.DataType.dtSourceCode; case "vbproj": return FilePackElements.DataType.dtSourceCode; case "csproj": return FilePackElements.DataType.dtSourceCode; case "settings": return FilePackElements.DataType.dtSourceCode; case "xml": return FilePackElements.DataType.dtSourceCode; case "xls": return FilePackElements.DataType.dtSourceCode; case "resx": return FilePackElements.DataType.dtSourceCode; case "vcxproj": return FilePackElements.DataType.dtSourceCode; case "php": return FilePackElements.DataType.dtSourceCode; case "php3": return FilePackElements.DataType.dtSourceCode; case "php4": return FilePackElements.DataType.dtSourceCode; case "php5": return FilePackElements.DataType.dtSourceCode; case "php6": return FilePackElements.DataType.dtSourceCode; case "sln": return FilePackElements.DataType.dtSourceCode; case "ini": return FilePackElements.DataType.dtConfiguration; case "inf": return FilePackElements.DataType.dtConfiguration; case "dll": return FilePackElements.DataType.dtExecuteable; case "exe": return FilePackElements.DataType.dtExecuteable; case "com": return FilePackElements.DataType.dtExecuteable; case "bas": return FilePackElements.DataType.dtExecuteable; case "lib": return FilePackElements.DataType.dtExecuteable; default: return FilePackElements.DataType.dtBinaryData; } } private static void getFileList(string sPath, ref string[] sFiles) { FileSystemInfo[] FSIs = new DirectoryInfo(sPath).GetFileSystemInfos(); foreach (FileSystemInfo FSI in FSIs) { if (sFiles.Length < FileLimit) { if (FSI is DirectoryInfo) { getFileList(FSI.FullName + IO.Path.DirectorySeparatorChar.ToString(), ref sFiles); } else { int x = sFiles.Length; Array.Resize(ref sFiles, x + 1); sFiles[x] = FSI.FullName; } } } } #endregion #region "Date Functions" private const int BaseYear = 1950; private static byte[] dateToBytes() { return FilePack.bitsToBytes(FilePack.dateToBits()); } private static byte[] dateToBytes(ref System.DateTime xDate) { return FilePack.bitsToBytes(FilePack.dateToBits(xDate)); } private static bool[] dateToBits() { return FilePack.dateToBits(System.DateTime.UtcNow); } private static bool[] dateToBits(System.DateTime xDate) { int xyear = xDate.Year - FilePack.BaseYear; bool[] bits = new bool[40]; if (xDate.Kind != DateTimeKind.Utc) xDate = xDate.ToUniversalTime(); // Year for (int i = 0; i <= 8; i++) { int x = (xyear | ((int)Math.Pow(2, i))); bits[8 - i] = x == xyear; } // Month for (int i = 0; i <= 4; i++) { int x = (xDate.Month | (int)(Math.Pow(2, i))); bits[13 - i] = x == xDate.Month; } // Day for (int i = 0; i <= 5; i++) { int x = (xDate.Day | (int)(Math.Pow(2, i))); bits[19 - i] = x == xDate.Day; } // Hour for (int i = 0; i <= 5; i++) { int x = (xDate.Hour | (int)(Math.Pow(2, i))); bits[25 - i] = x == xDate.Hour; } // Minute for (int i = 0; i <= 6; i++) { int x = (xDate.Minute | (int)(Math.Pow(2, i))); bits[32 - i] = x == xDate.Minute; } // Seconds for (int i = 0; i <= 6; i++) { int x = (xDate.Second | (int)(Math.Pow(2, i))); bits[39 - i] = x == xDate.Second; } return bits; } private static byte[] bitsToBytes(bool[] b) { byte[] tmp = new byte[((int)Math.Ceiling((decimal)(b.Length / 8)))]; for (int i = 0; i < Math.Ceiling((decimal)(b.Length / 8)); i++) { for (int j = 0; j <= 7; j++) { if (b.Length > (i * 8) + j) if (b[(i * 8) + j]) tmp[i] = (byte)(tmp[i] | (byte)(Math.Pow(2, j))); } } return tmp; } private static bool[] bytesToBits(byte[] b) { bool[] tmp = new bool[(b.Length * 8)]; for (int i = 0; i < b.Length; i++) { for (int j = 0; j <= 7; j++) { byte bIs = (byte)(b[i] | (byte)(Math.Pow(2, j))); tmp[(i * 8) + j] = bIs == b[i]; } } return tmp; } private static System.DateTime bitsToDate(byte[] b) { return FilePack.bitsToDate(FilePack.bytesToBits(b)); } private static System.DateTime bitsToDate(bool[] b) { int iDay = 0; int iMonth = 0; int iYear = 0; int iHours = 0; int iMinutes = 0; int iSeconds = 0; // Year for (int i = 0; i <= 8; i++) { if (b[8 - i] == true) iYear = iYear | (int)(Math.Pow(2, i)); } // Month for (int i = 0; i <= 4; i++) { if (b[13 - i] == true) iMonth = iMonth | (int)(Math.Pow(2, i)); } // Day for (int i = 0; i <= 5; i++) { if (b[19 - i] == true) iDay = iDay | (int)(Math.Pow(2, i)); } // Hour for (int i = 0; i <= 5; i++) { if (b[25 - i] == true) iHours = iHours | (int)(Math.Pow(2, i)); } // Minutes for (int i = 0; i <= 6; i++) { if (b[32 - i] == true) iMinutes = iMinutes | (int)(Math.Pow(2, i)); } // Seconds for (int i = 0; i <= 6; i++) { if (b[39 - i] == true) iSeconds = iSeconds | (int)(Math.Pow(2, i)); } return new System.DateTime(iYear + FilePack.BaseYear, iMonth, iDay, iHours, iMinutes, iSeconds, System.DateTimeKind.Utc); } #endregion #region "Shared Functions" /// /// Überprüft ob das Archiv Filepath gültig ist /// /// Gibt den Pfad des Archives an. /// Gibt bei erfolgreicher Prüfung true zurück. false falls das Archiv nicht gültig ist. public static bool IsValide(string Filepath) { if (File.Exists(Filepath)) { FileStream tstream = new FileStream(Filepath, FileMode.Open); if (tstream.Length < 17) { tstream.Close(); tstream.Dispose(); return false; } else { BinaryReader tb = new BinaryReader(tstream); byte[] s = tb.ReadBytes(10); if (FilePack.checkSignature(s)) { FilePackElements.eVersion SignatureVersion = (FilePackElements.eVersion)tb.ReadByte(); tb.Close(); tstream.Close(); tstream.Dispose(); switch (SignatureVersion) { case FilePackElements.eVersion.Version_1_0: return true; default: return false; } } else { tb.Close(); tstream.Close(); tstream.Dispose(); return false; } } } else { return false; } } /// /// Gibt die Archiv-Version der angegebenen Datei zurück. /// /// Gibt den Pfad des Archives an. /// Gibt die Archiv-Version der angegebenen Datei zurück. public static FilePackElements.eVersion GetFileVersion(string Filepath) { if (File.Exists(Filepath)) { FileStream tstream = new FileStream(Filepath, FileMode.Open); if (tstream.Length < 17) { tstream.Close(); tstream.Dispose(); return FilePackElements.eVersion.Unsuported; } else { BinaryReader tb = new BinaryReader(tstream); byte[] s = tb.ReadBytes(10); if (FilePack.checkSignature(s)) { FilePackElements.eVersion sv = (FilePackElements.eVersion)tb.ReadByte(); tb.Close(); tstream.Close(); tstream.Dispose(); return sv; } else { tb.Close(); tstream.Close(); tstream.Dispose(); return FilePackElements.eVersion.Unsuported; } } } else { return FilePackElements.eVersion.Unsuported; } } /// /// Überprüft das angegebene Archiv Filepath ob dieses mit einem Passwort geschützt ist. /// /// Gibt den Pfad des Archives an. /// true, wenn das angegebene Archiv mit einem Passwort geschützt ist. false wenn das Archiv nicht mit einem Passwort geschützt ist. public static bool IsPasswordProtectedFile(string Filepath) { if (File.Exists(Filepath)) { FileStream tstream = new FileStream(Filepath, FileMode.Open); if (tstream.Length < 17) { tstream.Close(); tstream.Dispose(); return false; } else { BinaryReader tb = new BinaryReader(tstream); byte[] s = tb.ReadBytes(10); if (FilePack.checkSignature(s)) { tb.ReadByte(); FilePackElements.eSet xset = (FilePackElements.eSet)tb.ReadByte(); byte upw = (byte)(xset | FilePackElements.eSet.UsePassword); tb.Close(); tstream.Close(); tstream.Dispose(); return (upw == (byte)xset); } else { tb.Close(); tstream.Close(); tstream.Dispose(); return false; } } } else { return false; } } #endregion } }