C#.NET C/S结构版本自动升级解决方案之升级包实现
作者:C/S框架网  发布日期:2011-09-11 00:21:10
  C#.NET C/S结构版本自动升级解决方案之升级包实现


升级包组成:

贴图图片

C# Code:


/// <summary>
/// 升级包类
/// </summary>
[Serializable]
public class PackageFile
{
   private string _PackingNo;
   private string _Remark;
   private List<PackageFileItem> _Files;
   
   public PackageFile(string packingNo)
   {
      _PackingNo = packingNo;
      _Files = new List<PackageFileItem>();
   }
   
   /// <summary>
   /// 文件包的名称
   /// </summary>
   public string PackingNo
   {
      get { return _PackingNo; }
      set { _PackingNo = value; }
   }
   
   /// <summary>
   /// 备注
   /// </summary>
   public string Remark
   {
      get { return _Remark; }
      set { _Remark = value; }
   }
   
   
   /// <summary>
   /// 文件包的所有文件列表
   /// </summary>
   public List<PackageFileItem> Files
   {
      get { return _Files; }
      set { _Files = value; }
   }
   
   /// <summary>
   /// 检查文件包是否存在文件
   /// </summary>
   /// <param name="location">文件的地址</param>
   /// <returns></returns>
   public bool ExistsLocation(string location)
   {
      var file = from a in _Files where a.FileLocation == location select a;
      return file.Count() > 0;
   }
   
   /// <summary>
   /// 将文件打包
   /// </summary>
   /// <param name="packingName">文件包名称</param>
   /// <param name="zip">是否压缩</param>
   /// <param name="destinationFile">文件地址</param>
   /// <returns></returns>
   public bool Packing(string packingName, bool zip, string destinationFile)
   {
      byte[] bs = ZipTools.CompressionObject(this);
      FileStream fs = File.OpenWrite(destinationFile);
      fs.Write(bs, 0, bs.Length);
      fs.Flush();
      fs.Close();
      return true;
   }
   
   /// <summary>
   /// 加载压缩包
   /// </summary>
   /// <param name="packageFile">压缩文件地址</param>
   /// <returns></returns>
   public static PackageFile LoadPackage(string packageFile)
   {
      try
      {
         if (Path.GetExtension(packageFile).ToUpper() == ".PKG")
         {
            FileStream fs = File.OpenRead(packageFile);
            byte[] buffer = new byte[fs.Length];
            int count = fs.Read(buffer, 0, buffer.Length);
            fs.Close();
            
            return (PackageFile)ZipTools.DecompressionObject(buffer);
         }
         else
         return null;
      }
      catch
      {
         return null;
      }
   }
   
   /// <summary>
   /// 加载压缩包
   /// </summary>
   /// <param name="ms">内存流</param>
   /// <returns></returns>
   public static PackageFile LoadPackage(MemoryStream ms)
   {
      return (PackageFile)ZipTools.DecompressionObject(ms.ToArray());
   }
   
   /// <summary>
   /// 加入文件
   /// </summary>
   /// <param name="files"></param>
   public void AddPackingFile(string[] files, string location, string folderName)
   {
      string fileLocation;
      
      foreach (string f in files)
      {
         PackageFileItem item = new PackageFileItem();
         
         FileStream fs = File.OpenRead(f);
         byte[] buffer = new byte[fs.Length];
         int count = fs.Read(buffer, 0, buffer.Length);
         fs.Close();
         
         fileLocation = location Path.Combine(folderName, Path.GetFileName(f));
         
         if (false == this.ExistsLocation(location))
         {
            item.FileLocation = fileLocation;
            item.FileBody = buffer;
            item.CreateDate = File.GetCreationTime(f);//.ToString("yyyy-MM-dd hh:mm:ss");
            item.FileName = Path.GetFileName(f);
            item.FileSize = buffer.Length;
            this.Files.Add(item);
         }
      }
   }
   
}

//来源:C/S框架网(www.csframework.com) QQ:1980854898


 


压缩与解压缩:

 使用封装工具压缩文件,一个文件就是一个升级包,每个升级包有唯一的版本号用于升级时对比。

 当用户升级版本时从服务器下载升级包到本地,由PackageExtract类负责解压缩文件,将解压后的文件复制到目的地。

PackageExtract类:


贴图图片


由Extract方法解压缩构造器传入的升级包对象。

C# Code:

/// <summary>
/// 解压缩文件类
/// </summary>
public class PackageExtract
{
   private PackageFile _pkg; //文件包
   
   private int _ExtractSuccess = 0;
   private int _ExtractFailed = 0;
   
   /// <summary>
   /// 解压成功的文件数
   /// </summary>
   public int ExtractSuccess
   {
      get { return _ExtractSuccess; }
   }
   
   /// <summary>
   /// 解压失败的文件数
   /// </summary>
   public int ExtractFailed
   {
      get { return _ExtractFailed; }
   }
   
   public PackageExtract(PackageFile pkg)
   {
      _pkg = pkg;
   }
   
   /// <summary>
   /// 解压缩文件
   /// </summary>
   /// <param name="extractedFiles">返回已解压缩的文件清单</param>
   /// <param name="extractFolder">解压文件存放目录</param>
   /// <returns></returns>
   public int Extract(List<PackageFileItem> extractedFiles, string extractFolder)
   {
      string dest;//输出目录
      
      foreach (PackageFileItem file in _pkg.Files)
      {
         try
         {
            //如指定输出目录,直接解压强文件到此目录
            if (extractFolder != "")
            {
               int i = file.FileLocation.IndexOf("\\") 1;
               dest = Path.Combine(extractFolder, file.FileLocation.Substring(i, file.FileLocation.Length - i));
            }
            else//替换目录标识符[APP]
            dest = RootNameConverter.ToAbsoluteFolderName(file.FileLocation);
            
            //升级主程序,CSFrameworkUpgraderClient.exe, 必须改为临时文件名
            if (file.FileName.ToUpper() == UpgraderConfig.UpgraderProgramFile.ToUpper())
            {
               dest = Path.GetDirectoryName(dest) "\\" UpgraderConfig.UpgraderTempProgramFile;
            }
            
            string dir = Path.GetDirectoryName(dest);
            if (false == Directory.Exists(dir)) Directory.CreateDirectory(dir); //创建目录
            
            //删除旧文件
            if (File.Exists(dest)) File.Delete(dest);
            
            //写入文件
            FileStream fs = File.OpenWrite(dest);
            fs.Write(file.FileBody, 0, file.FileBody.Length);
            fs.Flush();
            fs.Close();
            
            extractedFiles.Add(file);
            
            _ExtractSuccess ;
         }
         catch
         {
            _ExtractFailed ;
         }
      }
      
      return _pkg.Files.Count;
   }
}


//来源:C/S框架网(www.csframework.com) QQ:1980854898




 

上一篇 下一篇