ContainerList_Export.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. namespace System.Fire.Container {
  8. public partial class ContainerList {
  9. /// <summary>
  10. /// Exports an file from the container to the given path
  11. /// </summary>
  12. /// <param name="FileIndex">The container file index</param>
  13. /// <param name="ExportPath">Path to export the file to</param>
  14. /// <returns>Returns true if the export was successfull</returns>
  15. public bool ExportFile(int FileIndex, string ExportPath) {
  16. return this.ExportFile(this._files[FileIndex], ExportPath);
  17. }
  18. /// <summary>
  19. /// Exports an file from the container to the given path
  20. /// </summary>
  21. /// <param name="File">The container file index</param>
  22. /// <param name="ExportPath">Path to export the file to</param>
  23. /// <returns>Returns true if the export was successfull</returns>
  24. public bool ExportFile(ContainerFile File, string ExportPath) {
  25. FileInfo fi = new FileInfo(ExportPath);
  26. FileAttributes fa = new DirectoryInfo(fi.DirectoryName).Attributes;
  27. if ((fa | FileAttributes.ReadOnly) == fa) {
  28. System.Diagnostics.Debug.WriteLine(fi.DirectoryName + " is ReadOnly");
  29. return false;
  30. }
  31. if ((fa | FileAttributes.Hidden) == fa) {
  32. System.Diagnostics.Debug.WriteLine(fi.DirectoryName + " is Hidden");
  33. }
  34. if (!Directory.Exists(fi.DirectoryName))
  35. Directory.CreateDirectory(fi.DirectoryName);
  36. if (System.IO.File.Exists(ExportPath))
  37. System.IO.File.Delete(ExportPath);
  38. FileStream fs = new FileStream(ExportPath, FileMode.CreateNew);
  39. BinaryWriter bw = new BinaryWriter(fs);
  40. //byte Group = 0, Container = 0;
  41. //ushort Pos = 0;
  42. long size = 0;
  43. for (int i = 0, c = File.NumChunks; i < c; i++) {
  44. //File.GetChunk(i, ref Group, ref Container, ref Pos);
  45. byte[] b = this.ReadChunk(File.ChunkPos, i);
  46. if (size + b.Length > File.Size) {
  47. bw.Write(b.Take((int)(File.Size - size)).ToArray());
  48. size += File.Size - size;
  49. } else {
  50. bw.Write(b);
  51. size += b.Length;
  52. }
  53. }
  54. bw.Close();
  55. return true;
  56. }
  57. }
  58. }