/// <summary> /// 解压数据 /// </summary> private void Decompressuin() { if (FileInfo == null) { return; } LockControl(true); ControlInfo.SetSavePathAction(string.Empty);//清除下保存文本 ConsoleWriteLine("开始解压"); ResetContent(IgnoreDecompressionResetContentType.OriginalSizeText | IgnoreDecompressionResetContentType.CompressSizeText | IgnoreDecompressionResetContentType.CompressionRatioText); //重设置文本 ControlInfo.SetScheduleAction(0); //设置解压开始 var decompression = ZlCompressHelper.Decompression(FileInfo.FileContent); //解压 ControlInfo.SetScheduleAction(100); //设置解压完成 ConsoleWriteLine("解压完成"); var path = ControlInfo.GetSavePathFunc(new SaveFileInfo { FileName = string.Format("{0}", Path.GetFileNameWithoutExtension(FileInfo.Info.FullName)), Title = "请设置保存的解压文件", }); if (!string.IsNullOrEmpty(path)) { File.WriteAllBytes(path, decompression); MessageBox.Show("保存成功!"); } LockControl(false); }
/// <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);//解锁控件 }
public override void Show(FileInfoDto info) { FileInfo = info; ResetContent(); //重设置文本 ControlInfo.SetSavePathAction(string.Empty); //清除下保存文本 ContentControlVisible(true); //显示内容控件 OperationControlVisible(true); //显示操作控件 var length = FileInfo.Info.Length; OriginalSizeText = string.Format("解压前:{0}", Tool.ToDataString(length)); var decompressionSize = ZlCompressHelper.GetDecompressionSize(FileInfo.FileContent);//得到解压后大小 CompressSizeText = string.Format("解压后:{0}", Tool.ToDataString(decompressionSize)); CompressionRatioText = string.Format("压缩率:{0:0.00}%", (((double)length / decompressionSize) * 100)); }
/// <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); //解锁控件 }