压缩和解压,文件读取练习

压缩

先读取,再写入。只是在写入的时候,需要根据规则写入,如果用用压缩流写入,就成了压缩。

 1  using (FileStream fs = new FileStream("yasuo.txt", FileMode.Open, FileAccess.Read))
 2             {
 3                 byte[] buffer = new byte[1024 * 1024 * 5];
 4                 int readCount = fs.Read(buffer, 0, buffer.Length);
 5                 using (FileStream fWrite = new FileStream("newYaSuo.txt", FileMode.Create, FileAccess.Write))
 6                 {
 7                     using (GZipStream zip = new GZipStream(fWrite, CompressionMode.Compress))
 8                     {
 9                         while (readCount > 0)
10                         {
11                             zip.Write(buffer, 0, readCount);
12                             readCount = fs.Read(buffer, 0, buffer.Length);
13                         }
14                     }
15 
16                 }
17             }
View Code

解压

解压前,先要读取,然后再写入。只是开始读取后,如果直接写入,写入的还是以前压缩的东西,所以在写入前先要解压。 

 1 using (FileStream fs = new FileStream("newYaSuo.txt", FileMode.Open, FileAccess.Read))
 2             {
 3                 byte[] buffer = new byte[1024 * 1024 * 5];
 4                 using (GZipStream zip = new GZipStream(fs, CompressionMode.Decompress))
 5                 {
 6                     int readCount = zip.Read(buffer, 0, buffer.Length);
 7                     using (FileStream fWrite = new FileStream("newJieYaSuo.txt", FileMode.Create, FileAccess.Write))
 8                     {
 9 
10                         while (readCount > 0)
11                         {
12                             fWrite.Write(buffer, 0, readCount);
13                             readCount = zip.Read(buffer, 0, buffer.Length);
14                         }
15 
16                     }
17                 }
18             }
View Code

文件读取练习:人员信息维护

 1 public partial class Form1 : Form
 2     {
 3         public Form1()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         private void Form1_Load(object sender, EventArgs e)
 9         {
10             BindListBox();
11 
12         }
13 
14         private void BindListBox()
15         {
16             List<Person> list = new List<Person>();
17             string line = string.Empty;
18             
19             //读取文件中的数据
20             using (StreamReader sRead = new StreamReader("person.txt", Encoding.Default))
21             {
22                 while ((line=sRead.ReadLine())!=null)
23                 {
24                     string[] strs = line.Split(',');
25                     Person p = new Person();
26                     p.Name = strs[0];
27                     p.Age = Convert.ToInt32(strs[1]);
28                     list.Add(p);
29                 }
30             }
31 
32             lbPersonList.DataSource = list;
33             lbPersonList.DisplayMember = "Name";
34             lbPersonList.ValueMember = "Age";
35         }
36 
37         private void btnAdd_Click(object sender, EventArgs e)
38         {
39             using (StreamWriter sWrite=new StreamWriter("person.txt", true,Encoding.Default))
40             {
41                 sWrite.WriteLine(string.Format("{0},{1}",txtName.Text.Trim(),txtAge.Text.Trim()));
42                 //lbPersonList.Items.Add(txtName.Text.Trim());
43                 
44             }
45             BindListBox();
46         }
47 
48         private void lbPersonList_SelectedIndexChanged(object sender, EventArgs e)
49         {
50             Person p = lbPersonList.SelectedItem as Person;
51             if (p!=null)
52             {
53                 txtName.Text = p.Name;
54                 txtAge.Text = p.Age.ToString();
55             }
56         }
57 
58         private void btnClear_Click(object sender, EventArgs e)
59         {
60             txtAge.Text = "";
61             txtName.Text = "";
62         }
63     }
64 
65     public class Person
66     {
67         public string Name { get; set; }
68 
69         public int Age { get; set; }
70     }
View Code
原文地址:https://www.cnblogs.com/wesley168/p/6636333.html