123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- using System;
- using System.IO;
- using System.Runtime.InteropServices;
- namespace DotZLib
- {
-
-
-
- public class GZipStream : Stream, IDisposable
- {
- #region Dll Imports
- [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)]
- private static extern IntPtr gzopen(string name, string mode);
- [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
- private static extern int gzclose(IntPtr gzFile);
- [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
- private static extern int gzwrite(IntPtr gzFile, int data, int length);
- [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
- private static extern int gzread(IntPtr gzFile, int data, int length);
- [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
- private static extern int gzgetc(IntPtr gzFile);
- [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
- private static extern int gzputc(IntPtr gzFile, int c);
- #endregion
- #region Private data
- private IntPtr _gzFile;
- private bool _isDisposed = false;
- private bool _isWriting;
- #endregion
- #region Constructors
-
-
-
-
-
-
- public GZipStream(string fileName, CompressLevel level)
- {
- _isWriting = true;
- _gzFile = gzopen(fileName, String.Format("wb{0}", (int)level));
- if (_gzFile == IntPtr.Zero)
- throw new ZLibException(-1, "Could not open " + fileName);
- }
-
-
-
-
-
- public GZipStream(string fileName)
- {
- _isWriting = false;
- _gzFile = gzopen(fileName, "rb");
- if (_gzFile == IntPtr.Zero)
- throw new ZLibException(-1, "Could not open " + fileName);
- }
- #endregion
- #region Access properties
-
-
-
- public override bool CanRead
- {
- get
- {
- return !_isWriting;
- }
- }
-
-
-
- public override bool CanSeek
- {
- get
- {
- return false;
- }
- }
-
-
-
- public override bool CanWrite
- {
- get
- {
- return _isWriting;
- }
- }
- #endregion
- #region Destructor & IDispose stuff
-
-
-
- ~GZipStream()
- {
- cleanUp(false);
- }
-
-
-
- public void Dispose()
- {
- cleanUp(true);
- }
-
- private void cleanUp(bool isDisposing)
- {
- if (!_isDisposed)
- {
- gzclose(_gzFile);
- _isDisposed = true;
- }
- }
- #endregion
- #region Basic reading and writing
-
-
-
-
-
-
-
-
-
-
-
-
- public override int Read(byte[] buffer, int offset, int count)
- {
- if (!CanRead) throw new NotSupportedException();
- if (buffer == null) throw new ArgumentNullException();
- if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
- if ((offset+count) > buffer.Length) throw new ArgumentException();
- if (_isDisposed) throw new ObjectDisposedException("GZipStream");
- GCHandle h = GCHandle.Alloc(buffer, GCHandleType.Pinned);
- int result;
- try
- {
- result = gzread(_gzFile, h.AddrOfPinnedObject().ToInt32() + offset, count);
- if (result < 0)
- throw new IOException();
- }
- finally
- {
- h.Free();
- }
- return result;
- }
-
-
-
-
- public override int ReadByte()
- {
- if (!CanRead) throw new NotSupportedException();
- if (_isDisposed) throw new ObjectDisposedException("GZipStream");
- return gzgetc(_gzFile);
- }
-
-
-
-
-
-
-
-
-
-
-
- public override void Write(byte[] buffer, int offset, int count)
- {
- if (!CanWrite) throw new NotSupportedException();
- if (buffer == null) throw new ArgumentNullException();
- if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
- if ((offset+count) > buffer.Length) throw new ArgumentException();
- if (_isDisposed) throw new ObjectDisposedException("GZipStream");
- GCHandle h = GCHandle.Alloc(buffer, GCHandleType.Pinned);
- try
- {
- int result = gzwrite(_gzFile, h.AddrOfPinnedObject().ToInt32() + offset, count);
- if (result < 0)
- throw new IOException();
- }
- finally
- {
- h.Free();
- }
- }
-
-
-
-
-
-
- public override void WriteByte(byte value)
- {
- if (!CanWrite) throw new NotSupportedException();
- if (_isDisposed) throw new ObjectDisposedException("GZipStream");
- int result = gzputc(_gzFile, (int)value);
- if (result < 0)
- throw new IOException();
- }
- #endregion
- #region Position & length stuff
-
-
-
-
-
- public override void SetLength(long value)
- {
- throw new NotSupportedException();
- }
-
-
-
-
-
-
-
- public override long Seek(long offset, SeekOrigin origin)
- {
- throw new NotSupportedException();
- }
-
-
-
-
-
- public override void Flush()
- {
-
- }
-
-
-
-
-
- public override long Position
- {
- get
- {
- throw new NotSupportedException();
- }
- set
- {
- throw new NotSupportedException();
- }
- }
-
-
-
-
-
- public override long Length
- {
- get
- {
- throw new NotSupportedException();
- }
- }
- #endregion
- }
- }
|