使用 WinForm 窗口程序,实现字符串压缩和解压缩功能

本示例展示如何使用 C# WinForm 开发一个简单的字符串压缩和解压缩工具,利用 GZipStream 实现压缩和解压缩功能。

程序功能:

  • 在一个文本框中输入需要压缩的字符串。
  • 点击“压缩”按钮,将字符串压缩并显示在另一个文本框中。
  • 点击“解压缩”按钮,将压缩后的字符串解压缩并显示在另一个文本框中。

代码实现:

using System;
using System.Windows.Forms;
using System.IO.Compression;

namespace WinFormApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCompress_Click(object sender, EventArgs e)
        {
            string inputString = txtInput.Text;
            byte[] inputData = System.Text.Encoding.UTF8.GetBytes(inputString);

            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                using (System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress))
                {
                    sw.Write(inputData, 0, inputData.Length);
                }

                byte[] compressedData = ms.ToArray();
                string outputString = Convert.ToBase64String(compressedData);

                txtOutput.Text = outputString;
            }
        }

        private void btnDecompress_Click(object sender, EventArgs e)
        {
            string inputString = txtInput.Text;
            byte[] inputData = Convert.FromBase64String(inputString);

            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(inputData))
            {
                using (System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
                {
                    byte[] buffer = new byte[1024];
                    System.IO.MemoryStream outputMs = new System.IO.MemoryStream();
                    int bytesRead = 0;

                    do
                    {
                        bytesRead = sr.Read(buffer, 0, buffer.Length);
                        outputMs.Write(buffer, 0, bytesRead);
                    } while (bytesRead > 0);

                    byte[] outputData = outputMs.ToArray();
                    string outputString = System.Text.Encoding.UTF8.GetString(outputData);

                    txtOutput.Text = outputString;
                }
            }
        }
    }
}

界面设计:

在窗体上添加两个文本框(分别为 txtInputtxtOutput),以及两个按钮(分别为 btnCompressbtnDecompress)。

代码说明:

  1. btnCompress_Click 事件处理程序:

    • 获取输入文本框的内容。
    • 使用 System.Text.Encoding.UTF8.GetBytes 将字符串转换为字节数组。
    • 使用 GZipStream 对象进行压缩。
    • 使用 Convert.ToBase64String 将压缩后的字节数组转换为 Base64 字符串。
    • 将 Base64 字符串输出到输出文本框。
  2. btnDecompress_Click 事件处理程序:

    • 获取输入文本框的内容(Base64 字符串)。
    • 使用 Convert.FromBase64String 将 Base64 字符串转换为字节数组。
    • 使用 GZipStream 对象进行解压缩。
    • 使用 System.Text.Encoding.UTF8.GetString 将解压缩后的字节数组转换为字符串。
    • 将字符串输出到输出文本框。

运行结果:

  • 在输入文本框中输入需要压缩的字符串。
  • 点击“压缩”按钮,输出文本框中将显示压缩后的 Base64 字符串。
  • 点击“解压缩”按钮,输出文本框中将显示解压缩后的字符串。
C# WinForm 字符串压缩与解压缩 - 使用 GZipStream

原文地址: https://www.cveoy.top/t/topic/lq2c 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录