目录
实践过程
效果
代码
public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
#region 压缩文件及文件夹
private bool ZipFileDictory(string FolderToZip, ZipOutputStream ZOPStream, string ParentFolderName)
{
bool res = true;
string[] folders, filenames;
ZipEntry entry = null;
FileStream fs = null;
Crc crc = new Crc32();
try
{
entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/"));
ZOPStream.PutNextEntry(entry);
ZOPStream.Flush();
filenames = Directory.GetFiles(FolderToZip);
foreach (string file in filenames)
{
fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer,, buffer.Length);
entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file)));
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
ZOPStream.PutNextEntry(entry);
ZOPStream.Write(buffer,, buffer.Length);
}
}
catch
{
res = false;
}
finally
{
if (fs != null)
{
fs.Close();
fs = null;
}
if (entry != null)
{
entry = null;
}
GC.Collect();
GC.Collect();
}
folders = Directory.GetDirectories(FolderToZip);
foreach (string folder in folders)
{
if (!ZipFileDictory(folder, ZOPStream, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))
{
return false;
}
}
return res;
}
private bool ZipFileDictory(string FolderToZip, string ZipedFile)
{
bool res;
if (!Directory.Exists(FolderToZip))
{
return false;
}
ZipOutputStream ZOPStream = new ZipOutputStream(File.Create(ZipedFile));
ZOPStream.SetLevel();
res = ZipFileDictory(FolderToZip, ZOPStream, "");
ZOPStream.Finish();
ZOPStream.Close();
return res;
}
public bool Zip(String FileToZip, String ZipedFile)
{
if (Directory.Exists(FileToZip))
{
return ZipFileDictory(FileToZip, ZipedFile);
}
else
{
return false;
}
}
#endregion
#region 复制文件
public void CopyFile(string[] list,string strNewPath,ToolStripProgressBar TSPBar)
{
try
{
TSPBar.Maximum = list.Length;
string strNewFile = "c:\\" + strNewPath;
if (!Directory.Exists(strNewFile))
Directory.CreateDirectory(strNewFile);
foreach (object objFile in list)
{
string strFile = objFile.ToString();
string Filename = strFile.Substring(strFile.LastIndexOf("\\") +, strFile.Length - strFile.LastIndexOf("\\") - 1);
File.Copy(strFile, strNewFile+"\\"+Filename, true);
TSPBar.Value +=;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region 解压文件
public void UnZip(string FileToUpZip, string ZipedFolder)
{
if (!File.Exists(FileToUpZip))
{
return;
}
if (!Directory.Exists(ZipedFolder))
{
Directory.CreateDirectory(ZipedFolder);
}
ZipInputStream ZIPStream = null;
ZipEntry theEntry = null;
string fileName;
FileStream streamWriter = null;
try
{
ZIPStream = new ZipInputStream(File.OpenRead(FileToUpZip));
while ((theEntry = ZIPStream.GetNextEntry()) != null)
{
if (theEntry.Name != String.Empty)
{
fileName = Path.Combine(ZipedFolder, theEntry.Name);
if (fileName.EndsWith("/") || fileName.EndsWith("\\"))
{
Directory.CreateDirectory(fileName);
continue;
}
streamWriter = File.Create(fileName);
int size =;
byte[] data = new byte[];
while (true)
{
size = ZIPStream.Read(data,, data.Length);
if (size >)
{
streamWriter.Write(data,, size);
}
else
{
break;
}
}
}
}
}
finally
{
if (streamWriter != null)
{
streamWriter.Close();
streamWriter = null;
}
if (theEntry != null)
{
theEntry = null;
}
if (ZIPStream != null)
{
ZIPStream.Close();
ZIPStream = null;
}
GC.Collect();
GC.Collect();
}
}
#endregion
string[] files;
string[] files;
private void button_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
files = openFileDialog.FileNames;
string file = "";
for (int i =; i < files.Length; i++)
{
file += files[i].ToString() + ",";
}
file = file.Remove(file.LastIndexOf(","));
txtfiles.Text = file;
}
}
private void button_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
files = openFileDialog2.FileNames;
string file = "";
for (int i =; i < files2.Length; i++)
{
file += files[i].ToString() + ",";
}
file = file.Remove(file.LastIndexOf(","));
txtfiles.Text = file;
}
}
private void button_Click(object sender, EventArgs e)
{
try
{
if (txtfiles.Text.Trim()!="")
{
toolStripProgressBar.Maximum = files.Length;
if (files.Length >)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string strNewPath = DateTime.Now.ToString("yyyyMMddhhmmss");
CopyFile(files, strNewPath, toolStripProgressBar);
Zip("c:\\"+strNewPath,saveFileDialog.FileName);
Directory.Delete("c:\\" + strNewPath, true);
MessageBox.Show("压缩文件成功");
}
}
toolStripProgressBar.Value = 0;
}
else
{
MessageBox.Show("警告:请选择要进行批量压缩的文件!","警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
catch { }
}
private void button_Click(object sender, EventArgs e)
{
try
{
if (txtfiles.Text.Trim() != "")
{
toolStripProgressBar.Maximum = files2.Length;
for (int i =; i < files2.Length; i++)
{
toolStripProgressBar.Value = i;
string path = files[i].ToString();
string newpath = path.Remove(path.LastIndexOf("\\") +);
UnZip(path, newpath);
}
toolStripProgressBar.Value = 0;
MessageBox.Show("解压缩成功!");
}
else
{
MessageBox.Show("警告:请选择要进行批量解压缩的文件!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch { }
}
}
partial class Form
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.groupBox = new System.Windows.Forms.GroupBox();
this.button = new System.Windows.Forms.Button();
this.txtfiles = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.button = new System.Windows.Forms.Button();
this.groupBox = new System.Windows.Forms.GroupBox();
this.button = new System.Windows.Forms.Button();
this.txtfiles = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.button = new System.Windows.Forms.Button();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.statusStrip = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabel = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripProgressBar = new System.Windows.Forms.ToolStripProgressBar();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.groupBox.SuspendLayout();
this.groupBox.SuspendLayout();
this.statusStrip.SuspendLayout();
this.SuspendLayout();
this.groupBox.Controls.Add(this.button1);
this.groupBox.Controls.Add(this.txtfiles);
this.groupBox.Controls.Add(this.label1);
this.groupBox.ForeColor = System.Drawing.Color.Black;
this.groupBox.Location = new System.Drawing.Point(12, 12);
this.groupBox.Name = "groupBox1";
this.groupBox.Size = new System.Drawing.Size(432, 63);
this.groupBox.TabIndex = 0;
this.groupBox.TabStop = false;
this.groupBox.Text = "批量压缩文件";
this.button.Location = new System.Drawing.Point(383, 24);
this.button.Name = "button1";
this.button.Size = new System.Drawing.Size(43, 23);
this.button.TabIndex = 2;
this.button.Text = "...";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button1_Click);
this.txtfiles.BackColor = System.Drawing.Color.White;
this.txtfiles.Location = new System.Drawing.Point(, 25);
this.txtfiles.Name = "txtfiles";
this.txtfiles.ReadOnly = true;
this.txtfiles.Size = new System.Drawing.Size(, 21);
this.txtfiles.TabIndex =;
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(6, 28);
this.label.Name = "label1";
this.label.Size = new System.Drawing.Size(113, 12);
this.label.TabIndex = 0;
this.label.Text = "选择要压缩的文件:";
this.openFileDialog.InitialDirectory = "c:";
this.openFileDialog.Multiselect = true;
this.button.Location = new System.Drawing.Point(128, 172);
this.button.Name = "button2";
this.button.Size = new System.Drawing.Size(85, 23);
this.button.TabIndex = 3;
this.button.Text = "批量压缩";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button2_Click);
this.groupBox.Controls.Add(this.button3);
this.groupBox.Controls.Add(this.txtfiles2);
this.groupBox.Controls.Add(this.label2);
this.groupBox.ForeColor = System.Drawing.Color.Black;
this.groupBox.Location = new System.Drawing.Point(12, 91);
this.groupBox.Name = "groupBox2";
this.groupBox.Size = new System.Drawing.Size(432, 63);
this.groupBox.TabIndex = 4;
this.groupBox.TabStop = false;
this.groupBox.Text = "批量解压缩文件";
this.button.Location = new System.Drawing.Point(383, 24);
this.button.Name = "button3";
this.button.Size = new System.Drawing.Size(43, 23);
this.button.TabIndex = 2;
this.button.Text = "...";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button3_Click);
this.txtfiles.BackColor = System.Drawing.Color.White;
this.txtfiles.Location = new System.Drawing.Point(128, 24);
this.txtfiles.Name = "txtfiles2";
this.txtfiles.ReadOnly = true;
this.txtfiles.Size = new System.Drawing.Size(248, 21);
this.txtfiles.TabIndex = 1;
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(6, 28);
this.label.Name = "label2";
this.label.Size = new System.Drawing.Size(125, 12);
this.label.TabIndex = 0;
this.label.Text = "选择要解压缩的文件:";
this.button.Location = new System.Drawing.Point(233, 172);
this.button.Name = "button4";
this.button.Size = new System.Drawing.Size(85, 23);
this.button.TabIndex = 5;
this.button.Text = "批量解压缩";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button4_Click);
this.openFileDialog.DefaultExt = "RAR";
this.openFileDialog.Filter = "压缩文件|*.rar;*.zip";
this.openFileDialog.InitialDirectory = "c:";
this.openFileDialog.Multiselect = true;
this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabel,
this.toolStripProgressBar});
this.statusStrip.Location = new System.Drawing.Point(0, 200);
this.statusStrip.Name = "statusStrip1";
this.statusStrip.Size = new System.Drawing.Size(456, 22);
this.statusStrip.TabIndex = 6;
this.statusStrip.Text = "statusStrip1";
this.toolStripStatusLabel.AutoSize = false;
this.toolStripStatusLabel.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel.Size = new System.Drawing.Size(100, 17);
this.toolStripStatusLabel.Text = "执行进度:";
this.toolStripStatusLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.toolStripProgressBar.Name = "toolStripProgressBar1";
this.toolStripProgressBar.Size = new System.Drawing.Size(200, 16);
this.saveFileDialog.Filter = "RAR|*.rar";
this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, 222);
this.Controls.Add(this.statusStrip);
this.Controls.Add(this.button);
this.Controls.Add(this.groupBox);
this.Controls.Add(this.button);
this.Controls.Add(this.groupBox);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "批量解压缩";
this.groupBox.ResumeLayout(false);
this.groupBox.PerformLayout();
this.groupBox.ResumeLayout(false);
this.groupBox.PerformLayout();
this.statusStrip.ResumeLayout(false);
this.statusStrip.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.GroupBox groupBox;
private System.Windows.Forms.TextBox txtfiles;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Button button;
private System.Windows.Forms.OpenFileDialog openFileDialog;
private System.Windows.Forms.Button button;
private System.Windows.Forms.GroupBox groupBox;
private System.Windows.Forms.Button button;
private System.Windows.Forms.TextBox txtfiles;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Button button;
private System.Windows.Forms.OpenFileDialog openFileDialog;
private System.Windows.Forms.StatusStrip statusStrip;
private System.Windows.Forms.ToolStripProgressBar toolStripProgressBar;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel;
private System.Windows.Forms.SaveFileDialog saveFileDialog;
}