public void SaveAll(object oinfo)
        {
            var info      = (SaveAllInformation)oinfo;
            var usedNames = new Dictionary <uint, string>();

            IEnumerable <uint> saving = info.Saving ?? info.Table.Keys;

            this.SetStatus("", 0);

            int total   = saving.Count();
            int current = 0;

            var buffer = new byte[0x4000];

            foreach (var hash in saving)
            {
                current++;

                ArchiveTableFile.Entry index = info.Table.Get(hash);
                string fileName;

                bool decompressing = false;
                if (info.FileNames.Contains(hash) == true)
                {
                    fileName        = info.FileNames[hash];
                    usedNames[hash] = info.FileNames[hash];

                    if (info.Settings.DecompressSmallArchives == true)
                    {
                        string extension = Path.GetExtension(fileName);
                        if (extension == ".blz" || extension == ".blx360z" || extension == ".bl3z" ||
                            extension == ".eez" || extension == ".eex360z" || extension == ".ee3z" ||
                            extension == ".flz" ||
                            extension == ".nlz")
                        {
                            decompressing = true;
                        }
                    }
                }
                else
                {
                    if (info.Settings.SaveOnlyKnownFiles)
                    {
                        this.SetStatus("Skipping...", (int)((current / (float)total) * 100.0f));
                        continue;
                    }

                    fileName = hash.ToString("X8");

                    if (true)
                    {
                        info.Archive.Seek(index.Offset, SeekOrigin.Begin);
                        var guess = new byte[16];
                        int read  = info.Archive.Read(guess, 0, (int)Math.Min(guess.Length, index.Size));

                        if (read >= 2 && guess[0] == 0x78 && guess[1] == 0x01 &&
                            info.Settings.DecompressUnknownFiles == true)
                        {
                            info.Archive.Seek(index.Offset, SeekOrigin.Begin);

                            decompressing = true;
                            using (var memory = info.Archive.ReadToMemoryStream(index.Size))
                            {
                                var zlib = new InflaterInputStream(memory);
                                read = zlib.Read(guess, 0, guess.Length);
                                if (read < 0)
                                {
                                    throw new InvalidOperationException("zlib error");
                                }
                            }
                        }

                        var extension = FileExtensions.Detect(guess, read);

                        if (extension.Value != null)
                        {
                            fileName = Path.ChangeExtension(fileName, "." + extension.Value);
                        }

                        if (extension.Key != null)
                        {
                            fileName = Path.Combine(extension.Key, fileName);
                        }
                    }

                    fileName = Path.Combine("__UNKNOWN", fileName);
                }

                string path = Path.Combine(info.BasePath, fileName);
                if (File.Exists(path) == true &&
                    info.Settings.DontOverwriteFiles == true)
                {
                    this.SetStatus("Skipping...", (int)((current / (float)total) * 100.0f));
                    continue;
                }

                this.SetStatus(fileName, (int)((current / (float)total) * 100.0f));

                Directory.CreateDirectory(Path.Combine(info.BasePath, Path.GetDirectoryName(fileName)));

                info.Archive.Seek(index.Offset, SeekOrigin.Begin);

                using (var output = File.Create(path))
                {
                    if (decompressing == true)
                    {
                        using (var memory = info.Archive.ReadToMemoryStream(index.Size))
                        {
                            var zlib = new InflaterInputStream(memory);

                            while (true)
                            {
                                int read = zlib.Read(buffer, 0, buffer.Length);
                                if (read < 0)
                                {
                                    throw new InvalidOperationException("zlib error");
                                }

                                if (read == 0)
                                {
                                    break;
                                }

                                output.Write(buffer, 0, read);
                            }
                        }
                    }
                    else
                    {
                        output.WriteFromStream(info.Archive, index.Size);
                    }
                }
            }

            this.SaveDone();
        }
Exemplo n.º 2
0
        private void BuildFileTree()
        {
            this.fileList.Nodes.Clear();
            this.fileList.BeginUpdate();

            if (this._Table != null)
            {
                var dirNodes = new Dictionary <string, TreeNode>();

                var baseNode    = new TreeNode(Path.GetFileName(this.openDialog.FileName), 0, 0);
                var knownNode   = new TreeNode("Known", 1, 1);
                var unknownNode = new TreeNode("Unknown", 1, 1);

                foreach (uint hash in this._Table.Keys
                         .OrderBy(k => k, new FileNameHashComparer(this._Hashes)))
                {
                    ArchiveTableFile.Entry entry = this._Table[hash];
                    TreeNode node;

                    if (this._Hashes != null && this._Hashes.Contains(hash) == true)
                    {
                        string             fileName    = this._Hashes[hash];
                        string             pathName    = Path.GetDirectoryName(fileName);
                        TreeNodeCollection parentNodes = knownNode.Nodes;

                        if (string.IsNullOrEmpty(pathName) == false)
                        {
                            string[] dirs = pathName.Split(new[]
                            {
                                '\\'
                            });

                            foreach (string dir in dirs)
                            {
                                if (parentNodes.ContainsKey(dir))
                                {
                                    parentNodes = parentNodes[dir].Nodes;
                                }
                                else
                                {
                                    TreeNode parentNode = parentNodes.Add(dir, dir, 2, 2);
                                    parentNodes = parentNode.Nodes;
                                }
                            }
                        }

                        node = parentNodes.Add(null, Path.GetFileName(fileName), 3, 3);
                    }
                    else
                    {
                        node = unknownNode.Nodes.Add(null, hash.ToString("X8"), 3, 3);
                    }

                    node.Tag = hash;
                }

                if (knownNode.Nodes.Count > 0)
                {
                    baseNode.Nodes.Add(knownNode);
                }

                if (unknownNode.Nodes.Count > 0)
                {
                    baseNode.Nodes.Add(unknownNode);
                    unknownNode.Text = "Unknown (" +
                                       unknownNode.Nodes.Count.ToString(
                        System.Globalization.CultureInfo.InvariantCulture) + ")";
                }

                if (knownNode.Nodes.Count > 0)
                {
                    knownNode.Expand();
                }
                else if (unknownNode.Nodes.Count > 0)
                {
                    //unknownNode.Expand();
                }

                baseNode.Expand();
                this.fileList.Nodes.Add(baseNode);
            }

            //this.fileList.Sort();
            this.fileList.EndUpdate();
        }