using ICSharpCode.SharpZipLib.Tar; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.IO.Compression; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using wispro.sp.utility; using SharpCompress.Archives; using SharpCompress.Archives.Rar; using SharpCompress.Common; using System.Collections; namespace wispro.sp.winClient { public partial class frmCaseFileCompare : Form { private CompareDocx draft_fristReturn; private CompareDocx fristReturn_Finally; public frmCaseFileCompare() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.txtCaseNo.Text)) { MessageBox.Show("请输入案号!", "提示框", MessageBoxButtons.OK); this.txtCaseNo.Focus(); return; } var retObj1 = IPEasyUtility.DownloadCaseFiles(this.txtCaseNo.Text); string draftFile = null; string firstReFile = null; string finallyFile = null; if (((IDictionary)retObj1).ContainsKey("finallyFile")) { if (!string.IsNullOrEmpty(retObj1.finallyFile)) { if (retObj1.finallyFile.EndsWith(".zip")) { ExtractZip(retObj1.finallyFile, $"c:\\temp\\{retObj1.CaseNo}\\finallyFile"); foreach (string f in Directory.GetFiles($"c:\\temp\\{retObj1.CaseNo}\\finallyFile", "*.*", SearchOption.AllDirectories)) { if (f.IndexOf("定稿", StringComparison.OrdinalIgnoreCase) > 0) { finallyFile = f; break; } } } else { finallyFile = retObj1.finallyFile; } } } if (((IDictionary)retObj1).ContainsKey("firstReturnFile")) { if (!string.IsNullOrEmpty(retObj1.firstReturnFile)) { if (retObj1.firstReturnFile.EndsWith(".zip")) { ExtractZip(retObj1.firstReturnFile, $"c:\\temp\\{retObj1.CaseNo}\\firstReturnFile"); foreach (string f in Directory.GetFiles($"c:\\temp\\{retObj1.CaseNo}\\firstReturnFile", "*.*", SearchOption.AllDirectories)) { if (f.IndexOf("v1F", StringComparison.OrdinalIgnoreCase) > 0) { firstReFile = f; break; } } } else { firstReFile = retObj1.firstReturnFile; } } } if (((IDictionary)retObj1).ContainsKey("draftFile")) { if (!string.IsNullOrEmpty(retObj1.draftFile)) { if (retObj1.draftFile.EndsWith(".zip")) { ExtractZip(retObj1.draftFile, $"c:\\temp\\{retObj1.CaseNo}\\draftFile"); foreach (string f in Directory.GetFiles($"c:\\temp\\{retObj1.CaseNo}\\draftFile", "*.*", SearchOption.AllDirectories)) { if (f.IndexOf("v1r0", StringComparison.OrdinalIgnoreCase) > 0) { draftFile = f; break; } } } else { draftFile = retObj1.draftFile; } } } if (!string.IsNullOrEmpty(firstReFile) && !string.IsNullOrEmpty(draftFile)) { draft_fristReturn = new CompareDocx(); draft_fristReturn.Compare(draftFile, firstReFile); } if (!string.IsNullOrEmpty(firstReFile) && !string.IsNullOrEmpty(finallyFile)) { fristReturn_Finally = new CompareDocx(); fristReturn_Finally.Compare(firstReFile, finallyFile); } return; try { if (!string.IsNullOrEmpty(retObj1.finallyFile)) File.Delete(retObj1.finallyFile); } catch { } try { if (!string.IsNullOrEmpty(retObj1.draftFile)) File.Delete(retObj1.draftFile); } catch { } try { if (!string.IsNullOrEmpty(retObj1.draftFile)) File.Delete(retObj1.firstReturnFile); } catch { } if (Directory.Exists($"c:\\temp\\{retObj1.CaseNo}")) { Directory.Delete($"c:\\temp\\{retObj1.CaseNo}", true); } lblMsg.Text = string.Empty; if (string.IsNullOrEmpty(draftFile)) { lblMsg.Text = "初稿文件没有找到"; } if (string.IsNullOrEmpty(firstReFile)) { lblMsg.Text = $"{lblMsg.Text},第一次返稿文件没有找到!"; } if (string.IsNullOrEmpty(finallyFile)) { lblMsg.Text = $"{lblMsg.Text},定稿文件没有找到!"; } comboBox1.SelectedIndex = 0; } private void ExtractZip(string zipFilePath, string extractPath) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); // 确保提取目录存在 Directory.CreateDirectory(extractPath); // 使用 ZipArchive 解压缩 ZIP 文件 using (FileStream zipToOpen = new FileStream(zipFilePath, FileMode.Open)) { using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read, true, Encoding.GetEncoding("GB2312"))) { foreach (ZipArchiveEntry entry in archive.Entries) { if (entry.FullName.EndsWith("/", StringComparison.Ordinal) || entry.FullName.EndsWith("\\", StringComparison.Ordinal)) { Console.WriteLine($"目录: {entry.FullName}"); } else { string entryPath = Path.Combine(extractPath, entry.FullName); Directory.CreateDirectory(new System.IO.FileInfo(entryPath).Directory.FullName); entry.ExtractToFile(entryPath, true); } } } } } public static void ExtractRarFile(string rarPath, string outputPath) { try { // 确保输出目录存在 Directory.CreateDirectory(outputPath); // 打开RAR文件 using (var archive = RarArchive.Open(rarPath)) { // 遍历所有文件 foreach (var entry in archive.Entries) { if (!entry.IsDirectory) { // 构建完整输出路径 string outputFilePath = Path.Combine(outputPath, entry.Key); // 确保输出文件的目录存在 Directory.CreateDirectory(Path.GetDirectoryName(outputFilePath)); // 解压文件 entry.WriteToFile(outputFilePath, new ExtractionOptions { ExtractFullPath = true, Overwrite = true }); } } } } catch (Exception ex) { throw new Exception($"解压RAR文件时出错: {ex.Message}"); } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (draft_fristReturn != null || fristReturn_Finally != null) { richTextBox1.Clear(); string strHtml = string.Empty; switch (comboBox1.SelectedIndex) { case 0: if (draft_fristReturn != null) { strHtml = $"所有文字比较:

