CefZipReader.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Class that supports the reading of zip archives via the zlib unzip API.
  10. /// The methods of this class should only be called on the thread that creates
  11. /// the object.
  12. /// </summary>
  13. public sealed unsafe partial class CefZipReader
  14. {
  15. /// <summary>
  16. /// Create a new CefZipReader object. The returned object's methods can only
  17. /// be called from the thread that created the object.
  18. /// </summary>
  19. public static CefZipReader Create(CefStreamReader stream)
  20. {
  21. if (stream == null) throw new ArgumentNullException("stream");
  22. return CefZipReader.FromNative(
  23. cef_zip_reader_t.create(stream.ToNative())
  24. );
  25. }
  26. /// <summary>
  27. /// Moves the cursor to the first file in the archive. Returns true if the
  28. /// cursor position was set successfully.
  29. /// </summary>
  30. public bool MoveToFirstFile()
  31. {
  32. return cef_zip_reader_t.move_to_first_file(_self) != 0;
  33. }
  34. /// <summary>
  35. /// Moves the cursor to the next file in the archive. Returns true if the
  36. /// cursor position was set successfully.
  37. /// </summary>
  38. public bool MoveToNextFile()
  39. {
  40. return cef_zip_reader_t.move_to_next_file(_self) != 0;
  41. }
  42. /// <summary>
  43. /// Moves the cursor to the specified file in the archive. If |caseSensitive|
  44. /// is true then the search will be case sensitive. Returns true if the cursor
  45. /// position was set successfully.
  46. /// </summary>
  47. public bool MoveToFile(string fileName, bool caseSensitive)
  48. {
  49. fixed (char* fileName_str = fileName)
  50. {
  51. var n_fileName = new cef_string_t(fileName_str, fileName != null ? fileName.Length : 0);
  52. return cef_zip_reader_t.move_to_file(_self, &n_fileName, caseSensitive ? 1 : 0) != 0;
  53. }
  54. }
  55. /// <summary>
  56. /// Closes the archive. This should be called directly to ensure that cleanup
  57. /// occurs on the correct thread.
  58. /// </summary>
  59. public bool Close()
  60. {
  61. return cef_zip_reader_t.close(_self) != 0;
  62. }
  63. /// <summary>
  64. /// The below methods act on the file at the current cursor position.
  65. /// Returns the name of the file.
  66. /// </summary>
  67. public string GetFileName()
  68. {
  69. var n_result = cef_zip_reader_t.get_file_name(_self);
  70. return cef_string_userfree.ToString(n_result);
  71. }
  72. /// <summary>
  73. /// Returns the uncompressed size of the file.
  74. /// </summary>
  75. public long GetFileSize()
  76. {
  77. return cef_zip_reader_t.get_file_size(_self);
  78. }
  79. /// <summary>
  80. /// Returns the last modified timestamp for the file.
  81. /// </summary>
  82. public DateTime GetFileLastModified()
  83. {
  84. var time = cef_zip_reader_t.get_file_last_modified(_self);
  85. return cef_time_t.ToDateTime(&time);
  86. }
  87. /// <summary>
  88. /// Opens the file for reading of uncompressed data. A read password may
  89. /// optionally be specified.
  90. /// </summary>
  91. public bool OpenFile(string password)
  92. {
  93. fixed (char* password_str = password)
  94. {
  95. var n_password = new cef_string_t(password_str, password != null ? password.Length : 0);
  96. return cef_zip_reader_t.open_file(_self, &n_password) != 0;
  97. }
  98. }
  99. /// <summary>
  100. /// Closes the file.
  101. /// </summary>
  102. public bool CloseFile()
  103. {
  104. return cef_zip_reader_t.close_file(_self) != 0;
  105. }
  106. /// <summary>
  107. /// Read uncompressed file contents into the specified buffer. Returns &lt; 0 if
  108. /// an error occurred, 0 if at the end of file, or the number of bytes read.
  109. /// </summary>
  110. public int ReadFile(byte[] buffer, int offset, int length)
  111. {
  112. if (offset < 0 || length < 0 || buffer.Length - offset < length) throw new ArgumentOutOfRangeException();
  113. fixed (byte* buffer_ptr = buffer)
  114. {
  115. return cef_zip_reader_t.read_file(_self, buffer_ptr + offset, (UIntPtr)length);
  116. }
  117. }
  118. /// <summary>
  119. /// Returns the current offset in the uncompressed file contents.
  120. /// </summary>
  121. public long Tell()
  122. {
  123. return cef_zip_reader_t.tell(_self);
  124. }
  125. /// <summary>
  126. /// Returns true if at end of the file contents.
  127. /// </summary>
  128. public bool Eof()
  129. {
  130. return cef_zip_reader_t.eof(_self) != 0;
  131. }
  132. }
  133. }