| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- 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
- {
- /// <summary>
- /// Zip压缩
- /// </summary>
- public class ZipCompessHelper
- {
- /// 对目标文件夹进行压缩,将压缩结果保存为指定文件
- /// </summary>
- /// <param name="dirPath">目标文件夹</param>
- /// <param name="fileName">压缩文件</param>
- 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);
- }
- }
- /// <summary>
- /// 对目标压缩文件解压缩,将内容解压缩到指定文件夹
- /// </summary>
- /// <param name="fileName">压缩文件</param>
- /// <param name="dirPath">解压缩目录</param>
- 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);
- }
- }
- }
- }
- /// <summary>
- /// 生成更新文件的XML
- /// </summary>
- /// <param name="updateInfo">更新配置</param>
- /// <param name="filePath">保存路径</param>
- 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;
- }
- }
- }
- }
|