123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- //------------------------------------------------------------
- // Game Framework
- // Copyright © 2013-2021 loyalsoft. All rights reserved.
- // Homepage: http://www.game7000.com/
- // Feedback: http://www.game7000.com/
- //------------------------------------------------------------
- using System;
- using System.IO;
- namespace GameFramework
- {
- public static partial class Utility
- {
- /// <summary>
- /// 压缩解压缩相关的实用函数。
- /// </summary>
- public static partial class Compression
- {
- private static ICompressionHelper s_CompressionHelper = null;
- /// <summary>
- /// 设置压缩解压缩辅助器。
- /// </summary>
- /// <param name="compressionHelper">要设置的压缩解压缩辅助器。</param>
- public static void SetCompressionHelper(ICompressionHelper compressionHelper)
- {
- s_CompressionHelper = compressionHelper;
- }
- /// <summary>
- /// 压缩数据。
- /// </summary>
- /// <param name="bytes">要压缩的数据的二进制流。</param>
- /// <returns>压缩后的数据的二进制流。</returns>
- public static byte[] Compress(byte[] bytes)
- {
- if (bytes == null)
- {
- throw new GameFrameworkException("Bytes is invalid.");
- }
- return Compress(bytes, 0, bytes.Length);
- }
- /// <summary>
- /// 压缩数据。
- /// </summary>
- /// <param name="bytes">要压缩的数据的二进制流。</param>
- /// <param name="compressedStream">压缩后的数据的二进制流。</param>
- /// <returns>是否压缩数据成功。</returns>
- public static bool Compress(byte[] bytes, Stream compressedStream)
- {
- if (bytes == null)
- {
- throw new GameFrameworkException("Bytes is invalid.");
- }
- return Compress(bytes, 0, bytes.Length, compressedStream);
- }
- /// <summary>
- /// 压缩数据。
- /// </summary>
- /// <param name="bytes">要压缩的数据的二进制流。</param>
- /// <param name="offset">要压缩的数据的二进制流的偏移。</param>
- /// <param name="length">要压缩的数据的二进制流的长度。</param>
- /// <returns>压缩后的数据的二进制流。</returns>
- public static byte[] Compress(byte[] bytes, int offset, int length)
- {
- using (MemoryStream compressedStream = new MemoryStream())
- {
- if (Compress(bytes, offset, length, compressedStream))
- {
- return compressedStream.ToArray();
- }
- else
- {
- return null;
- }
- }
- }
- /// <summary>
- /// 压缩数据。
- /// </summary>
- /// <param name="bytes">要压缩的数据的二进制流。</param>
- /// <param name="offset">要压缩的数据的二进制流的偏移。</param>
- /// <param name="length">要压缩的数据的二进制流的长度。</param>
- /// <param name="compressedStream">压缩后的数据的二进制流。</param>
- /// <returns>是否压缩数据成功。</returns>
- public static bool Compress(byte[] bytes, int offset, int length, Stream compressedStream)
- {
- if (s_CompressionHelper == null)
- {
- throw new GameFrameworkException("Compressed helper is invalid.");
- }
- if (bytes == null)
- {
- throw new GameFrameworkException("Bytes is invalid.");
- }
- if (offset < 0 || length < 0 || offset + length > bytes.Length)
- {
- throw new GameFrameworkException("Offset or length is invalid.");
- }
- if (compressedStream == null)
- {
- throw new GameFrameworkException("Compressed stream is invalid.");
- }
- try
- {
- return s_CompressionHelper.Compress(bytes, offset, length, compressedStream);
- }
- catch (Exception exception)
- {
- if (exception is GameFrameworkException)
- {
- throw;
- }
- throw new GameFrameworkException(Text.Format("Can not compress with exception '{0}'.", exception), exception);
- }
- }
- /// <summary>
- /// 压缩数据。
- /// </summary>
- /// <param name="stream">要压缩的数据的二进制流。</param>
- /// <returns>压缩后的数据的二进制流。</returns>
- public static byte[] Compress(Stream stream)
- {
- using (MemoryStream compressedStream = new MemoryStream())
- {
- if (Compress(stream, compressedStream))
- {
- return compressedStream.ToArray();
- }
- else
- {
- return null;
- }
- }
- }
- /// <summary>
- /// 压缩数据。
- /// </summary>
- /// <param name="stream">要压缩的数据的二进制流。</param>
- /// <param name="compressedStream">压缩后的数据的二进制流。</param>
- /// <returns>是否压缩数据成功。</returns>
- public static bool Compress(Stream stream, Stream compressedStream)
- {
- if (s_CompressionHelper == null)
- {
- throw new GameFrameworkException("Compressed helper is invalid.");
- }
- if (stream == null)
- {
- throw new GameFrameworkException("Stream is invalid.");
- }
- if (compressedStream == null)
- {
- throw new GameFrameworkException("Compressed stream is invalid.");
- }
- try
- {
- return s_CompressionHelper.Compress(stream, compressedStream);
- }
- catch (Exception exception)
- {
- if (exception is GameFrameworkException)
- {
- throw;
- }
- throw new GameFrameworkException(Text.Format("Can not compress with exception '{0}'.", exception), exception);
- }
- }
- /// <summary>
- /// 解压缩数据。
- /// </summary>
- /// <param name="bytes">要解压缩的数据的二进制流。</param>
- /// <returns>解压缩后的数据的二进制流。</returns>
- public static byte[] Decompress(byte[] bytes)
- {
- if (bytes == null)
- {
- throw new GameFrameworkException("Bytes is invalid.");
- }
- return Decompress(bytes, 0, bytes.Length);
- }
- /// <summary>
- /// 解压缩数据。
- /// </summary>
- /// <param name="bytes">要解压缩的数据的二进制流。</param>
- /// <param name="decompressedStream">解压缩后的数据的二进制流。</param>
- /// <returns>是否解压缩数据成功。</returns>
- public static bool Decompress(byte[] bytes, Stream decompressedStream)
- {
- if (bytes == null)
- {
- throw new GameFrameworkException("Bytes is invalid.");
- }
- return Decompress(bytes, 0, bytes.Length, decompressedStream);
- }
- /// <summary>
- /// 解压缩数据。
- /// </summary>
- /// <param name="bytes">要解压缩的数据的二进制流。</param>
- /// <param name="offset">要解压缩的数据的二进制流的偏移。</param>
- /// <param name="length">要解压缩的数据的二进制流的长度。</param>
- /// <returns>解压缩后的数据的二进制流。</returns>
- public static byte[] Decompress(byte[] bytes, int offset, int length)
- {
- using (MemoryStream decompressedStream = new MemoryStream())
- {
- if (Decompress(bytes, offset, length, decompressedStream))
- {
- return decompressedStream.ToArray();
- }
- else
- {
- return null;
- }
- }
- }
- /// <summary>
- /// 解压缩数据。
- /// </summary>
- /// <param name="bytes">要解压缩的数据的二进制流。</param>
- /// <param name="offset">要解压缩的数据的二进制流的偏移。</param>
- /// <param name="length">要解压缩的数据的二进制流的长度。</param>
- /// <param name="decompressedStream">解压缩后的数据的二进制流。</param>
- /// <returns>是否解压缩数据成功。</returns>
- public static bool Decompress(byte[] bytes, int offset, int length, Stream decompressedStream)
- {
- if (s_CompressionHelper == null)
- {
- throw new GameFrameworkException("Compressed helper is invalid.");
- }
- if (bytes == null)
- {
- throw new GameFrameworkException("Bytes is invalid.");
- }
- if (offset < 0 || length < 0 || offset + length > bytes.Length)
- {
- throw new GameFrameworkException("Offset or length is invalid.");
- }
- if (decompressedStream == null)
- {
- throw new GameFrameworkException("Decompressed stream is invalid.");
- }
- try
- {
- return s_CompressionHelper.Decompress(bytes, offset, length, decompressedStream);
- }
- catch (Exception exception)
- {
- if (exception is GameFrameworkException)
- {
- throw;
- }
- throw new GameFrameworkException(Text.Format("Can not decompress with exception '{0}'.", exception), exception);
- }
- }
- /// <summary>
- /// 解压缩数据。
- /// </summary>
- /// <param name="stream">要解压缩的数据的二进制流。</param>
- /// <returns>是否解压缩数据成功。</returns>
- public static byte[] Decompress(Stream stream)
- {
- using (MemoryStream decompressedStream = new MemoryStream())
- {
- if (Decompress(stream, decompressedStream))
- {
- return decompressedStream.ToArray();
- }
- else
- {
- return null;
- }
- }
- }
- /// <summary>
- /// 解压缩数据。
- /// </summary>
- /// <param name="stream">要解压缩的数据的二进制流。</param>
- /// <param name="decompressedStream">解压缩后的数据的二进制流。</param>
- /// <returns>是否解压缩数据成功。</returns>
- public static bool Decompress(Stream stream, Stream decompressedStream)
- {
- if (s_CompressionHelper == null)
- {
- throw new GameFrameworkException("Compressed helper is invalid.");
- }
- if (stream == null)
- {
- throw new GameFrameworkException("Stream is invalid.");
- }
- if (decompressedStream == null)
- {
- throw new GameFrameworkException("Decompressed stream is invalid.");
- }
- try
- {
- return s_CompressionHelper.Decompress(stream, decompressedStream);
- }
- catch (Exception exception)
- {
- if (exception is GameFrameworkException)
- {
- throw;
- }
- throw new GameFrameworkException(Text.Format("Can not decompress with exception '{0}'.", exception), exception);
- }
- }
- }
- }
- }
|