ZipReader.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. namespace FairyGUI.Utils
  2. {
  3. /// <summary>
  4. /// 一个简单的Zip文件处理类。不处理解压。
  5. /// </summary>
  6. public class ZipReader
  7. {
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. public class ZipEntry
  12. {
  13. public string name;
  14. public int compress;
  15. public uint crc;
  16. public int size;
  17. public int sourceSize;
  18. public int offset;
  19. public bool isDirectory;
  20. }
  21. ByteBuffer _stream;
  22. int _entryCount;
  23. int _pos;
  24. int _index;
  25. /// <summary>
  26. ///
  27. /// </summary>
  28. /// <param name="stream"></param>
  29. public ZipReader(byte[] data)
  30. {
  31. _stream = new ByteBuffer(data);
  32. _stream.littleEndian = true;
  33. int pos = _stream.length - 22;
  34. _stream.position = pos + 10;
  35. _entryCount = _stream.ReadShort();
  36. _stream.position = pos + 16;
  37. _pos = _stream.ReadInt();
  38. }
  39. /// <summary>
  40. ///
  41. /// </summary>
  42. public int entryCount
  43. {
  44. get { return _entryCount; }
  45. }
  46. /// <summary>
  47. ///
  48. /// </summary>
  49. /// <returns></returns>
  50. public bool GetNextEntry(ZipEntry entry)
  51. {
  52. if (_index >= _entryCount)
  53. return false;
  54. _stream.position = _pos + 28;
  55. int len = _stream.ReadUshort();
  56. int len2 = _stream.ReadUshort() + _stream.ReadUshort();
  57. _stream.position = _pos + 46;
  58. string name = _stream.ReadString(len);
  59. name = name.Replace("\\", "/");
  60. entry.name = name;
  61. if (name[name.Length - 1] == '/') //directory
  62. {
  63. entry.isDirectory = true;
  64. entry.compress = 0;
  65. entry.crc = 0;
  66. entry.size = entry.sourceSize = 0;
  67. entry.offset = 0;
  68. }
  69. else
  70. {
  71. entry.isDirectory = false;
  72. _stream.position = _pos + 10;
  73. entry.compress = _stream.ReadUshort();
  74. _stream.position = _pos + 16;
  75. entry.crc = _stream.ReadUint();
  76. entry.size = _stream.ReadInt();
  77. entry.sourceSize = _stream.ReadInt();
  78. _stream.position = _pos + 42;
  79. entry.offset = _stream.ReadInt() + 30 + len;
  80. }
  81. _pos += 46 + len + len2;
  82. _index++;
  83. return true;
  84. }
  85. /// <summary>
  86. ///
  87. /// </summary>
  88. /// <param name="name"></param>
  89. /// <returns></returns>
  90. public byte[] GetEntryData(ZipEntry entry)
  91. {
  92. byte[] data = new byte[entry.size];
  93. if (entry.size > 0)
  94. {
  95. _stream.position = entry.offset;
  96. _stream.ReadBytes(data, 0, entry.size);
  97. }
  98. return data;
  99. }
  100. }
  101. }