GZipStream.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // © Copyright Henrik Ravn 2004
  3. //
  4. // Use, modification and distribution are subject to the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. using System;
  8. using System.IO;
  9. using System.Runtime.InteropServices;
  10. namespace DotZLib
  11. {
  12. /// <summary>
  13. /// Implements a compressed <see cref="Stream"/>, in GZip (.gz) format.
  14. /// </summary>
  15. public class GZipStream : Stream, IDisposable
  16. {
  17. #region Dll Imports
  18. [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)]
  19. private static extern IntPtr gzopen(string name, string mode);
  20. [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
  21. private static extern int gzclose(IntPtr gzFile);
  22. [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
  23. private static extern int gzwrite(IntPtr gzFile, int data, int length);
  24. [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
  25. private static extern int gzread(IntPtr gzFile, int data, int length);
  26. [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
  27. private static extern int gzgetc(IntPtr gzFile);
  28. [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
  29. private static extern int gzputc(IntPtr gzFile, int c);
  30. #endregion
  31. #region Private data
  32. private IntPtr _gzFile;
  33. private bool _isDisposed = false;
  34. private bool _isWriting;
  35. #endregion
  36. #region Constructors
  37. /// <summary>
  38. /// Creates a new file as a writeable GZipStream
  39. /// </summary>
  40. /// <param name="fileName">The name of the compressed file to create</param>
  41. /// <param name="level">The compression level to use when adding data</param>
  42. /// <exception cref="ZLibException">If an error occurred in the internal zlib function</exception>
  43. public GZipStream(string fileName, CompressLevel level)
  44. {
  45. _isWriting = true;
  46. _gzFile = gzopen(fileName, String.Format("wb{0}", (int)level));
  47. if (_gzFile == IntPtr.Zero)
  48. throw new ZLibException(-1, "Could not open " + fileName);
  49. }
  50. /// <summary>
  51. /// Opens an existing file as a readable GZipStream
  52. /// </summary>
  53. /// <param name="fileName">The name of the file to open</param>
  54. /// <exception cref="ZLibException">If an error occurred in the internal zlib function</exception>
  55. public GZipStream(string fileName)
  56. {
  57. _isWriting = false;
  58. _gzFile = gzopen(fileName, "rb");
  59. if (_gzFile == IntPtr.Zero)
  60. throw new ZLibException(-1, "Could not open " + fileName);
  61. }
  62. #endregion
  63. #region Access properties
  64. /// <summary>
  65. /// Returns true of this stream can be read from, false otherwise
  66. /// </summary>
  67. public override bool CanRead
  68. {
  69. get
  70. {
  71. return !_isWriting;
  72. }
  73. }
  74. /// <summary>
  75. /// Returns false.
  76. /// </summary>
  77. public override bool CanSeek
  78. {
  79. get
  80. {
  81. return false;
  82. }
  83. }
  84. /// <summary>
  85. /// Returns true if this tsream is writeable, false otherwise
  86. /// </summary>
  87. public override bool CanWrite
  88. {
  89. get
  90. {
  91. return _isWriting;
  92. }
  93. }
  94. #endregion
  95. #region Destructor & IDispose stuff
  96. /// <summary>
  97. /// Destroys this instance
  98. /// </summary>
  99. ~GZipStream()
  100. {
  101. cleanUp(false);
  102. }
  103. /// <summary>
  104. /// Closes the external file handle
  105. /// </summary>
  106. public void Dispose()
  107. {
  108. cleanUp(true);
  109. }
  110. // Does the actual closing of the file handle.
  111. private void cleanUp(bool isDisposing)
  112. {
  113. if (!_isDisposed)
  114. {
  115. gzclose(_gzFile);
  116. _isDisposed = true;
  117. }
  118. }
  119. #endregion
  120. #region Basic reading and writing
  121. /// <summary>
  122. /// Attempts to read a number of bytes from the stream.
  123. /// </summary>
  124. /// <param name="buffer">The destination data buffer</param>
  125. /// <param name="offset">The index of the first destination byte in <c>buffer</c></param>
  126. /// <param name="count">The number of bytes requested</param>
  127. /// <returns>The number of bytes read</returns>
  128. /// <exception cref="ArgumentNullException">If <c>buffer</c> is null</exception>
  129. /// <exception cref="ArgumentOutOfRangeException">If <c>count</c> or <c>offset</c> are negative</exception>
  130. /// <exception cref="ArgumentException">If <c>offset</c> + <c>count</c> is &gt; buffer.Length</exception>
  131. /// <exception cref="NotSupportedException">If this stream is not readable.</exception>
  132. /// <exception cref="ObjectDisposedException">If this stream has been disposed.</exception>
  133. public override int Read(byte[] buffer, int offset, int count)
  134. {
  135. if (!CanRead) throw new NotSupportedException();
  136. if (buffer == null) throw new ArgumentNullException();
  137. if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
  138. if ((offset+count) > buffer.Length) throw new ArgumentException();
  139. if (_isDisposed) throw new ObjectDisposedException("GZipStream");
  140. GCHandle h = GCHandle.Alloc(buffer, GCHandleType.Pinned);
  141. int result;
  142. try
  143. {
  144. result = gzread(_gzFile, h.AddrOfPinnedObject().ToInt32() + offset, count);
  145. if (result < 0)
  146. throw new IOException();
  147. }
  148. finally
  149. {
  150. h.Free();
  151. }
  152. return result;
  153. }
  154. /// <summary>
  155. /// Attempts to read a single byte from the stream.
  156. /// </summary>
  157. /// <returns>The byte that was read, or -1 in case of error or End-Of-File</returns>
  158. public override int ReadByte()
  159. {
  160. if (!CanRead) throw new NotSupportedException();
  161. if (_isDisposed) throw new ObjectDisposedException("GZipStream");
  162. return gzgetc(_gzFile);
  163. }
  164. /// <summary>
  165. /// Writes a number of bytes to the stream
  166. /// </summary>
  167. /// <param name="buffer"></param>
  168. /// <param name="offset"></param>
  169. /// <param name="count"></param>
  170. /// <exception cref="ArgumentNullException">If <c>buffer</c> is null</exception>
  171. /// <exception cref="ArgumentOutOfRangeException">If <c>count</c> or <c>offset</c> are negative</exception>
  172. /// <exception cref="ArgumentException">If <c>offset</c> + <c>count</c> is &gt; buffer.Length</exception>
  173. /// <exception cref="NotSupportedException">If this stream is not writeable.</exception>
  174. /// <exception cref="ObjectDisposedException">If this stream has been disposed.</exception>
  175. public override void Write(byte[] buffer, int offset, int count)
  176. {
  177. if (!CanWrite) throw new NotSupportedException();
  178. if (buffer == null) throw new ArgumentNullException();
  179. if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
  180. if ((offset+count) > buffer.Length) throw new ArgumentException();
  181. if (_isDisposed) throw new ObjectDisposedException("GZipStream");
  182. GCHandle h = GCHandle.Alloc(buffer, GCHandleType.Pinned);
  183. try
  184. {
  185. int result = gzwrite(_gzFile, h.AddrOfPinnedObject().ToInt32() + offset, count);
  186. if (result < 0)
  187. throw new IOException();
  188. }
  189. finally
  190. {
  191. h.Free();
  192. }
  193. }
  194. /// <summary>
  195. /// Writes a single byte to the stream
  196. /// </summary>
  197. /// <param name="value">The byte to add to the stream.</param>
  198. /// <exception cref="NotSupportedException">If this stream is not writeable.</exception>
  199. /// <exception cref="ObjectDisposedException">If this stream has been disposed.</exception>
  200. public override void WriteByte(byte value)
  201. {
  202. if (!CanWrite) throw new NotSupportedException();
  203. if (_isDisposed) throw new ObjectDisposedException("GZipStream");
  204. int result = gzputc(_gzFile, (int)value);
  205. if (result < 0)
  206. throw new IOException();
  207. }
  208. #endregion
  209. #region Position & length stuff
  210. /// <summary>
  211. /// Not supported.
  212. /// </summary>
  213. /// <param name="value"></param>
  214. /// <exception cref="NotSupportedException">Always thrown</exception>
  215. public override void SetLength(long value)
  216. {
  217. throw new NotSupportedException();
  218. }
  219. /// <summary>
  220. /// Not suppported.
  221. /// </summary>
  222. /// <param name="offset"></param>
  223. /// <param name="origin"></param>
  224. /// <returns></returns>
  225. /// <exception cref="NotSupportedException">Always thrown</exception>
  226. public override long Seek(long offset, SeekOrigin origin)
  227. {
  228. throw new NotSupportedException();
  229. }
  230. /// <summary>
  231. /// Flushes the <c>GZipStream</c>.
  232. /// </summary>
  233. /// <remarks>In this implementation, this method does nothing. This is because excessive
  234. /// flushing may degrade the achievable compression rates.</remarks>
  235. public override void Flush()
  236. {
  237. // left empty on purpose
  238. }
  239. /// <summary>
  240. /// Gets/sets the current position in the <c>GZipStream</c>. Not suppported.
  241. /// </summary>
  242. /// <remarks>In this implementation this property is not supported</remarks>
  243. /// <exception cref="NotSupportedException">Always thrown</exception>
  244. public override long Position
  245. {
  246. get
  247. {
  248. throw new NotSupportedException();
  249. }
  250. set
  251. {
  252. throw new NotSupportedException();
  253. }
  254. }
  255. /// <summary>
  256. /// Gets the size of the stream. Not suppported.
  257. /// </summary>
  258. /// <remarks>In this implementation this property is not supported</remarks>
  259. /// <exception cref="NotSupportedException">Always thrown</exception>
  260. public override long Length
  261. {
  262. get
  263. {
  264. throw new NotSupportedException();
  265. }
  266. }
  267. #endregion
  268. }
  269. }