Ejemplo n.º 1
0
        /// <summary>
        /// Gets the <see cref="IFileFormat"/> from a file or byte array.
        /// </summary>
        /// <param name="FileName">The name of the file</param>
        /// <param name="data">The byte array of the data</param>
        /// <param name="InArchive">If the file is in an archive so it can be saved back</param>
        /// <param name="archiveNode">The node being replaced from an archive</param>
        /// <param name="Compressed">If the file is being compressed or not</param>
        /// <param name="CompType">The type of <see cref="CompressionType"/> being used</param>
        /// <returns></returns>
        public static IFileFormat OpenFileFormat(string FileName, byte[] data = null, bool InArchive     = false,
                                                 string ArchiveHash           = "", TreeNode archiveNode = null, bool Compressed = false, CompressionType CompType = 0)
        {
            if (data == null)
            {
                data = File.ReadAllBytes(FileName);
            }

            Cursor.Current = Cursors.WaitCursor;
            FileReader fileReader = new FileReader(data);
            string     Magic4     = fileReader.ReadMagic(0, 4);
            string     Magic2     = fileReader.ReadMagic(0, 2);

            if (Magic4 == "Yaz0")
            {
                data = EveryFileExplorer.YAZ0.Decompress(data);
                return(OpenFileFormat(FileName, data, InArchive, ArchiveHash, archiveNode, true, CompressionType.Yaz0));
            }
            if (Magic4 == "ZLIB")
            {
                data = FileReader.InflateZLIB(fileReader.getSection(64, data.Length - 64));
                return(OpenFileFormat(FileName, data, InArchive, ArchiveHash, archiveNode, true, CompressionType.Zlib));
            }
            fileReader.Dispose();
            fileReader.Close();
            foreach (IFileFormat fileFormat in FileManager.GetFileFormats())
            {
                if (fileFormat.Magic == Magic4 || fileFormat.Magic == Magic2)
                {
                    fileFormat.CompressionType  = CompType;
                    fileFormat.FileIsCompressed = Compressed;
                    fileFormat.Data             = data;
                    fileFormat.Load();
                    fileFormat.FileName              = Path.GetFileName(FileName);
                    fileFormat.FilePath              = FileName;
                    fileFormat.IFileInfo             = new IFileInfo();
                    fileFormat.IFileInfo.InArchive   = InArchive;
                    fileFormat.IFileInfo.ArchiveHash = ArchiveHash;
                    fileFormat.FileIsCompressed      = Compressed;
                    if (Compressed)
                    {
                        fileFormat.CompressionType = CompType;
                    }

                    if (fileFormat is TreeNode && archiveNode != null)
                    {
                        ((TreeNode)fileFormat).Text             = archiveNode.Text;
                        ((TreeNode)fileFormat).ImageKey         = archiveNode.ImageKey;
                        ((TreeNode)fileFormat).SelectedImageKey = archiveNode.SelectedImageKey;
                    }
                    return(fileFormat);
                }
                if (fileFormat.Magic == string.Empty)
                {
                    foreach (string str3 in fileFormat.Extension)
                    {
                        if (str3.Remove(0, 1) == Path.GetExtension(FileName))
                        {
                            fileFormat.Data = data;
                            fileFormat.Load();
                            fileFormat.FileName              = Path.GetFileName(FileName);
                            fileFormat.FilePath              = FileName;
                            fileFormat.IFileInfo             = new IFileInfo();
                            fileFormat.IFileInfo.InArchive   = true;
                            fileFormat.IFileInfo.ArchiveHash = ArchiveHash;

                            if (fileFormat is TreeNode)
                            {
                                ((TreeNode)fileFormat).Text             = archiveNode.Text;
                                ((TreeNode)fileFormat).ImageKey         = archiveNode.ImageKey;
                                ((TreeNode)fileFormat).SelectedImageKey = archiveNode.SelectedImageKey;
                            }
                            return(fileFormat);
                        }
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
        public void DecompressData(CompressionType CompressionType, byte[] data)
        {
            try
            {
                switch (CompressionType)
                {
                case CompressionType.Yaz0:
                    SaveFileForCompression(EveryFileExplorer.YAZ0.Decompress(data));
                    break;

                case CompressionType.Zlib:
                    break;

                case CompressionType.Gzip:
                    SaveFileForCompression(STLibraryCompression.GZIP.Decompress(data));
                    break;

                case CompressionType.Zstb:
                    SaveFileForCompression(STLibraryCompression.ZSTD.Decompress(data));
                    break;

                case CompressionType.Lz4f:
                    using (var reader = new FileReader(data))
                    {
                        reader.Position = 0;
                        int OuSize = reader.ReadInt32();
                        int InSize = data.Length - 4;
                        SaveFileForCompression(STLibraryCompression.Type_LZ4F.Decompress(reader.getSection(4, InSize)));
                    }
                    break;

                case CompressionType.Lz4:
                    SaveFileForCompression(STLibraryCompression.Type_LZ4.Decompress(data));
                    break;
                }
            }
            catch
            {
                MessageBox.Show($"File not compressed with {CompressionType} compression!");
            }
        }