コード例 #1
0
        public static string OutDiff(string base_dir, string cur_dir, string package_out_dir, string ver)
        {
            string base_md5_file = Path.Combine(base_dir, Config.getMd5File());
            string cur_md5_file  = Path.Combine(cur_dir, Config.getMd5File());

            if (!File.Exists(base_md5_file))
            {
                FormUpdate.GetInstance().appendLog("base md5 not found, begin gen");
                FileStream   fs = File.Create(base_md5_file);
                StreamWriter sw = new StreamWriter(fs);
                GenResMd5(base_dir, sw, base_dir);
                sw.Close();
            }

            if (!File.Exists(cur_md5_file))
            {
                FormUpdate.GetInstance().appendLog("base md5 not found, begin gen");
                FileStream   fs = File.Create(cur_md5_file);
                StreamWriter sw = new StreamWriter(fs);
                GenResMd5(cur_md5_file, sw, cur_dir);
                sw.Close();
            }

            //读取文件
            Dictionary <String, String> base_md5_dic = new Dictionary <string, string>();
            Dictionary <String, String> cur_md5_dic  = new Dictionary <string, string>();


            string[] line = File.ReadAllLines(base_md5_file);
            foreach (string t in line)
            {
                string[] md5_file = t.Split(':');
                if (md5_file.Length != 2)
                {
                    FormUpdate.GetInstance().appendLog("base dir get file key error:" + t);
                    continue;
                }
                base_md5_dic.Add(md5_file[0], md5_file[1]);
            }

            line = File.ReadAllLines(cur_md5_file);
            foreach (string t in line)
            {
                string[] md5_file = t.Split(':');
                if (md5_file.Length != 2)
                {
                    FormUpdate.GetInstance().appendLog("cur dir get file key error:" + t);
                    continue;
                }
                cur_md5_dic.Add(md5_file[0], md5_file[1]);
            }
            Dictionary <String, String> diff_dic = new Dictionary <string, string>();

            //get diff
            foreach (string key in cur_md5_dic.Keys)
            {
                if (!base_md5_dic.ContainsKey(key))
                {
                    diff_dic.Add(key, cur_md5_dic[key]);
                }
                else if (base_md5_dic[key].CompareTo(cur_md5_dic[key]) != 0)
                {
                    diff_dic.Add(key, cur_md5_dic[key]);
                }
            }

            //copy to package out
            string tmpDir = Path.Combine(package_out_dir.Substring(0, package_out_dir.LastIndexOf(Path.DirectorySeparatorChar)), "temp");

            if (Directory.Exists(tmpDir))
            {
                Directory.Delete(tmpDir, true);
            }
            Directory.CreateDirectory(tmpDir);

            foreach (String key in diff_dic.Keys)
            {
                string dir = key.Substring(0, key.LastIndexOf(Path.DirectorySeparatorChar));
                dir = Path.Combine(tmpDir, dir);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                File.Copy(Path.Combine(cur_dir, key), Path.Combine(tmpDir, key));
            }

            string[]      dires       = Directory.GetDirectories(tmpDir);
            List <string> directories = new List <string>();

            directories.AddRange(dires);
            string zip_file_path = Path.Combine(package_out_dir, "update_" + ver + ".zip");


            using (ZipOutputStream ZipStream = new ZipOutputStream(System.IO.File.Create(zip_file_path)))
            {
                ZipStream.SetLevel(9);
                ZipEntryFactory factory = new ZipEntryFactory();
                while (directories.Count > 0)
                {
                    List <string> tmp = new List <string>();
                    foreach (string dir in directories)
                    {
                        string[] dirs = Directory.GetDirectories(dir);
                        if (dirs.Length > 0)
                        {
                            tmp.AddRange(dirs);
                        }
                    }
                    //add files to zip
                    foreach (string dir_tmp in directories)
                    {
                        string virtualDirectory = dir_tmp.Replace(tmpDir, string.Empty);
                        Log("add virtual dir:" + virtualDirectory);
                        ZipEntry zipEntry = factory.MakeDirectoryEntry(virtualDirectory);
                        zipEntry.DateTime = DateTime.Now;
                        ZipStream.PutNextEntry(zipEntry);
                        string[] files_tmp = Directory.GetFiles(dir_tmp);
                        foreach (string file_path in files_tmp)
                        {
                            string   entry_name = file_path.Replace(tmpDir, string.Empty);
                            ZipEntry entry      = factory.MakeFileEntry(entry_name);
                            entry.DateTime = DateTime.Now;
                            ZipStream.PutNextEntry(entry);
                            byte[] buffer = new byte[10240];
                            using (FileStream fs = System.IO.File.OpenRead(file_path))
                            {
                                int sourceBytes;
                                do
                                {
                                    sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                    ZipStream.Write(buffer, 0, sourceBytes);
                                } while (sourceBytes > 0);
                            }
                        }
                    }
                    directories = tmp;
                }
                ZipStream.Finish();
                ZipStream.Close();
            }
            //FastZip zip = new FastZip();
            //zip.CreateZip(zip_file_path, tmpDir, true, "");
            return(zip_file_path);
        }