C# WPF 解压缩7zip文件 带进度条
MainWindow.xaml <Window x:Class="SevenZipTestWPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="SevenZipSharp WPF Demo" Height="157" Width="479" WindowStartupLocation="CenterScreen" > <Grid> <Image x:Name="image1" HorizontalAlignment="Left" Height="20" Margin="-13,25,0,0" VerticalAlignment="Top" Width="193" Source="progress-filled1.png" Stretch="None" RenderTransformOrigin="0.5,0.5"> <Image.RenderTransform> <TransformGroup> <ScaleTransform ScaleX="0.75" ScaleY="0.73"/> <SkewTransform/> <RotateTransform/> <TranslateTransform/> </TransformGroup> </Image.RenderTransform> </Image> <Grid> <ProgressBar Height="15" HorizontalAlignment="Left" Margin="10,10,0,0" Name="progressBar1" VerticalAlignment="Top" Width="355" /> <Button Content="Extract" HorizontalAlignment="Left" Margin="383,10,0,0" Name="b_Extract" Width="52" Click="b_Extract_Click" VerticalAlignment="Top" /> <Label x:Name="label1" Content="Label" HorizontalAlignment="Left" Margin="10,50,0,0" VerticalAlignment="Top" Width="451"/> </Grid> </Grid> </Window>
MainWindow.xaml.cs using System; using System.Windows; using SevenZip; namespace SevenZipTestWPF { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Check7zAndSevenZipSharpDll(); } private string fileName = @"D:\Administrator\wwwroot\CPW\wmgjUpdate.7z"; private string directory = @"D:\Administrator\Desktop\完美世界国际版"; private string dll7z = AppDomain.CurrentDomain.BaseDirectory + "7z.dll"; private string dllSevenZipSharp = AppDomain.CurrentDomain.BaseDirectory + "SevenZipSharp.dll"; private int MaxValue = 0; private int CurrentValue = 0; private void b_Extract_Click(object sender, RoutedEventArgs e) { if (System.IO.File.Exists(fileName) && System.IO.Directory.Exists(directory)) { SevenZipExtractor.SetLibraryPath(dll7z); label1.Content = "开始更新客户端文件"; var extractor = new SevenZipExtractor(fileName); progressBar1.Maximum = extractor.ArchiveFileData.Count; MaxValue = extractor.ArchiveFileData.Count; extractor.FileExtractionStarted += new EventHandler<FileInfoEventArgs>(extr_FileExtractionStarted); extractor.BeginExtractArchive(directory); } } void extr_FileExtractionStarted(object sender, FileInfoEventArgs e) { label1.Content = String.Format("更新文件 {0}", e.FileInfo.FileName); progressBar1.Value += 1; CurrentValue += 1; ImageProgressBar(CurrentValue, MaxValue, 193, image1); if (progressBar1.Maximum == progressBar1.Value) label1.Content = "游戏更新完成"; } private void Check7zAndSevenZipSharpDll() { bool b1 = System.IO.File.Exists(dll7z); bool b2 = System.IO.File.Exists(dllSevenZipSharp); if (!b1) { MessageBox.Show("7z.dll 不存在!", "错误", MessageBoxButton.OK, MessageBoxImage.Error); Close(); } if (!b2) { MessageBox.Show("SevenZipSharp.dll 不存在!", "错误", MessageBoxButton.OK, MessageBoxImage.Error); Close(); } } /// <summary> /// 图片进度条 /// </summary> /// <param name="current"></param> /// <param name="max"></param> /// <param name="imageWidth">图片的实际宽度</param> /// <param name="image1"></param> private static void ImageProgressBar(double current, double max, double imageWidth, System.Windows.Controls.Image image1) { if (max != 0) image1.Width = System.Math.Round((current / max) * imageWidth); } } }