初稿与第一次返稿比较结果:

初稿文件字数:{draft_fristReturn.AllStringResult.oldWordCount}

删除文字数量:{draft_fristReturn.AllStringResult.DeleteCount}

插入文字数量:{draft_fristReturn.AllStringResult.InsertCount}

修改比率:{(draft_fristReturn.AllStringResult.diffRate * 100).ToString("0.0000")}%

文档语义相似度:{(draft_fristReturn.AllStringResult.TextSimilarity*100).ToString("0.0000")}%

"; } if (fristReturn_Finally != null) { strHtml = strHtml + $"

第一次返稿与定稿比较结果:

第一次返稿文件字数:{fristReturn_Finally.AllStringResult.oldWordCount}

删除文字数量:{fristReturn_Finally.AllStringResult.DeleteCount}

插入文字数量:{fristReturn_Finally.AllStringResult.InsertCount}

修改比率:{(fristReturn_Finally.AllStringResult.diffRate * 100).ToString("0.0000")}%

文档语义相似度:{(fristReturn_Finally.AllStringResult.TextSimilarity * 100).ToString("0.0000")}%

"; } richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(strHtml); break; case 1: if (draft_fristReturn != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(draft_fristReturn.AllStringResult.CompareResultString); } break; case 2: if (fristReturn_Finally != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(fristReturn_Finally.AllStringResult.CompareResultString); } break; case 3: if (draft_fristReturn != null) { strHtml = $"摘要比较:

初稿与第一次返稿比较结果:

初稿文件字数:{draft_fristReturn.AbstractResult.oldWordCount}

删除文字数量:{draft_fristReturn.AbstractResult.DeleteCount}

插入文字数量:{draft_fristReturn.AbstractResult.InsertCount}

修改比率:{(draft_fristReturn.AbstractResult.diffRate * 100).ToString("0.0000")}%

语义相似度:{(draft_fristReturn.AbstractResult.TextSimilarity * 100).ToString("0.0000")}%

"; } if (fristReturn_Finally != null) { strHtml = strHtml + $"

