SimpleZipReplacement.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #if ASTAR_NO_ZIP
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace Pathfinding.Serialization.Zip {
  6. public enum ZipOption {
  7. Always
  8. }
  9. public class ZipFile {
  10. public System.Text.Encoding AlternateEncoding;
  11. public ZipOption AlternateEncodingUsage = ZipOption.Always;
  12. public int ParallelDeflateThreshold = 0;
  13. Dictionary<string, ZipEntry> dict = new Dictionary<string, ZipEntry>();
  14. public void AddEntry (string name, byte[] bytes) {
  15. dict[name] = new ZipEntry(name, bytes);
  16. }
  17. public bool ContainsEntry (string name) {
  18. return dict.ContainsKey(name);
  19. }
  20. public void Save (System.IO.Stream stream) {
  21. var writer = new System.IO.BinaryWriter(stream);
  22. writer.Write(dict.Count);
  23. foreach (KeyValuePair<string, ZipEntry> pair in dict) {
  24. writer.Write(pair.Key);
  25. writer.Write(pair.Value.bytes.Length);
  26. writer.Write(pair.Value.bytes);
  27. }
  28. }
  29. public static ZipFile Read (System.IO.Stream stream) {
  30. ZipFile file = new ZipFile();
  31. var reader = new System.IO.BinaryReader(stream);
  32. int count = reader.ReadInt32();
  33. for (int i = 0; i < count; i++) {
  34. var name = reader.ReadString();
  35. var length = reader.ReadInt32();
  36. var bytes = reader.ReadBytes(length);
  37. file.dict[name] = new ZipEntry(name, bytes);
  38. }
  39. return file;
  40. }
  41. public ZipEntry this[string index] {
  42. get {
  43. ZipEntry v;
  44. dict.TryGetValue(index, out v);
  45. return v;
  46. }
  47. }
  48. public void Dispose () {
  49. }
  50. }
  51. public class ZipEntry {
  52. internal string name;
  53. internal byte[] bytes;
  54. public ZipEntry (string name, byte[] bytes) {
  55. this.name = name;
  56. this.bytes = bytes;
  57. }
  58. public void Extract (System.IO.Stream stream) {
  59. stream.Write(bytes, 0, bytes.Length);
  60. }
  61. }
  62. }
  63. #endif