using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
using EasyDevCore.Common.Security;
namespace EasyDevCore.Common
{
///
///
///
public enum CompressMethod
{
///
/// The deflate
///
Deflate,
///
/// The G zip
///
GZip
}
///
///
///
public class CompressHelper
{
///
/// Compresses the data.
///
/// The data.
/// The method.
///
public static byte[] CompressData(byte[] data, CompressMethod method = CompressMethod.Deflate)
{
using (var inputStream = new MemoryStream(data))
using (var compressStream = new MemoryStream())
{
using (var compressor = (method == CompressMethod.Deflate ? (Stream)(new DeflateStream(compressStream, CompressionMode.Compress)) : (Stream)(new GZipStream(compressStream, CompressionMode.Compress))))
{
inputStream.CopyTo(compressor);
}
return compressStream.ToArray();
}
}
///
/// Compresses the data.
///
/// The data.
/// The password to encrypt data.
/// The algorithm.
/// The method.
///
public static byte[] CompressData(byte[] data, string password, SymmetricAlgorithmMethod algorithm = SymmetricAlgorithmMethod.DESCryptoServiceProvider, CompressMethod method = CompressMethod.Deflate)
{
return EncryptionHelper.EncryptData(password, algorithm, CompressData(data, method));
}
///
/// Decompress data
///
/// The data.
/// The method.
///
public static byte[] DecompressData(byte[] data, CompressMethod method = CompressMethod.Deflate)
{
using (var inputStream = new MemoryStream(data))
using (var decompressStream = new MemoryStream())
{
using (var decompressor = (method == CompressMethod.Deflate ? (Stream)(new DeflateStream(inputStream, CompressionMode.Decompress)) : (Stream)(new GZipStream(inputStream, CompressionMode.Decompress))))
{
decompressor.CopyTo(decompressStream);
}
return decompressStream.ToArray();
}
}
///
/// Des the compress data.
///
/// The data.
/// The password to decrypt data.
/// The algorithm.
/// The method.
///
public static byte[] DecompressData(byte[] data, string password, SymmetricAlgorithmMethod algorithm = SymmetricAlgorithmMethod.DESCryptoServiceProvider, CompressMethod method = CompressMethod.Deflate)
{
return DecompressData(EncryptionHelper.DecryptData(password, algorithm, data), method);
}
///
/// Compresses the string.
///
/// The value.
/// The method.
/// The encoding.
///
public static string CompressString(string value, Encoding encoding, CompressMethod method = CompressMethod.Deflate)
{
return Encoding.GetEncoding(850).GetString(CompressData(encoding.GetBytes(value), method));
}
///
/// Compresses the string.
///
/// The value.
/// The password to encrypt data.
/// The encoding.
/// The method.
///
public static string CompressString(string value, string password, Encoding encoding, CompressMethod method = CompressMethod.Deflate)
{
return Encoding.GetEncoding(850).GetString(CompressData(encoding.GetBytes(value), password: password, method: method));
}
///
/// Compresses the string.
///
/// The value.
/// The method.
///
public static string CompressString(string value, CompressMethod method = CompressMethod.Deflate)
{
return Encoding.GetEncoding(850).GetString(CompressData(ConvertHelper.ToByteArray(value), method));
}
///
/// Compresses the string.
///
/// The value.
/// The password to encrypt.
/// The method.
///
public static string CompressString(string value, string password, CompressMethod method = CompressMethod.Deflate)
{
return Encoding.GetEncoding(850).GetString(CompressData(ConvertHelper.ToByteArray(value), password: password, method: method));
}
///
/// Decompresses the string.
///
/// The compress value.
/// The method.
/// The encoding.
///
public static string DecompressString(string compressValue, Encoding encoding, CompressMethod method = CompressMethod.Deflate)
{
return encoding.GetString(DecompressData(Encoding.GetEncoding(850).GetBytes(compressValue), method));
}
///
/// Decompresses the string.
///
/// The compress value.
/// The password.
/// The encoding.
/// The method.
///
public static string DecompressString(string compressValue, string password, Encoding encoding, CompressMethod method = CompressMethod.Deflate)
{
return encoding.GetString(DecompressData(Encoding.GetEncoding(850).GetBytes(compressValue), password: password, method: method));
}
///
/// Decompresses the string.
///
/// The value.
/// The method.
///
public static string DecompressString(string value, CompressMethod method = CompressMethod.Deflate)
{
return ConvertHelper.ToString(DecompressData(Encoding.GetEncoding(850).GetBytes(value), method));
}
///
/// Decompresses the string.
///
/// The value.
/// The password.
/// The method.
///
public static string DecompressString(string value, string password, CompressMethod method = CompressMethod.Deflate)
{
return ConvertHelper.ToString(DecompressData(Encoding.GetEncoding(850).GetBytes(value), password: password, method: method));
}
}
}