第一次返稿与定稿比较结果:

第一次返稿文件字数:{fristReturn_Finally.AbstractResult.oldWordCount}

删除文字数量:{fristReturn_Finally.AbstractResult.DeleteCount}

插入文字数量:{fristReturn_Finally.AbstractResult.InsertCount}

修改比率:{(fristReturn_Finally.AbstractResult.diffRate * 100).ToString("0.0000")}%

语义相似度:{(fristReturn_Finally.AbstractResult.TextSimilarity * 100).ToString("0.0000")}%

"; } richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(strHtml); break; case 4: if (draft_fristReturn != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(draft_fristReturn.AbstractResult.CompareResultString); } break; case 5: if (fristReturn_Finally != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(fristReturn_Finally.AbstractResult.CompareResultString); } break; case 6: if (draft_fristReturn != null) { strHtml = $"权力要求比较:

初稿与第一次返稿比较结果:

初稿文件字数:{draft_fristReturn.ClaimResult.oldWordCount}

删除文字数量:{draft_fristReturn.ClaimResult.DeleteCount}

插入文字数量:{draft_fristReturn.ClaimResult.InsertCount}

修改比率:{(draft_fristReturn.ClaimResult.diffRate * 100).ToString("0.0000")}%

语义相似度:{(draft_fristReturn.ClaimResult.TextSimilarity * 100).ToString("0.0000")}%

"; } if (fristReturn_Finally != null) { strHtml = strHtml + $"

第一次返稿与定稿比较结果:

第一次返稿文件字数:{fristReturn_Finally.ClaimResult.oldWordCount}

删除文字数量:{fristReturn_Finally.ClaimResult.DeleteCount}

插入文字数量:{fristReturn_Finally.ClaimResult.InsertCount}

修改比率:{(fristReturn_Finally.ClaimResult.diffRate * 100).ToString("0.0000")}%

语义相似度:{(fristReturn_Finally.ClaimResult.TextSimilarity * 100).ToString("0.0000")}%

"; } richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(strHtml); break; case 7: if (draft_fristReturn != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(draft_fristReturn.ClaimResult.CompareResultString); } break; case 8: if (fristReturn_Finally != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(fristReturn_Finally.ClaimResult.CompareResultString); } break; case 9: if (draft_fristReturn != null) { strHtml = $"说明书比较:

初稿与第一次返稿比较结果:

初稿文件字数:{draft_fristReturn.FulltextResult.oldWordCount}

删除文字数量:{draft_fristReturn.FulltextResult.DeleteCount}

插入文字数量:{draft_fristReturn.FulltextResult.InsertCount}

修改比率:{(draft_fristReturn.FulltextResult.diffRate * 100).ToString("0.0000")}%

语义相似度:{(draft_fristReturn.FulltextResult.TextSimilarity * 100).ToString("0.0000")}%

"; } if (fristReturn_Finally != null) { strHtml = strHtml + $"

第一次返稿与定稿比较结果:

第一次返稿文件字数:{fristReturn_Finally.FulltextResult.oldWordCount}

删除文字数量:{fristReturn_Finally.FulltextResult.DeleteCount}

插入文字数量:{fristReturn_Finally.FulltextResult.InsertCount}

修改比率:{(fristReturn_Finally.FulltextResult.diffRate * 100).ToString("0.0000")}%

语义相似度:{(fristReturn_Finally.FulltextResult.TextSimilarity * 100).ToString("0.0000")}%

"; } richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(strHtml); break; case 10: if (draft_fristReturn != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(draft_fristReturn.FulltextResult.CompareResultString); } break; case 11: if (fristReturn_Finally != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(fristReturn_Finally.FulltextResult.CompareResultString); } break; } } } private void button2_Click(object sender, EventArgs e) { var retData = IPEasyUtility.GetFinished3FilesCases(1); return; OpenFileDialog openFileDialog = new OpenFileDialog(); if(openFileDialog.ShowDialog() == DialogResult.OK) { string strPath = openFileDialog.FileName.ToLower().Replace(".rar", "").Replace(".zip",""); if(openFileDialog.FileName.Contains(".rar")) { ExtractRarFile(openFileDialog.FileName, strPath); } else { ExtractZip(openFileDialog.FileName,strPath); } string[] files = Directory.EnumerateFiles(strPath, "*.*", SearchOption.AllDirectories).ToArray(); Array.Sort(files); Array.Reverse(files); draft_fristReturn = new CompareDocx(); draft_fristReturn.Compare(files[0], files[1]); if (files.Length > 2) { fristReturn_Finally = new CompareDocx(); fristReturn_Finally.Compare(files[1], files[2]); } SaveResult(strPath,draft_fristReturn); SaveResult(strPath, fristReturn_Finally); } } private void SaveResult(string strPath,CompareDocx draft_fristReturn) { string strHtml = @" 对比结果
{0}
{1}
全部对比 摘要 权利要求 说明书
原文字数 {2} {3} {4} {5}
删除字数 {6} {7} {8} {9}
插入字数 {10} {11} {12} {13}
修改比例 {14} {15} {16} {17}
语义相似度 {18} {19} {20} {21}
{22}
{23}
{24}
{25}
"; if (draft_fristReturn != null) { var temHtml = strHtml .Replace("{0}", new FileInfo(draft_fristReturn.oldDocument.FilePath).Name) .Replace("{1}", new FileInfo(draft_fristReturn.newDocument.FilePath).Name) .Replace("{2}", draft_fristReturn.AllStringResult.oldWordCount.ToString()) .Replace("{3}", draft_fristReturn.AbstractResult.oldWordCount.ToString()) .Replace("{4}", draft_fristReturn.ClaimResult.oldWordCount.ToString()) .Replace("{5}", draft_fristReturn.FulltextResult.oldWordCount.ToString()) .Replace("{6}", draft_fristReturn.AllStringResult.DeleteCount.ToString()) .Replace("{7}", draft_fristReturn.AbstractResult.DeleteCount.ToString()) .Replace("{8}", draft_fristReturn.ClaimResult.DeleteCount.ToString()) .Replace("{9}", draft_fristReturn.FulltextResult.DeleteCount.ToString()) .Replace("{10}", draft_fristReturn.AllStringResult.InsertCount.ToString()) .Replace("{11}", draft_fristReturn.AbstractResult.InsertCount.ToString()) .Replace("{12}", draft_fristReturn.ClaimResult.InsertCount.ToString()) .Replace("{13}", draft_fristReturn.FulltextResult.InsertCount.ToString()) .Replace("{14}", (draft_fristReturn.AllStringResult.diffRate * 100).ToString("0.0000") + "%") .Replace("{15}", (draft_fristReturn.AbstractResult.diffRate * 100).ToString("0.0000") + "%") .Replace("{16}", (draft_fristReturn.ClaimResult.diffRate * 100).ToString("0.0000") + "%") .Replace("{17}", (draft_fristReturn.FulltextResult.diffRate * 100).ToString("0.0000") + "%") .Replace("{18}", (draft_fristReturn.AllStringResult.TextSimilarity * 100).ToString("0.0000") + "%") .Replace("{19}", (draft_fristReturn.AbstractResult.TextSimilarity * 100).ToString("0.0000") + "%") .Replace("{20}", (draft_fristReturn.ClaimResult.TextSimilarity * 100).ToString("0.0000") + "%") .Replace("{21}", (draft_fristReturn.FulltextResult.TextSimilarity * 100).ToString("0.0000") + "%") .Replace("{22}", draft_fristReturn.AllStringResult.CompareResultString) .Replace("{23}", draft_fristReturn.AbstractResult.CompareResultString) .Replace("{24}", draft_fristReturn.ClaimResult.CompareResultString) .Replace("{25}", draft_fristReturn.FulltextResult.CompareResultString); string fileName = $"{new FileInfo(draft_fristReturn.oldDocument.FilePath).Name}_{new FileInfo(draft_fristReturn.newDocument.FilePath).Name}.html"; using (var f = File.CreateText(Path.Combine(strPath, fileName))) { f.Write(temHtml); } } } } }