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 /// /// Compress string. /// /// Text to be compressed. /// Return compressed string. 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); } /// /// Compress a DataSet. /// /// DataSet to be compressed. /// Return comressed string. 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); } /// /// Compress a DataTable. /// /// DatTable to be compressed. /// Return comressed string. 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); } /// /// Compress instance of an object. /// /// Type of object to be compressed. /// Instatnce of T type object. /// Return compressed string. public static string Compress(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); } /// /// Compress instance of an object of type T. /// /// >Type of object /// Instatnce of T type object collection. /// Return compressed string. public static string Compress(List 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); } /// /// Compress a file /// /// Name of the file to be compressed. /// Name of the compressed file to be saved. 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 /// /// Convert from compressed string into actual string. /// /// Compressed string to be decompressed. /// Return actual string after decompression. 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); } /// /// Reconstruct actual DataSet. /// /// Compressed string to be decompressed. /// Return actual DataSet. 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; } /// /// Reconstruct actual DataTable. /// /// Compressed string to be decompressed. /// Return actual DataTable. 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; } /// /// Reconstruct the actual object. /// /// Type of object to be decompressed. /// Compressed string to be decompressed. /// Return ObjectTemplate of type T public static T DecompressObject(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; } /// /// Reconstruct the actual objects. /// /// Type of object to be decompressed. /// Compressed string to be decompressed. /// Return collection ObjectTemplate of type T public static List DecompressObjects(string text) where T : ObjectTemplate { List 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)bf.Deserialize(zip); } } } catch (Exception e) { throw new Exception(e.Message, e); } return instance; } /// /// Decompress source file. /// /// Name of the compressed file. /// Name of the decompressed file to be saved. 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 } }