/// <summary> /// 分析压缩数据 /// </summary> public void AnalyzeData() { if (FileInfo == null) { return; } LockControl(true); //锁定控件 ResetContent(IgnoreCompressResetContentType.OriginalSizeText); //重设置文本 var countList = new int[CompressGroupQty]; for (int i = 1; i <= countList.Length; i++) { countList[i - 1] = i; } var count = 0; var fileContent = FileInfo.FileContent; var task = new TaskScheduling <int, CompressItem>(countList, CompressThreadQty) { TaskFunc = size => { var item = ZlCompressHelper.GetCompressItem(fileContent, size); ControlInfo.SetScheduleAction(Interlocked.Increment(ref count) / (double)countList.Length * 100); //设置进度 return(item); }, ExceptionFunc = (size, ex) => { ConsoleWriteLine(string.Format("分析压缩数据出现错误!{0}", ex.Message));//输出错误 return(null); } }; var max = task.Execute().Where(w => w != null).OrderByDescending(b => b.CompressSize) .FirstOrDefault(); //得到压缩价值最大的项 if (max == null) { ConsoleWriteLine("没有查询到一个组合的压缩数据!全部出现异常!"); //输出错误 } else if (max.CompressSize < 1) { ConsoleWriteLine("没有有价值的压缩组合!请调大组合大小!"); //输出错误 } else { ConsoleWriteLine(string.Format("采用{0}字节组合[{1}],组合出现次数{2}次", max.Group.Bytes.Length, string.Join(",", max.Group.Bytes), max.Count)); OriginalSizeText = string.Format("压缩前:{0}", Tool.ToDataString(fileContent.Length)); CompressSizeText = string.Format("压缩后:{0}", Tool.ToDataString(fileContent.Length - max.CompressSize)); CompressionRatioText = string.Format("压缩率:{0:0.00}%", (((double)(fileContent.Length - max.CompressSize) / fileContent.Length) * 100)); } LockControl(false);//解锁控件 }
/// <summary> /// 压缩方法 /// </summary> private void Compress() { if (FileInfo == null) { return; } LockControl(true); //锁定控件 ControlInfo.SetSavePathAction(string.Empty); //清除下保存文本 ResetContent(IgnoreCompressResetContentType.OriginalSizeText); //重设置文本 var countList = new int[CompressGroupQty]; for (int i = 1; i <= countList.Length; i++) { countList[i - 1] = i; } double sumCount = countList.Length + 1; //总数 var count = 0; //执行到数 var fileContent = FileInfo.FileContent; var task = new TaskScheduling <int, CompressItem>(countList, CompressThreadQty) { TaskFunc = size => { var item = ZlCompressHelper.GetCompressItem(fileContent, size); ControlInfo.SetScheduleAction(Interlocked.Increment(ref count) / sumCount * 100); //设置进度 return(item); }, ExceptionFunc = (size, ex) => { ConsoleWriteLine(string.Format("分析压缩数据出现错误!{0}", ex.Message));//输出错误 return(null); } }; var max = task.Execute().Where(w => w != null).OrderByDescending(b => b.CompressSize) .FirstOrDefault(); //得到压缩价值最大的项 if (max == null) { ConsoleWriteLine("没有查询到一个组合的压缩数据!全部出现异常!"); //输出错误 } else if (max.CompressSize < 1) { ConsoleWriteLine("没有有价值的压缩组合!请调大组合大小!"); //输出错误 } else { ConsoleWriteLine(string.Format("采用{0}字节组合[{1}]", max.Group.Bytes.Length, string.Join(",", max.Group.Bytes))); OriginalSizeText = string.Format("压缩前:{0}", Tool.ToDataString(fileContent.Length)); CompressSizeText = string.Format("压缩后:{0}", Tool.ToDataString(fileContent.Length - max.CompressSize)); CompressionRatioText = string.Format("压缩率:{0:0.00}%", (((double)(fileContent.Length - max.CompressSize) / fileContent.Length) * 100)); var compressBytes = ZlCompressHelper.Compress(fileContent, max); //压缩处理 ControlInfo.SetScheduleAction(100); //设置执行完成 var path = ControlInfo.GetSavePathFunc(new SaveFileInfo { DefaultExt = ".zl", FileName = string.Format("{0}.zl", FileInfo.Info.Name), Title = "请设置保存的压缩文件", }); if (!string.IsNullOrEmpty(path)) { File.WriteAllBytes(path, compressBytes); MessageBox.Show("保存成功!"); } } ControlInfo.SetScheduleAction(100); //设置执行完成 LockControl(false); //解锁控件 }