[C#] C#文件流进行压缩和解压缩 →→→→→进入此内容的聊天室

来自 , 2019-03-18, 写在 C#, 查看 127 次.
URL http://www.code666.cn/view/e3f3064a
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.IO.Compression;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace 文件流
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void button1_Click(object sender, EventArgs e)
  23.         {
  24.             string s = "好好好好好好好好好好好好好好好好好好好好好哈好好好好好啊";
  25.             for (int i = 0; i < 10; i++)
  26.             {
  27.                 s += s;
  28.             }
  29.             using (FileStream fs=File.OpenWrite(@"c:\1.txt"))
  30.             {
  31.                 using (GZipStream zipStream=new GZipStream(fs,CompressionMode.Compress))
  32.                 {
  33.                     byte[] bytes = Encoding.UTF8.GetBytes(s);
  34.                     zipStream.Write(bytes,0,bytes.Length);
  35.                    MessageBox.Show("压缩成功!");          
  36.  
  37.                 }
  38.             }
  39.        
  40.         }
  41.  
  42.         private void button2_Click(object sender, EventArgs e)
  43.         {
  44.             using (FileStream fs=File.OpenRead(@"c:\1.txt"))
  45.             {
  46.                 using (GZipStream zipStream=new GZipStream(fs,CompressionMode.Decompress))
  47.                 {
  48.                     using (FileStream fs1=File.OpenWrite(@"c:\upzip.txt"))
  49.                     {
  50.                         int bytesRead;
  51.                         byte[] bytes=new byte[1024];
  52.                         while((bytesRead=zipStream.Read(bytes,0,bytes.Length))>0)
  53.                         {
  54.                             fs1.Write(bytes,0,bytesRead);
  55.                         }
  56.  
  57.                         MessageBox.Show("解压成功!");        
  58.  
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.     }
  64. }
  65. //csharp/5823

回复 "C#文件流进行压缩和解压缩"

这儿你可以回复上面这条便签

captcha