现在的位置: 首页 > 综合 > 正文

如何使用GZip压缩文件

2012年10月18日 ⁄ 综合 ⁄ 共 5460字 ⁄ 字号 评论关闭

Form1.cs

View Code

 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.IO;
10 using System.IO.Compression;
11
12 namespace GZipFile
13 {
14 public partial class Form1 : Form
15 {
16 public Form1()
17 {
18 InitializeComponent();
19 }
20
21 private void button1_Click(object sender, EventArgs e)
22 {
23 openFileDialog1.Filter = "所有文件(*.*)|*.*";
24 if (openFileDialog1.ShowDialog() == DialogResult.OK)
25 {
26 textBox1.Text = openFileDialog1.FileName;
27 }
28 }
29
30 private void button2_Click(object sender, EventArgs e)
31 {
32
33 if (String.IsNullOrEmpty(textBox1.Text))
34 {
35 MessageBox.Show("请选择源文件!", "信息提示");
36 return;
37 }
38
39 if (String.IsNullOrEmpty(textBox2.Text))
40 {
41 MessageBox.Show("请输入压缩文件名!", "信息提示");
42 return;
43 }
44
45 string str1 = textBox1.Text;
46 string str2 = textBox2.Text.Trim() + ".gzip";
47 byte[] myByte = null;
48 FileStream myStream = null;
49 FileStream myDesStream = null;
50 GZipStream myComStream = null;
51 try
52 {
53 myStream = new FileStream(str1, FileMode.Open, FileAccess.Read, FileShare.Read);
54 myByte = new byte[myStream.Length];
55 myStream.Read(myByte, 0, myByte.Length);
56 myDesStream = new FileStream(str2, FileMode.OpenOrCreate, FileAccess.Write);
57 myComStream = new GZipStream(myDesStream, CompressionMode.Compress, true);
58 myComStream.Write(myByte, 0, myByte.Length);
59 MessageBox.Show("压缩文件完成!");
60 }
61 catch { }
62 finally
63 {
64 myStream.Close();
65 myComStream.Close();
66 myDesStream.Close();
67 }
68 }
69 }
70 }

Form1.Designer.cs

View Code

  1 namespace GZipFile
2 {
3 partial class Form1
4 {
5 /// <summary>
6 /// 必需的设计器变量。
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /// <summary>
11 /// 清理所有正在使用的资源。
12 /// </summary>
13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14 protected override void Dispose(bool disposing)
15 {
16 if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 #region Windows 窗体设计器生成的代码
24
25 /// <summary>
26 /// 设计器支持所需的方法 - 不要
27 /// 使用代码编辑器修改此方法的内容。
28 /// </summary>
29 private void InitializeComponent()
30 {
31 this.button2 = new System.Windows.Forms.Button();
32 this.textBox2 = new System.Windows.Forms.TextBox();
33 this.label2 = new System.Windows.Forms.Label();
34 this.button1 = new System.Windows.Forms.Button();
35 this.groupBox1 = new System.Windows.Forms.GroupBox();
36 this.textBox1 = new System.Windows.Forms.TextBox();
37 this.label1 = new System.Windows.Forms.Label();
38 this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
39 this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
40 this.groupBox1.SuspendLayout();
41 this.SuspendLayout();
42 //
43 // button2
44 //
45 this.button2.Location = new System.Drawing.Point(111, 90);
46 this.button2.Name = "button2";
47 this.button2.Size = new System.Drawing.Size(88, 23);
48 this.button2.TabIndex = 14;
49 this.button2.Text = "压缩";
50 this.button2.UseVisualStyleBackColor = true;
51 this.button2.Click += new System.EventHandler(this.button2_Click);
52 //
53 // textBox2
54 //
55 this.textBox2.Location = new System.Drawing.Point(74, 50);
56 this.textBox2.Name = "textBox2";
57 this.textBox2.Size = new System.Drawing.Size(166, 21);
58 this.textBox2.TabIndex = 17;
59 //
60 // label2
61 //
62 this.label2.AutoSize = true;
63 this.label2.Location = new System.Drawing.Point(6, 54);
64 this.label2.Name = "label2";
65 this.label2.Size = new System.Drawing.Size(65, 12);
66 this.label2.TabIndex = 16;
67 this.label2.Text = "压缩文件:";
68 //
69 // button1
70 //
71 this.button1.Location = new System.Drawing.Point(241, 16);
72 this.button1.Name = "button1";
73 this.button1.Size = new System.Drawing.Size(39, 23);
74 this.button1.TabIndex = 15;
75 this.button1.Text = "选择";
76 this.button1.UseVisualStyleBackColor = true;
77 this.button1.Click += new System.EventHandler(this.button1_Click);
78 //
79 // groupBox1
80 //
81 this.groupBox1.Controls.Add(this.textBox2);
82 this.groupBox1.Controls.Add(this.label2);
83 this.groupBox1.Controls.Add(this.button1);
84 this.groupBox1.Controls.Add(this.textBox1);
85 this.groupBox1.Controls.Add(this.label1);
86 this.groupBox1.Location = new System.Drawing.Point(7, 2);
87 this.groupBox1.Name = "groupBox1";
88 this.groupBox1.Size = new System.Drawing.Size(290, 82);
89 this.groupBox1.TabIndex = 15;
90 this.groupBox1.TabStop = false;
91 //
92 // textBox1
93 //
94 this.textBox1.Location = new System.Drawing.Point(74, 17);
95 this.textBox1.Name = "textBox1";
96 this.textBox1.ReadOnly = true;
97 this.textBox1.Size = new System.Drawing.Size(166, 21);
98 this.textBox1.TabIndex = 14;
99 //
100 // label1
101 //
102 this.label1.AutoSize = true;
103 this.label1.Location = new System.Drawing.Point(6, 21);
104 this.label1.Name = "label1";
105 this.label1.Size = new System.Drawing.Size(65, 12);
106 this.label1.TabIndex = 13;
107 this.label1.Text = "源 文 件:";
108 //
109 // Form1
110 //
111 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
112 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
113 this.ClientSize = new System.Drawing.Size(304, 115);
114 this.Controls.Add(this.button2);
115 this.Controls.Add(this.groupBox1);
116 this.MaximizeBox = false;
117 this.MinimizeBox = false;
118 this.Name = "Form1";
119 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
120 this.Text = "使用GZip压缩文件";
121 this.groupBox1.ResumeLayout(false);
122 this.groupBox1.PerformLayout();
123 this.ResumeLayout(false);
124
125 }
126
127 #endregion
128
129 private System.Windows.Forms.Button button2;
130 private System.Windows.Forms.TextBox textBox2;
131 private System.Windows.Forms.Label label2;
132 private System.Windows.Forms.Button button1;
133 private System.Windows.Forms.GroupBox groupBox1;
134 private System.Windows.Forms.TextBox textBox1;
135 private System.Windows.Forms.Label label1;
136 private System.Windows.Forms.OpenFileDialog openFileDialog1;
137 private System.Windows.Forms.SaveFileDialog saveFileDialog1;
138 }
139 }

抱歉!评论已关闭.