ContainerList_GetStream.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System.Linq;
  2. using System.IO;
  3. namespace System.Fire.Container {
  4. public partial class ContainerList {
  5. /// <summary>
  6. /// Get the data as stream object that was stored in the container
  7. /// </summary>
  8. /// <param name="FileIndex">The container file index</param>
  9. /// <returns>Returns the extracted data as stream from the container</returns>
  10. public Stream GetStream(int FileIndex) {
  11. return this.GetStream(this._files[FileIndex]);
  12. }
  13. /// <summary>
  14. /// Get the data as stream object that was stored in the container
  15. /// </summary>
  16. /// <param name="File">The container file index</param>
  17. /// <returns>Returns the extracted data as stream from the container</returns>
  18. public Stream GetStream(ContainerFile File) {
  19. MemoryStream mem = new MemoryStream();
  20. BinaryWriter bw = new BinaryWriter(mem);
  21. long size = 0;
  22. for (int i = 0, c = File.NumChunks; i < c; i++) {
  23. byte[] b = this.ReadChunk(File.ChunkPos, i);
  24. if (size + b.Length > File.Size) {
  25. bw.Write(b.Take((int)(File.Size - size)).ToArray());
  26. size += File.Size - size;
  27. } else {
  28. bw.Write(b);
  29. size += b.Length;
  30. }
  31. }
  32. return mem;
  33. }
  34. }
  35. }