PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/NekoKun.Core/Helpers/ArchiveFile.cs

https://bitbucket.org/nekokun/nekokun
C# | 88 lines | 78 code | 10 blank | 0 comment | 4 complexity | 86de6af91a7d3e77f9b527da4a0a949b MD5 | raw file
Possible License(s): MIT, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using NekoKun.Serialization.RubyMarshal;
  6. namespace NekoKun
  7. {
  8. public class ArchiveFile: AbstractFile
  9. {
  10. protected List<string> files;
  11. protected Dictionary<string, byte[]> contents;
  12. protected string manifest;
  13. public ArchiveFile(string filename)
  14. : base(filename)
  15. {
  16. FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
  17. manifest = NekoKun.Serialization.RubyMarshal.RubyMarshal.Load(fs) as string;
  18. contents = new Dictionary<string, byte[]>();
  19. files = new List<string> (Array.ConvertAll<object, string>(
  20. (NekoKun.Serialization.RubyMarshal.RubyMarshal.Load(fs) as List<object>).ToArray(),
  21. (object o) => {
  22. contents.Add(o.ToString(), null);
  23. return o.ToString();
  24. }
  25. ));
  26. fs.Close();
  27. }
  28. public string Manifest { get { return this.manifest; } }
  29. protected override void Save()
  30. {
  31. throw new NotImplementedException();
  32. }
  33. public override AbstractEditor CreateEditor()
  34. {
  35. throw new NotImplementedException();
  36. }
  37. public byte[] this[string key]
  38. {
  39. get
  40. {
  41. if (contents.ContainsKey(key))
  42. {
  43. if (contents[key] == null)
  44. LoadData();
  45. return contents[key];
  46. }
  47. return null;
  48. }
  49. }
  50. private void LoadData()
  51. {
  52. FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
  53. NekoKun.Serialization.RubyMarshal.RubyMarshal.Load(fs);
  54. NekoKun.Serialization.RubyMarshal.RubyMarshal.Load(fs);
  55. byte[] buffer = Ionic.Zlib.ZlibStream.UncompressBuffer((NekoKun.Serialization.RubyMarshal.RubyMarshal.Load(fs) as RubyString).Raw);
  56. MemoryStream ms = new MemoryStream(buffer, false);
  57. Dictionary<object, object> con = NekoKun.Serialization.RubyMarshal.RubyMarshal.Load(ms) as Dictionary<object, object>;
  58. fs.Close();
  59. foreach (var item in con)
  60. {
  61. string key = (item.Key as RubyString).Text;
  62. this.contents[key] = (item.Value as RubyString).Raw;
  63. }
  64. }
  65. public void Extract(string dir)
  66. {
  67. foreach (string item in this.files)
  68. {
  69. string name = item.Replace('/', System.IO.Path.DirectorySeparatorChar);
  70. string fullname = System.IO.Path.Combine(dir, name);
  71. string dirname = System.IO.Path.GetDirectoryName(fullname);
  72. if (!System.IO.Directory.Exists(dirname))
  73. System.IO.Directory.CreateDirectory(dirname);
  74. System.IO.File.WriteAllBytes(fullname, this[item]);
  75. }
  76. }
  77. }
  78. }