程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#:SevenZipSharp使用7z.dll來進行壓縮與解壓縮

C#:SevenZipSharp使用7z.dll來進行壓縮與解壓縮

編輯:C#入門知識

1 點擊上面的地址,打開下載頁面

2 點擊"普通下載"--等待30秒--點擊"下載"按鈕--保存

程序運行截圖如下:


使用7z.dll來解壓文件


選擇解壓到何處


解壓完畢。


添加需要壓縮的文件


選擇保存路徑


壓縮完畢的提示。


主要源程序:

[csharp]
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2012/10/29
 * Time: 16:13
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */ 
using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Windows.Forms; 
 
using SevenZip; 
using SevenZipSharp; 
 
namespace SevenZipSharp 

    /// <summary> 
    /// Description of MainForm. 
    /// </summary> 
    public partial class MainForm : Form 
    { 
        SevenZip.SevenZipExtractor extractor = null; 
        SevenZipCompressor compressor = new SevenZipCompressor(); 
         
        public MainForm() 
        { 
            // 
            // The InitializeComponent() call is required for Windows Forms designer support. 
            // 
            InitializeComponent(); 
             
            // 
            // TODO: Add constructor code after the InitializeComponent() call. 
            // 
            this.label1.Text = "Please choose a archive to extract at first."; 
            this.label5.Text = "0"; 
            this.label2.Text = ""; 
            this.label3.Text = ""; 
            this.listBox1.Items.Clear(); 
            this.listBox2.Items.Clear(); 
        } 
         
        void ShowException(string msg){ 
            MessageBox.Show(msg,"Exception!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); 
        } 
         
        void ShowInformation(string msg){ 
            MessageBox.Show(msg,"Information!",MessageBoxButtons.OK,MessageBoxIcon.Information); 
        } 
         
        void BtnBrowseCompressedFileClick(object sender, EventArgs e) 
        { 
            try{ 
                OpenFileDialog ofd = new OpenFileDialog(); 
                ofd.Filter = "ZIP File(*.zip)|*.zip|RAR File(*.rar)|*.rar"; 
                if(ofd.ShowDialog() == DialogResult.OK){ 
                    string archiveFullName = ofd.FileName; 
                    extractor = new SevenZipExtractor(archiveFullName); 
                    extractor.ExtractionFinished += delegate { 
                        this.label1.Text = "Extraction Finished."; 
                    }; 
                    extractor.Extracting += delegate { 
                        this.label1.Text = "Extracting, please wait..."; 
                    }; 
                    extractor.FileExtractionStarted += delegate { 
                        this.label1.Text = "Start extracting..."; 
                    }; 
                     
                    this.label5.Text = extractor.FilesCount.ToString(); 
                    this.label2.Text = extractor.PackedSize.ToString() + " bytes"; 
                    this.listBox1.Items.Clear(); 
                    foreach(string fileName in extractor.ArchiveFileNames){ 
                        this.listBox1.Items.Add(fileName); 
                    } 
                } 
            }catch(Exception ex){ 
                ShowException(ex.Message); 
            } 
        } 
         
        void BtnExtractClick(object sender, EventArgs e) 
        { 
            try{ 
                FolderBrowserDialog fbd = new FolderBrowserDialog(); 
                if(fbd.ShowDialog() == DialogResult.OK){ 
                    extractor.ExtractArchive(fbd.SelectedPath); 
                } 
                extractor.Dispose(); 
                extractor = null; 
            }catch(Exception ex){ 
                ShowException(ex.Message); 
            } 
        } 
         
        void MainFormFormClosing(object sender, FormClosingEventArgs e) 
        { 
            if(extractor != null) 
                extractor.Dispose(); 
        } 
         
        void BtnAddFileClick(object sender, EventArgs e) 
        { 
            try{ 
                OpenFileDialog ofd = new OpenFileDialog(); 
                ofd.Filter= "All Files(*.*)|*.*"; 
                if(ofd.ShowDialog() == DialogResult.OK){ 
                    this.listBox2.Items.Add(ofd.FileName); 
                } 
            }catch(Exception ex){ 
                ShowException(ex.Message); 
            } 
        } 
         
        void BtnRemoveFileClick(object sender, EventArgs e) 
        { 
            try{ 
                if(this.listBox2.SelectedItem != null){ 
                    int index = this.listBox2.SelectedIndex; 
                    this.listBox2.Items.RemoveAt(index); 
                } 
            }catch(Exception ex){ 
                ShowException(ex.Message); 
            } 
        } 
         
        void BtnCompressClick(object sender, EventArgs e) 
        { 
            try{ 
                string archiveName = string.Empty; 
                SaveFileDialog sfd = new SaveFileDialog(); 
                sfd.Filter = "ZIP File(*.zip)|*.zip|RAR File(*.rar)|*.rar"; 
                if( sfd.ShowDialog() == DialogResult.OK){ 
                    archiveName = sfd.FileName; 
                     
                    string[] fileFullNames = new string[this.listBox2.Items.Count]; 
                    int i = 0; 
                    foreach(object o in this.listBox2.Items){ 
                        fileFullNames[i++] = o.ToString(); 
                    } 
                     
                    compressor.FileCompressionStarted += delegate {  
                        this.label3.Text = "Start compressing..."; 
                    }; 
                    compressor.FileCompressionFinished += delegate {  
                        this.label3.Text = "Finish compressing."; 
                    }; 
                    compressor.CompressFiles(archiveName,fileFullNames); 
                    ShowInformation("Compressiong Finished!"); 
                } 
            }catch(Exception ex){ 
                ShowException(ex.Message); 
            } 
        } 
    } 


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved