using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; //---------------------------------------------------------------- //Module Name: $safeprojectname$ //Purpose: //CopyRight: Muchinfo //History: //---------------------------------------------------------------- //DateTime 2015/12/29 16:39:22 //Author //Description Create //---------------------------------------------------------------- using System.Xml.Serialization; namespace Muchinfo.Client.Update.UpdateListFactory { /// /// Zip压缩 /// public class ZipCompessHelper { /// 对目标文件夹进行压缩,将压缩结果保存为指定文件 /// /// 目标文件夹 /// 压缩文件 public static void Compress(string dirPath, string fileName) { ArrayList list = new ArrayList(); foreach (string f in Directory.GetFiles(dirPath)) { byte[] destBuffer = File.ReadAllBytes(f); SerializeFileInfo sfi = new SerializeFileInfo(f, destBuffer); list.Add(sfi); } IFormatter formatter = new BinaryFormatter(); using (Stream s = new MemoryStream()) { formatter.Serialize(s, list); s.Position = 0; CreateCompressFile(s, fileName); } } /// /// 对目标压缩文件解压缩,将内容解压缩到指定文件夹 /// /// 压缩文件 /// 解压缩目录 public static void DeCompress(string fileName, string dirPath) { using (Stream source = File.OpenRead(fileName)) { using (Stream destination = new MemoryStream()) { using (GZipStream input = new GZipStream(source, CompressionMode.Decompress, true)) { byte[] bytes = new byte[4096]; int n; while ((n = input.Read(bytes, 0, bytes.Length)) != 0) { destination.Write(bytes, 0, n); } } destination.Flush(); destination.Position = 0; DeSerializeFiles(destination, dirPath); } } } private static void DeSerializeFiles(Stream s, string dirPath) { BinaryFormatter b = new BinaryFormatter(); ArrayList list = (ArrayList)b.Deserialize(s); foreach (SerializeFileInfo f in list) { string newName = dirPath + Path.GetFileName(f.FileName); using (FileStream fs = new FileStream(newName, FileMode.Create, FileAccess.Write)) { fs.Write(f.FileBuffer, 0, f.FileBuffer.Length); fs.Close(); } } } public static void CreateCompressFile(Stream source, string destinationName) { using (Stream destination = new FileStream(destinationName, FileMode.Create, FileAccess.Write)) { using (GZipStream output = new GZipStream(destination, CompressionMode.Compress)) { byte[] bytes = new byte[4096]; int n; while ((n = source.Read(bytes, 0, bytes.Length)) != 0) { output.Write(bytes, 0, n); } } } } /// /// 生成更新文件的XML /// /// 更新配置 /// 保存路径 public static void SaveUpdateXml(UpdateInfo updateInfo,string filePath) { var xs = new XmlSerializer(updateInfo.GetType()); using (var stream = new FileStream(filePath,FileMode.Create)) { xs.Serialize(stream, updateInfo); stream.Close(); } } } [Serializable] public class SerializeFileInfo { public SerializeFileInfo(string name, byte[] buffer) { fileName = name; fileBuffer = buffer; } string fileName; public string FileName { get { return fileName; } } byte[] fileBuffer; public byte[] FileBuffer { get { return fileBuffer; } } } }