/// <summary> /// Extract bytes from Pk2 file. /// </summary> public byte[] GetFileBytes(Pk2File File) { BinaryReader reader = new BinaryReader(m_FileStream); reader.BaseStream.Position = File.Position; return(reader.ReadBytes((int)File.Size)); }
/// <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)); }
/// <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(); }
/// <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); }
/// <summary> /// Gets the Pk2 file info /// </summary> /// <param name="Path">Path to the location inside Pk2</param> public Pk2File GetFile(string Path) { if (Path == "") { return(null); } // Normalize key format Path = Path.ToUpper(m_EnglishCulture).Replace("/", "\\"); Pk2File file = null; m_Files.TryGetValue(Path, out file); return(file); }
/// <summary> /// Extract the stream from the file. /// </summary> public Stream GetFileStream(Pk2File File) { return(new MemoryStream(GetFileBytes(File))); }
/// <summary> /// Reads Pk2 block structure from the position specified and save all data into the Folder /// </summary> private void Read(long Position, Pk2Folder CurrentFolder, string ParentPath) { // Set cursor position in the stream BinaryReader reader = new BinaryReader(m_FileStream); reader.BaseStream.Position = Position; // Keep a list with all folders from this block to add it to subfolders List <Pk2Folder> subfolders = new List <Pk2Folder>(); // Read pk2 block sPk2EntryBlock entryBlock = (sPk2EntryBlock)BufferToStruct(m_Blowfish.Decode(reader.ReadBytes(Marshal.SizeOf(typeof(sPk2EntryBlock)))), typeof(sPk2EntryBlock)); for (int i = 0; i < 20; i++) { // Entry sPk2Entry entry = entryBlock.Entries[i]; // Check entry type switch (entry.Type) { // Null Entry case 0: break; // Folder case 1: // Check if is not a parent/root folder if (entry.Name != "." && entry.Name != "..") { Pk2Folder folder = new Pk2Folder() { Name = entry.Name, Position = BitConverter.ToInt64(entry.g_Position, 0) }; // Add subfolder subfolders.Add(folder); // Save dictionary reference m_Folders[ParentPath + entry.Name.ToUpper(m_EnglishCulture)] = folder; } break; // File case 2: Pk2File file = new Pk2File { Position = entry.Position, Name = entry.Name, Size = entry.Size, ParentFolder = CurrentFolder }; // Add files to the current folder CurrentFolder.Files.Add(file); // Save dictionary reference m_Files[ParentPath + entry.Name.ToUpper(m_EnglishCulture)] = file; break; } } // Read the next pk2 block chain if (entryBlock.Entries[19].NextChain != 0) { Read(entryBlock.Entries[19].NextChain, CurrentFolder, ParentPath); } // Add subfolders to the current folder CurrentFolder.SubFolders.AddRange(subfolders); // Continue reading folder by folder foreach (var f in subfolders) { Read(f.Position, f, ParentPath + f.Name.ToUpper(m_EnglishCulture) + "\\"); } }