Exemplo n.º 1
0
        /// <summary>
        /// Extract the bytes from the file.
        /// </summary>
        public byte[] GetFileBytes(Pk2File File)
        {
            BinaryReader reader = new BinaryReader(m_FileStream);

            reader.BaseStream.Position = File.Position;
            return(reader.ReadBytes((int)File.Size));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Read the Pk2 structure recursively.
        /// </summary>
        private void Read(long Position, string RootPath)
        {
            BinaryReader reader = new BinaryReader(m_FileStream);

            reader.BaseStream.Position = Position;
            List <Pk2Folder> folders    = new List <Pk2Folder>();
            sPk2EntryBlock   entryBlock = (sPk2EntryBlock)BufferToStruct(m_Blowfish.Decode(reader.ReadBytes(Marshal.SizeOf(typeof(sPk2EntryBlock)))), typeof(sPk2EntryBlock));

            for (int i = 0; i < 20; i++)
            {
                sPk2Entry entry = entryBlock.Entries[i];                 //.....
                switch (entry.Type)
                {
                case 0:                         //Null Entry

                    break;

                case 1:                         //Folder
                    if (entry.Name != "." && entry.Name != "..")
                    {
                        Pk2Folder folder = new Pk2Folder();
                        folder.Name     = entry.Name;
                        folder.Position = BitConverter.ToInt64(entry.g_Position, 0);
                        folders.Add(folder);
                        m_Folders[(RootPath + entry.Name).ToUpper()] = folder;
                        m_CurrentFolder.SubFolders.Add(folder);
                    }
                    break;

                case 2:                         //File
                    Pk2File file = new Pk2File();
                    file.Position     = entry.Position;
                    file.Name         = entry.Name;
                    file.Size         = entry.Size;
                    file.ParentFolder = m_CurrentFolder;
                    m_Files[(RootPath + entry.Name).ToUpper()] = file;
                    m_CurrentFolder.Files.Add(file);
                    break;
                }
            }
            if (entryBlock.Entries[19].NextChain != 0)
            {
                Read(entryBlock.Entries[19].NextChain, RootPath);
            }

            foreach (Pk2Folder folder in folders)
            {
                m_CurrentFolder = folder;
                if (folder.Files == null)
                {
                    folder.Files = new List <Pk2File>();
                }
                if (folder.SubFolders == null)
                {
                    folder.SubFolders = new List <Pk2Folder>();
                }
                Read(folder.Position, RootPath + folder.Name + "\\");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Extract the bytes from Pk2 path specified.
        /// </summary>
        public byte[] GetFileBytes(string Path)
        {
            BinaryReader reader = new BinaryReader(m_FileStream);
            Pk2File      file   = GetFile(Path);

            reader.BaseStream.Position = file.Position;
            return(reader.ReadBytes((int)file.Size));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Extract a file from the buffer to an output path.
        /// </summary>
        public void ExtractFile(Pk2File File, string OutputPath)
        {
            byte[]       data   = GetFileBytes(File);
            FileStream   stream = new FileStream(OutputPath, FileMode.Create);
            BinaryWriter writer = new BinaryWriter(stream);

            writer.Write(data);
            stream.Close();
        }
Exemplo n.º 5
0
 /// <summary>
 /// Extract the string text from the file.
 /// </summary>
 public string GetFileText(Pk2File File)
 {
     byte[] tempBuffer = GetFileBytes(File);
     if (tempBuffer != null)
     {
         TextReader txtReader = new StreamReader(new MemoryStream(tempBuffer));
         return(txtReader.ReadToEnd());
     }
     return(null);
 }
Exemplo n.º 6
0
        /// <summary>
        /// Get a file from Pk2 path specified.
        /// </summary>
        public Pk2File GetFile(string FilePath)
        {
            if (FilePath == "")
            {
                return(null);
            }

            // Normalize to the same dictionary key path format
            FilePath = FilePath.ToUpper();
            FilePath = FilePath.Replace("/", "\\");

            Pk2File file = null;

            m_Files.TryGetValue(FilePath, out file);
            return(file);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Get the file extension from the file.
        /// </summary>
        public string GetFileExtension(Pk2File File)
        {
            int Offset = File.Name.LastIndexOf('.');

            return(File.Name.Substring(Offset));
        }
Exemplo n.º 8
0
 /// <summary>
 /// Extract the stream from the file.
 /// </summary>
 public Stream GetFileStream(Pk2File File)
 {
     return(new MemoryStream(GetFileBytes(File)));
 }