//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2020 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------
using System;
using System.IO;
namespace ET
{
public static partial class Utility
{
///
/// 压缩解压缩相关的实用函数。
///
public static partial class Zip
{
private static IZipHelper s_ZipHelper = null;
///
/// 设置压缩解压缩辅助器。
///
/// 要设置的压缩解压缩辅助器。
public static void SetZipHelper(IZipHelper zipHelper)
{
s_ZipHelper = zipHelper;
}
///
/// 压缩数据。
///
/// 要压缩的数据的二进制流。
/// 压缩后的数据的二进制流。
public static byte[] Compress(byte[] bytes)
{
if (bytes == null)
{
throw new System.Exception("Bytes is invalid.");
}
return Compress(bytes, 0, bytes.Length);
}
///
/// 压缩数据。
///
/// 要压缩的数据的二进制流。
/// 压缩后的数据的二进制流。
/// 是否压缩数据成功。
public static bool Compress(byte[] bytes, Stream compressedStream)
{
if (bytes == null)
{
throw new System.Exception("Bytes is invalid.");
}
return Compress(bytes, 0, bytes.Length, compressedStream);
}
///
/// 压缩数据。
///
/// 要压缩的数据的二进制流。
/// 要压缩的数据的二进制流的偏移。
/// 要压缩的数据的二进制流的长度。
/// 压缩后的数据的二进制流。
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;
}
}
}
///
/// 压缩数据。
///
/// 要压缩的数据的二进制流。
/// 要压缩的数据的二进制流的偏移。
/// 要压缩的数据的二进制流的长度。
/// 压缩后的数据的二进制流。
/// 是否压缩数据成功。
public static bool Compress(byte[] bytes, int offset, int length, Stream compressedStream)
{
if (s_ZipHelper == null)
{
throw new System.Exception("Zip helper is invalid.");
}
if (bytes == null)
{
throw new System.Exception("Bytes is invalid.");
}
if (offset < 0 || length <= 0 || offset + length > bytes.Length)
{
throw new System.Exception("Offset or length is invalid.");
}
if (compressedStream == null)
{
throw new System.Exception("Compressed stream is invalid.");
}
try
{
return s_ZipHelper.Compress(bytes, offset, length, compressedStream);
}
catch (Exception exception)
{
if (exception is System.Exception)
{
throw;
}
throw new System.Exception(Text.Format("Can not compress with exception '{0}'.", exception.ToString()), exception);
}
}
///
/// 压缩数据。
///
/// 要压缩的数据的二进制流。
/// 压缩后的数据的二进制流。
public static byte[] Compress(Stream stream)
{
using (MemoryStream compressedStream = new MemoryStream())
{
if (Compress(stream, compressedStream))
{
return compressedStream.ToArray();
}
else
{
return null;
}
}
}
///
/// 压缩数据。
///
/// 要压缩的数据的二进制流。
/// 压缩后的数据的二进制流。
/// 是否压缩数据成功。
public static bool Compress(Stream stream, Stream compressedStream)
{
if (s_ZipHelper == null)
{
throw new System.Exception("Zip helper is invalid.");
}
if (stream == null)
{
throw new System.Exception("Stream is invalid.");
}
if (compressedStream == null)
{
throw new System.Exception("Compressed stream is invalid.");
}
try
{
return s_ZipHelper.Compress(stream, compressedStream);
}
catch (Exception exception)
{
if (exception is System.Exception)
{
throw;
}
throw new System.Exception(Text.Format("Can not compress with exception '{0}'.", exception.ToString()), exception);
}
}
///
/// 解压缩数据。
///
/// 要解压缩的数据的二进制流。
/// 解压缩后的数据的二进制流。
public static byte[] Decompress(byte[] bytes)
{
if (bytes == null)
{
throw new System.Exception("Bytes is invalid.");
}
return Decompress(bytes, 0, bytes.Length);
}
///
/// 解压缩数据。
///
/// 要解压缩的数据的二进制流。
/// 解压缩后的数据的二进制流。
/// 是否解压缩数据成功。
public static bool Decompress(byte[] bytes, Stream decompressedStream)
{
if (bytes == null)
{
throw new System.Exception("Bytes is invalid.");
}
return Decompress(bytes, 0, bytes.Length, decompressedStream);
}
///
/// 解压缩数据。
///
/// 要解压缩的数据的二进制流。
/// 要解压缩的数据的二进制流的偏移。
/// 要解压缩的数据的二进制流的长度。
/// 解压缩后的数据的二进制流。
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;
}
}
}
///
/// 解压缩数据。
///
/// 要解压缩的数据的二进制流。
/// 要解压缩的数据的二进制流的偏移。
/// 要解压缩的数据的二进制流的长度。
/// 解压缩后的数据的二进制流。
/// 是否解压缩数据成功。
public static bool Decompress(byte[] bytes, int offset, int length, Stream decompressedStream)
{
if (s_ZipHelper == null)
{
throw new System.Exception("Zip helper is invalid.");
}
if (bytes == null)
{
throw new System.Exception("Bytes is invalid.");
}
if (offset < 0 || length <= 0 || offset + length > bytes.Length)
{
throw new System.Exception("Offset or length is invalid.");
}
if (decompressedStream == null)
{
throw new System.Exception("Decompressed stream is invalid.");
}
try
{
return s_ZipHelper.Decompress(bytes, offset, length, decompressedStream);
}
catch (Exception exception)
{
if (exception is System.Exception)
{
throw;
}
throw new System.Exception(Text.Format("Can not decompress with exception '{0}'.", exception.ToString()), exception);
}
}
///
/// 解压缩数据。
///
/// 要解压缩的数据的二进制流。
/// 是否解压缩数据成功。
public static byte[] Decompress(Stream stream)
{
using (MemoryStream decompressedStream = new MemoryStream())
{
if (Decompress(stream, decompressedStream))
{
return decompressedStream.ToArray();
}
else
{
return null;
}
}
}
///
/// 解压缩数据。
///
/// 要解压缩的数据的二进制流。
/// 解压缩后的数据的二进制流。
/// 是否解压缩数据成功。
public static bool Decompress(Stream stream, Stream decompressedStream)
{
if (s_ZipHelper == null)
{
throw new System.Exception("Zip helper is invalid.");
}
if (stream == null)
{
throw new System.Exception("Stream is invalid.");
}
if (decompressedStream == null)
{
throw new System.Exception("Decompressed stream is invalid.");
}
try
{
return s_ZipHelper.Decompress(stream, decompressedStream);
}
catch (Exception exception)
{
if (exception is System.Exception)
{
throw;
}
throw new System.Exception(Text.Format("Can not decompress with exception '{0}'.", exception.ToString()), exception);
}
}
}
}
}