public void AddFile(string filename)
        {
            FileInfo fi = new FileInfo(filename);
            string filePart;
            if (filename.ToLower().StartsWith(TempDirectory.ToLower()))
            {
                filePart = filename.Substring(TempDirectory.Length);
            }
            else
            {
                filePart = filename.Substring(filename.LastIndexOf(@"\") + 1);
            }

            if (!FileDirectory.ContainsKey(filePart))
            {
                FileEntry fe = new FileEntry(filePart, (int)fi.Length);
                fe.Offset = currentOffset;
                FileList.Add(fe);
                FileDirectory.Add(filePart, fe);
                currentOffset += fe.Size;
            }
        }
        public void Extract(bool overwrite, string targetDirectory)
        {
            try
            {
                string data;
                XmlDocument doc = new XmlDocument();
                int headerSize = 0;
                using (FileStream fs = File.OpenRead(Filename))
                {

                    byte[] buffer = new byte[256];
                    fs.Read(buffer, 0, 255);
                    data = Encoding.UTF8.GetString(buffer);

                    int start = data.IndexOf("0x");
                    if (start == -1)
                    {
                        throw new SystemException(Language.GetLocalizedText(215, "Invalid File Format"));
                    }
                    headerSize = Convert.ToInt32(data.Substring(start, 10), 16);

                    fs.Seek(0, SeekOrigin.Begin);

                    buffer = new byte[headerSize];
                    fs.Read(buffer, 0, headerSize);
                    data = Encoding.UTF8.GetString(buffer);
                    doc.LoadXml(data);

                    XmlNode cab = doc["FileCabinet"];
                    XmlNode files = cab["Files"];

                    int offset = headerSize;
                    FileList.Clear();
                    foreach (XmlNode child in files.ChildNodes)
                    {
                        FileEntry fe = new FileEntry(child.Attributes["Name"].Value.ToString(), Convert.ToInt32(child.Attributes["Size"].Value));
                        fe.Offset = offset;
                        offset += fe.Size;
                        FileList.Add(fe);
                    }

                    foreach (FileEntry entry in FileList)
                    {
                        while (entry.Filename.StartsWith("\\"))
                        {
                            entry.Filename = entry.Filename.Substring(1);
                        }
                        string fullPath = TempDirectory + @"\" + entry.Filename;
                        string dir = fullPath.Substring(0, fullPath.LastIndexOf("\\"));
                        string file = fullPath.Substring(fullPath.LastIndexOf("\\") + 1);

                        if (!string.IsNullOrEmpty(targetDirectory) && entry.Filename.Contains("\\"))
                        {
                            fullPath = targetDirectory + "\\" + file;
                            dir = targetDirectory;
                        }

                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        if (overwrite || !File.Exists(fullPath))
                        {
                            FileStream fileOut = new FileStream(fullPath, FileMode.Create);
                            buffer = new byte[entry.Size];
                            fs.Seek(entry.Offset, SeekOrigin.Begin);
                            if (fs.Read(buffer, 0, entry.Size) != entry.Size)
                            {
                                throw new SystemException(Language.GetLocalizedText(214, "One of the files in the collection is missing, corrupt or inaccessable"));
                            }
                            fileOut.Write(buffer, 0, entry.Size);
                            fileOut.Close();
                        }
                    }

                    int x = offset;
                    fs.Close();
                }
            }
            catch
            {
              //  UiTools.ShowMessageBox("The data cabinet file was not found. WWT will now download all data from network.");
            }
        }
        public void Extract(bool overwrite, string targetDirectory)
        {
            try
            {
                string      data;
                XmlDocument doc        = new XmlDocument();
                int         headerSize = 0;
                using (FileStream fs = File.OpenRead(Filename))
                {
                    byte[] buffer = new byte[256];
                    fs.Read(buffer, 0, 255);
                    data = Encoding.UTF8.GetString(buffer);

                    int start = data.IndexOf("0x");
                    if (start == -1)
                    {
                        throw new SystemException(Language.GetLocalizedText(215, "Invalid File Format"));
                    }
                    headerSize = Convert.ToInt32(data.Substring(start, 10), 16);

                    fs.Seek(0, SeekOrigin.Begin);


                    buffer = new byte[headerSize];
                    fs.Read(buffer, 0, headerSize);
                    data = Encoding.UTF8.GetString(buffer);
                    doc.LoadXml(data);

                    XmlNode cab   = doc["FileCabinet"];
                    XmlNode files = cab["Files"];

                    int offset = headerSize;
                    FileList.Clear();
                    foreach (XmlNode child in files.ChildNodes)
                    {
                        FileEntry fe = new FileEntry(child.Attributes["Name"].Value.ToString(), Convert.ToInt32(child.Attributes["Size"].Value));
                        fe.Offset = offset;
                        offset   += fe.Size;
                        FileList.Add(fe);
                    }

                    foreach (FileEntry entry in FileList)
                    {
                        while (entry.Filename.StartsWith("\\"))
                        {
                            entry.Filename = entry.Filename.Substring(1);
                        }
                        string fullPath = TempDirectory + @"\" + entry.Filename;
                        string dir      = fullPath.Substring(0, fullPath.LastIndexOf("\\"));
                        string file     = fullPath.Substring(fullPath.LastIndexOf("\\") + 1);

                        if (!string.IsNullOrEmpty(targetDirectory) && entry.Filename.Contains("\\"))
                        {
                            fullPath = targetDirectory + "\\" + file;
                            dir      = targetDirectory;
                        }

                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        if (overwrite || !File.Exists(fullPath))
                        {
                            FileStream fileOut = new FileStream(fullPath, FileMode.Create);
                            buffer = new byte[entry.Size];
                            fs.Seek(entry.Offset, SeekOrigin.Begin);
                            if (fs.Read(buffer, 0, entry.Size) != entry.Size)
                            {
                                throw new SystemException(Language.GetLocalizedText(214, "One of the files in the collection is missing, corrupt or inaccessable"));
                            }
                            fileOut.Write(buffer, 0, entry.Size);
                            fileOut.Close();
                        }
                    }


                    int x = offset;
                    fs.Close();
                }
            }
            catch
            {
                //  UiTools.ShowMessageBox("The data cabinet file was not found. WWT will now download all data from network.");
            }
        }