C #: SevenZipSharp uses 7z. dll for compression and decompression

Source: Internet
Author: User

1. Click the address above to open the download page.

2. Click "normal download"-Wait for 30 seconds-click "Download"-save

The program runs as follows:


Use 7z. dll to decompress the file


Select where to decompress


Decompress the package.


Add the file to be compressed


Select save path


After compression is completed.


Main source program:

[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 );
}
}
}
}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.