使用GZipStream类在C#中进行数据压缩和解压缩操作

GZipStream是.NET中用于实现GZip算法的类。GZip是一种用于压缩和解压缩数据的算法,广泛应用于文件压缩和网络传输等场景

GZip算法简介

GZip是基于DEFLATE算法的压缩方法,由Jean-Loup Gailly和Mark Adler创建,最初用于Unix系统中的gzip工具。GZip主要用于减少文件大小以便更高效地存储和传输数据

GZipStream类

在.NET中,GZipStream类位于System.IO.Compression命名空间中,提供了使用GZip算法进行数据压缩和解压缩的功能。GZipStream类继承自Stream类,因此可以与其他流操作类(如MemoryStream和FileStream)一起使用

实例

这段代码展示了如何使用GZipStream类在C#中进行数据压缩和解压缩操作。它展示了如何将一个字符串转换为字节数组,使用GZip算法进行压缩,然后再将其解压缩回原始字符串。通过这个示例,我们可以看到压缩算法在处理具有重复模式的数据时是如何工作的,以及如何利用MemoryStream和GZipStream类实现压缩和解压缩功能。这对于需要减少存储空间或提高网络传输效率的应用非常有用

cs 复制代码
using System;
using System.IO;
using System.IO.Compression;
using System.Text;

class Program
{
    static void Main()
    {
        // 原始数据,可以是任意字节数组
        string originalData = "This is some text that we want to compress using GZip. " +
                              "This is some text that we want to compress using GZip. " +
                              "This is some text that we want to compress using GZip. " +
                              "This is some text that we want to compress using GZip. " +
                              "This is some text that we want to compress using GZip.";
        byte[] originalBytes = Encoding.UTF8.GetBytes(originalData);

        // 调用 Compress 方法压缩数据
        byte[] compressedBytes = Compress(originalBytes);

        // 输出原始数据和压缩后的数据大小
        Console.WriteLine($"Original Size: {originalBytes.Length} bytes");
        Console.WriteLine($"Compressed Size: {compressedBytes.Length} bytes");

        // 调用 Decompress 方法解压缩数据
        byte[] decompressedBytes = Decompress(compressedBytes);
        string decompressedData = Encoding.UTF8.GetString(decompressedBytes);

        // 验证解压缩后的数据是否与原始数据相同
        Console.WriteLine($"Decompressed Data: {decompressedData}");
    }

    public static byte[] Compress(byte[] bytes)
    {
        using (var memoryStream = new MemoryStream())
        {
            using (var gzipStream = new GZipStream(memoryStream, CompressionLevel.Optimal))
            {
                gzipStream.Write(bytes, 0, bytes.Length);
            }
            return memoryStream.ToArray();
        }
    }

    public static byte[] Decompress(byte[] bytes)
    {
        using (var memoryStream = new MemoryStream(bytes))
        {
            using (var outputStream = new MemoryStream())
            {
                using (var decompressStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    decompressStream.CopyTo(outputStream);
                }
                return outputStream.ToArray();
            }
        }
    }
}

运行结果:

cs 复制代码
Original Size: 274 bytes
Compressed Size: 76 bytes
Decompressed Data: This is some text that we want to compress using GZip. This is some text that we want to compress using GZip. This is some text that we want to compress using GZip. This is some text that we want to compress using GZip. This is some text that we want to compress using GZip.

参考

相关推荐
追逐时光者1 小时前
精选 4 款基于 .NET 开源、功能强大的 Windows 系统优化工具
后端·.net
TF男孩1 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
AAA修煤气灶刘哥2 小时前
别让Redis「歪脖子」!一次搞定数据倾斜与请求倾斜的捉妖记
redis·分布式·后端
AAA修煤气灶刘哥2 小时前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
你的人类朋友3 小时前
什么是API签名?
前端·后端·安全
昵称为空C5 小时前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
架构师沉默5 小时前
设计多租户 SaaS 系统,如何做到数据隔离 & 资源配额?
java·后端·架构
RoyLin6 小时前
TypeScript设计模式:适配器模式
前端·后端·node.js
该用户已不存在6 小时前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
Moonbit6 小时前
MoonBit 正式加入 WebAssembly Component Model 官方文档 !
前端·后端·编程语言