EchoTex_Payroll/Ease.Core/Utility/ZipHelper.cs

406 lines
13 KiB
C#
Raw Normal View History

2024-10-14 10:01:49 +06:00

using System;
using System.IO;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
using Ease.Core.Model;
using System.Collections.Generic;
namespace Ease.Core.Utility
{
public class ZipHelper
{
#region Constructor
private ZipHelper()
{
}
#endregion
#region Compress functions
/// <summary>
/// Compress string.
/// </summary>
/// <param name="text">Text to be compressed.</param>
/// <returns>Return compressed string. </returns>
public static string Compress(string text)
{
byte[] compressed;
try
{
using (MemoryStream ms = new MemoryStream())
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}
ms.Position = 0;
byte[] zipBuffer = new byte[ms.Length];
ms.Read(zipBuffer, 0, zipBuffer.Length);
compressed = new byte[zipBuffer.Length + 4];
Buffer.BlockCopy(zipBuffer, 0, compressed, 4, zipBuffer.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, compressed, 0, 4);
}
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
return Convert.ToBase64String(compressed);
}
/// <summary>
/// Compress a DataSet.
/// </summary>
/// <param name="data">DataSet to be compressed.</param>
/// <returns>Return comressed string.</returns>
public static string Compress(DataSet data)
{
byte[] compressed;
try
{
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
data.WriteXml(zip, XmlWriteMode.WriteSchema);
}
compressed = ms.ToArray();
}
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
return Convert.ToBase64String(compressed);
}
/// <summary>
/// Compress a DataTable.
/// </summary>
/// <param name="data">DatTable to be compressed.</param>
/// <returns>Return comressed string.</returns>
public static string Compress(DataTable data)
{
byte[] compressed;
try
{
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
data.WriteXml(zip, XmlWriteMode.WriteSchema);
}
compressed = ms.ToArray();
}
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
return Convert.ToBase64String(compressed);
}
/// <summary>
/// Compress instance of an object.
/// </summary>
/// <typeparam name="T">Type of object to be compressed.</typeparam>
/// <param name="ot">Instatnce of T type object.</param>
/// <returns>Return compressed string.</returns>
public static string Compress<T>(T ot)
where T : ObjectTemplate
{
byte[] compressed;
try
{
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(zip, ot);
}
compressed = ms.ToArray();
}
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
return Convert.ToBase64String(compressed);
}
/// <summary>
/// Compress instance of an object of type T.
/// </summary>
/// <typeparam name="T">>Type of object</typeparam>
/// <param name="ost">Instatnce of T type object collection.</param>
/// <returns>Return compressed string.</returns>
public static string Compress<T>(List<T> ost)
where T : ObjectTemplate
{
byte[] compressed;
try
{
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(zip, ost);
}
compressed = ms.ToArray();
}
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
return Convert.ToBase64String(compressed);
}
/// <summary>
/// Compress a file
/// </summary>
/// <param name="srcFileName">Name of the file to be compressed.</param>
/// <param name="fileName">Name of the compressed file to be saved.</param>
public static void Compress(string srcFileName, string fileName)
{
try
{
byte[] buffer;
using (FileStream fsSrc = new FileStream(srcFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
buffer = new byte[fsSrc.Length];
fsSrc.Read(buffer, 0, buffer.Length);
}
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (GZipStream zip = new GZipStream(fs, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}
}
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
}
#endregion
#region Decompress functions
/// <summary>
/// Convert from compressed string into actual string.
/// </summary>
/// <param name="text">Compressed string to be decompressed.</param>
/// <returns>Return actual string after decompression. </returns>
public static string Decompress(string text)
{
byte[] buffer;
try
{
using (MemoryStream ms = new MemoryStream())
{
byte[] data = Convert.FromBase64String(text);
int length = BitConverter.ToInt32(data, 0);
ms.Write(data, 4, data.Length - 4);
buffer = new byte[length];
ms.Position = 0;
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
{
zip.Read(buffer, 0, buffer.Length);
}
}
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
return Encoding.UTF8.GetString(buffer);
}
/// <summary>
/// Reconstruct actual DataSet.
/// </summary>
/// <param name="text">Compressed string to be decompressed.</param>
/// <returns>Return actual DataSet.</returns>
public static DataSet DecompressDataSet(string text)
{
DataSet ds = new DataSet();
try
{
byte[] data = Convert.FromBase64String(text);
using (MemoryStream ms = new MemoryStream(data))
{
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
{
ds.ReadXml(zip, XmlReadMode.ReadSchema);
}
}
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
return ds;
}
/// <summary>
/// Reconstruct actual DataTable.
/// </summary>
/// <param name="text">Compressed string to be decompressed.</param>
/// <returns>Return actual DataTable.</returns>
public static DataTable DecompressDataTable(string text)
{
DataTable dt = new DataTable();
try
{
byte[] data = Convert.FromBase64String(text);
using (MemoryStream ms = new MemoryStream(data))
{
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
{
dt.ReadXml(zip);
}
}
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
return dt;
}
/// <summary>
/// Reconstruct the actual object.
/// </summary>
/// <typeparam name="T">Type of object to be decompressed.</typeparam>
/// <param name="text">Compressed string to be decompressed.</param>
/// <returns>Return ObjectTemplate of type T</returns>
public static T DecompressObject<T>(string text)
where T : ObjectTemplate
{
T instance;
try
{
byte[] data = Convert.FromBase64String(text);
using (MemoryStream ms = new MemoryStream(data))
{
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
{
BinaryFormatter bf = new BinaryFormatter();
instance = (T)bf.Deserialize(zip);
}
}
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
return instance;
}
/// <summary>
/// Reconstruct the actual objects.
/// </summary>
/// <typeparam name="T">Type of object to be decompressed.</typeparam>
/// <param name="text">Compressed string to be decompressed.</param>
/// <returns>Return collection ObjectTemplate of type T</returns>
public static List<T> DecompressObjects<T>(string text)
where T : ObjectTemplate
{
List<T> instance;
try
{
byte[] data = Convert.FromBase64String(text);
using (MemoryStream ms = new MemoryStream(data))
{
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
{
BinaryFormatter bf = new BinaryFormatter();
instance = (List<T>)bf.Deserialize(zip);
}
}
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
return instance;
}
/// <summary>
/// Decompress source file.
/// </summary>
/// <param name="srcFileName">Name of the compressed file.</param>
/// <param name="fileName">Name of the decompressed file to be saved.</param>
public static void Decompress(string srcFileName, string fileName)
{
try
{
int length = 0;
const int bz = 4096;
DateTime ct = DateTime.Now;
byte[] buffer = new byte[bz];
FileStream fsi = new FileStream(srcFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
FileStream fso = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
using (GZipStream zip = new GZipStream(fsi, CompressionMode.Decompress, true))
{
while (true)
{
length = zip.Read(buffer, 0, bz);
if (length != 0)
fso.Write(buffer, 0, length);
if (length != bz)
break;
}
}
if (fso != null)
{
fso.Close();
fso.Dispose();
fso = null;
}
if (fsi != null)
{
fsi.Close();
fsi.Dispose();
fsi = null;
}
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
}
#endregion
}
}