FormMain.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Windows.Forms;
  3. using SevenZip;
  4. using System.IO;
  5. using System.Threading;
  6. using System.Windows;
  7. namespace SevenZipTestForms
  8. {
  9. public partial class FormMain : Form
  10. {
  11. public FormMain()
  12. {
  13. InitializeComponent();
  14. }
  15. private void FormMain_Load(object sender, EventArgs e)
  16. {
  17. cb_Format.Items.AddRange(Enum.GetNames(typeof(OutArchiveFormat)));
  18. cb_Method.Items.AddRange(Enum.GetNames(typeof(CompressionMethod)));
  19. cb_Format.SelectedIndex = (int)OutArchiveFormat.SevenZip;
  20. cb_Method.SelectedIndex = (int)CompressionMethod.Default;
  21. // tb_CompressDirectory.Text = @"D:\CS16";
  22. // tb_CompressOutput.Text = @"D:\CS1699.7z";
  23. // tb_ExtractDirectory.Text = @"C:\XZ";
  24. // tb_ExtractArchive.Text = @"C:\新管理软件201517.7z";
  25. trb_Level.Maximum = Enum.GetNames(typeof(CompressionLevel)).Length - 1;
  26. trb_Level.Value = (int)CompressionLevel.Normal;
  27. trb_Level_Scroll(this, EventArgs.Empty);
  28. }
  29. #region 压缩
  30. private void b_Compress_Click(object sender, EventArgs e)
  31. {
  32. SevenZipCompressor.SetLibraryPath(@"7z.dll");
  33. SevenZipCompressor cmp = new SevenZipCompressor();
  34. cmp.Compressing += new EventHandler<ProgressEventArgs>(cmp_Compressing);
  35. cmp.FileCompressionStarted += new EventHandler<FileNameEventArgs>(cmp_FileCompressionStarted);
  36. cmp.CompressionFinished += new EventHandler<EventArgs>(cmp_CompressionFinished);
  37. cmp.ArchiveFormat = (OutArchiveFormat)Enum.Parse(typeof(OutArchiveFormat), cb_Format.Text);
  38. cmp.CompressionLevel = (CompressionLevel)trb_Level.Value;
  39. cmp.CompressionMethod = (CompressionMethod)cb_Method.SelectedIndex;
  40. cmp.VolumeSize = chb_Volumes.Checked ? (int)nup_VolumeSize.Value : 0;
  41. string directory = tb_CompressDirectory.Text;
  42. string archFileName = tb_CompressOutput.Text;
  43. cmp.BeginCompressDirectory(directory, archFileName);
  44. }
  45. void cmp_Compressing(object sender, ProgressEventArgs e)
  46. {
  47. labBFB.Text = "已完成" + e.PercentDone.ToString()+"%";
  48. labBFB.Refresh();
  49. pb_CompressProgress.Value = e.PercentDone;
  50. //pb_CompressProgress.Increment(e.PercentDelta);
  51. pb_CompressProgress.Refresh();
  52. }
  53. void cmp_FileCompressionStarted(object sender, FileNameEventArgs e)
  54. {
  55. l_CompressProgress.Text = String.Format("正在压缩 \"{0}\"", e.FileName);
  56. l_CompressProgress.Refresh();
  57. pb_CompressWork.Value = e.PercentDone;
  58. pb_CompressWork.Refresh();
  59. }
  60. void cmp_CompressionFinished(object sender, EventArgs e)
  61. {
  62. l_CompressProgress.Text = "已压缩完成 100%";
  63. pb_CompressWork.Style = ProgressBarStyle.Blocks;
  64. pb_CompressProgress.Value = 0;
  65. pb_CompressWork.Value = 0;
  66. }
  67. private void b_Browse_Click(object sender, EventArgs e)
  68. {
  69. if (fbd_Directory.ShowDialog() == DialogResult.OK)
  70. {
  71. tb_CompressDirectory.Text = fbd_Directory.SelectedPath;
  72. }
  73. }
  74. private void trb_Level_Scroll(object sender, EventArgs e)
  75. {
  76. l_CompressionLevel.Text = String.Format("压缩级别: {0}", (CompressionLevel)trb_Level.Value);
  77. }
  78. private void b_BrowseOut_Click(object sender, EventArgs e)
  79. {
  80. sfd_Archive.Filter = "ZIP File(*.zip)|*.zip|RAR File(*.rar)|*.rar";
  81. if (sfd_Archive.ShowDialog() == DialogResult.OK)
  82. {
  83. tb_CompressOutput.Text = sfd_Archive.FileName;
  84. }
  85. }
  86. #endregion
  87. #region 解压
  88. void extr_ExtractionFinished(object sender, EventArgs e)
  89. {
  90. pb_ExtractWork.Style = ProgressBarStyle.Blocks;
  91. pb_ExtractProgress.Value = 0;
  92. pb_ExtractWork.Value = 0;
  93. l_ExtractProgress.Text = "完成";
  94. (sender as SevenZipExtractor).Dispose();
  95. }
  96. void extr_FileExists(object sender, FileOverwriteEventArgs e)
  97. {
  98. tb_Messages.Text += String.Format("警告: \"{0}\" 已经存在;覆盖\r\n", e.FileName);
  99. }
  100. void extr_FileExtractionStarted(object sender, FileInfoEventArgs e)
  101. {
  102. l_ExtractProgress.Text = String.Format("提取 \"{0}\"", e.FileInfo.FileName);
  103. l_ExtractProgress.Refresh();
  104. pb_ExtractWork.Increment(1);
  105. pb_ExtractWork.Refresh();
  106. }
  107. void extr_Extracting(object sender, ProgressEventArgs e)
  108. {
  109. pb_ExtractProgress.Increment(e.PercentDelta);
  110. pb_ExtractProgress.Refresh();
  111. }
  112. private void b_Extract_Click(object sender, EventArgs e)
  113. {
  114. SevenZipExtractor.SetLibraryPath(@"7z.dll");
  115. string fileName = tb_ExtractArchive.Text;
  116. string directory = tb_ExtractDirectory.Text;
  117. var extr = new SevenZipExtractor(fileName);
  118. pb_ExtractWork.Maximum = (int)extr.FilesCount;
  119. extr.Extracting += new EventHandler<ProgressEventArgs>(extr_Extracting);
  120. extr.FileExtractionStarted += new EventHandler<FileInfoEventArgs>(extr_FileExtractionStarted);
  121. extr.FileExists += new EventHandler<FileOverwriteEventArgs>(extr_FileExists);
  122. extr.ExtractionFinished += new EventHandler<EventArgs>(extr_ExtractionFinished);
  123. extr.BeginExtractArchive(directory);
  124. }
  125. private void b_ExtractBrowseDirectory_Click(object sender, EventArgs e)
  126. {
  127. if (fbd_Directory.ShowDialog() == DialogResult.OK)
  128. {
  129. tb_ExtractDirectory.Text = fbd_Directory.SelectedPath;
  130. }
  131. }
  132. private void b_ExtractBrowseArchive_Click(object sender, EventArgs e)
  133. {
  134. if (ofd_Archive.ShowDialog() == DialogResult.OK)
  135. {
  136. tb_ExtractArchive.Text = ofd_Archive.FileName;
  137. using (SevenZipExtractor extr = new SevenZipExtractor(ofd_Archive.FileName))
  138. {
  139. var lines = new string[extr.ArchiveFileNames.Count];
  140. extr.ArchiveFileNames.CopyTo(lines, 0);
  141. tb_Messages.Lines = lines;
  142. }
  143. }
  144. }
  145. #endregion
  146. }
  147. }