123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- using System;
- using System.Windows.Forms;
- using SevenZip;
- using System.IO;
- using System.Threading;
- using System.Windows;
- namespace SevenZipTestForms
- {
- public partial class FormMain : Form
- {
- public FormMain()
- {
- InitializeComponent();
- }
- private void FormMain_Load(object sender, EventArgs e)
- {
- cb_Format.Items.AddRange(Enum.GetNames(typeof(OutArchiveFormat)));
- cb_Method.Items.AddRange(Enum.GetNames(typeof(CompressionMethod)));
- cb_Format.SelectedIndex = (int)OutArchiveFormat.SevenZip;
- cb_Method.SelectedIndex = (int)CompressionMethod.Default;
- // tb_CompressDirectory.Text = @"D:\CS16";
- // tb_CompressOutput.Text = @"D:\CS1699.7z";
- // tb_ExtractDirectory.Text = @"C:\XZ";
- // tb_ExtractArchive.Text = @"C:\新管理软件201517.7z";
- trb_Level.Maximum = Enum.GetNames(typeof(CompressionLevel)).Length - 1;
- trb_Level.Value = (int)CompressionLevel.Normal;
- trb_Level_Scroll(this, EventArgs.Empty);
- }
- #region 压缩
- private void b_Compress_Click(object sender, EventArgs e)
- {
- SevenZipCompressor.SetLibraryPath(@"7z.dll");
- SevenZipCompressor cmp = new SevenZipCompressor();
- cmp.Compressing += new EventHandler<ProgressEventArgs>(cmp_Compressing);
- cmp.FileCompressionStarted += new EventHandler<FileNameEventArgs>(cmp_FileCompressionStarted);
- cmp.CompressionFinished += new EventHandler<EventArgs>(cmp_CompressionFinished);
- cmp.ArchiveFormat = (OutArchiveFormat)Enum.Parse(typeof(OutArchiveFormat), cb_Format.Text);
- cmp.CompressionLevel = (CompressionLevel)trb_Level.Value;
- cmp.CompressionMethod = (CompressionMethod)cb_Method.SelectedIndex;
- cmp.VolumeSize = chb_Volumes.Checked ? (int)nup_VolumeSize.Value : 0;
- string directory = tb_CompressDirectory.Text;
- string archFileName = tb_CompressOutput.Text;
- cmp.BeginCompressDirectory(directory, archFileName);
- }
- void cmp_Compressing(object sender, ProgressEventArgs e)
- {
- labBFB.Text = "已完成" + e.PercentDone.ToString()+"%";
- labBFB.Refresh();
- pb_CompressProgress.Value = e.PercentDone;
- //pb_CompressProgress.Increment(e.PercentDelta);
- pb_CompressProgress.Refresh();
- }
- void cmp_FileCompressionStarted(object sender, FileNameEventArgs e)
- {
- l_CompressProgress.Text = String.Format("正在压缩 \"{0}\"", e.FileName);
- l_CompressProgress.Refresh();
- pb_CompressWork.Value = e.PercentDone;
- pb_CompressWork.Refresh();
-
- }
- void cmp_CompressionFinished(object sender, EventArgs e)
- {
- l_CompressProgress.Text = "已压缩完成 100%";
- pb_CompressWork.Style = ProgressBarStyle.Blocks;
- pb_CompressProgress.Value = 0;
- pb_CompressWork.Value = 0;
- }
- private void b_Browse_Click(object sender, EventArgs e)
- {
- if (fbd_Directory.ShowDialog() == DialogResult.OK)
- {
- tb_CompressDirectory.Text = fbd_Directory.SelectedPath;
- }
- }
- private void trb_Level_Scroll(object sender, EventArgs e)
- {
- l_CompressionLevel.Text = String.Format("压缩级别: {0}", (CompressionLevel)trb_Level.Value);
- }
- private void b_BrowseOut_Click(object sender, EventArgs e)
- {
- sfd_Archive.Filter = "ZIP File(*.zip)|*.zip|RAR File(*.rar)|*.rar";
- if (sfd_Archive.ShowDialog() == DialogResult.OK)
- {
- tb_CompressOutput.Text = sfd_Archive.FileName;
- }
- }
- #endregion
- #region 解压
- void extr_ExtractionFinished(object sender, EventArgs e)
- {
- pb_ExtractWork.Style = ProgressBarStyle.Blocks;
- pb_ExtractProgress.Value = 0;
- pb_ExtractWork.Value = 0;
- l_ExtractProgress.Text = "完成";
- (sender as SevenZipExtractor).Dispose();
- }
- void extr_FileExists(object sender, FileOverwriteEventArgs e)
- {
- tb_Messages.Text += String.Format("警告: \"{0}\" 已经存在;覆盖\r\n", e.FileName);
- }
- void extr_FileExtractionStarted(object sender, FileInfoEventArgs e)
- {
- l_ExtractProgress.Text = String.Format("提取 \"{0}\"", e.FileInfo.FileName);
- l_ExtractProgress.Refresh();
- pb_ExtractWork.Increment(1);
- pb_ExtractWork.Refresh();
- }
- void extr_Extracting(object sender, ProgressEventArgs e)
- {
- pb_ExtractProgress.Increment(e.PercentDelta);
- pb_ExtractProgress.Refresh();
- }
- private void b_Extract_Click(object sender, EventArgs e)
- {
- SevenZipExtractor.SetLibraryPath(@"7z.dll");
- string fileName = tb_ExtractArchive.Text;
- string directory = tb_ExtractDirectory.Text;
- var extr = new SevenZipExtractor(fileName);
- pb_ExtractWork.Maximum = (int)extr.FilesCount;
- extr.Extracting += new EventHandler<ProgressEventArgs>(extr_Extracting);
- extr.FileExtractionStarted += new EventHandler<FileInfoEventArgs>(extr_FileExtractionStarted);
- extr.FileExists += new EventHandler<FileOverwriteEventArgs>(extr_FileExists);
- extr.ExtractionFinished += new EventHandler<EventArgs>(extr_ExtractionFinished);
- extr.BeginExtractArchive(directory);
- }
- private void b_ExtractBrowseDirectory_Click(object sender, EventArgs e)
- {
- if (fbd_Directory.ShowDialog() == DialogResult.OK)
- {
- tb_ExtractDirectory.Text = fbd_Directory.SelectedPath;
- }
- }
- private void b_ExtractBrowseArchive_Click(object sender, EventArgs e)
- {
- if (ofd_Archive.ShowDialog() == DialogResult.OK)
- {
- tb_ExtractArchive.Text = ofd_Archive.FileName;
- using (SevenZipExtractor extr = new SevenZipExtractor(ofd_Archive.FileName))
- {
- var lines = new string[extr.ArchiveFileNames.Count];
- extr.ArchiveFileNames.CopyTo(lines, 0);
- tb_Messages.Lines = lines;
- }
- }
- }
- #endregion
- }
- }
|