在C#中用GZipStream压缩与解压数据

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

需要先using

using System.IO;
using System.IO.Compression;

 

// 压缩
        private void Compress(Stream source, Stream dest) {
            using (GZipStream zipStream = new GZipStream(dest, CompressionMode.Compress, true)) {
                byte[] buf = new byte[1024];
                int len;
                while ((len = source.Read(buf, 0, buf.Length)) > 0) {
                    zipStream.Write(buf, 0, len);
                }
            }
        }

        // 解压
        private void Decompress(Stream source, Stream dest) {
            using (GZipStream zipStream = new GZipStream(source, CompressionMode.Decompress, true)) {
                byte[] buf = new byte[1024];
                int len;
                while ((len = zipStream.Read(buf, 0, buf.Length)) > 0) {
                    dest.Write(buf, 0, len);
                }
            }
        }

示例:

internal void ExportPackage(string filename, string expression, Stream recordStream, string postscript) {
            short version = 1;
            using (MemoryStream stream = new MemoryStream()) {
                // 写入版本号
                StoreBase.WriteValue(stream, version);               
                // 写入数据实体
                byte[] dataBytes = ObjectHelper.Serialize(this.ActiveObject);
                StoreBase.WriteValue(stream, dataBytes);               
                // 写入函数
                StoreBase.WriteValue(stream, _functionDeclares.Count);
                foreach (string typeName in _functionDeclares.Keys) {
                    StoreBase.WriteValue(stream, typeName);
                    StoreBase.WriteValue(stream, _functionDeclares[typeName].Count);

                    foreach (FuncDescrption func in _functionDeclares[typeName]) {
                        StoreBase.WriteValue(stream, func.FuncName);
                        StoreBase.WriteValue(stream, func.NameParams);
                        StoreBase.WriteValue(stream, func.Descrption);
                    }
                }
                // 写入表达式
                StoreBase.WriteValue(stream, expression);
                // 写入执行历史
                byte[] recordBytes = new byte[recordStream.Length];
                recordStream.Read(recordBytes, 0, recordBytes.Length);
                StoreBase.WriteValue(stream, recordBytes);
                // 写入调试附言
                StoreBase.WriteValue(stream, postscript);

                // 压缩
                stream.Position = 0;
                using (FileStream fs = new FileStream(filename, FileMode.Create)) {
                    Compress(stream, fs);
                }
            }
        }

        internal void ImportPackage(string filename, LoadPackageEventHandler handler) {
            short version = 1;
            using (MemoryStream stream = new MemoryStream()) {
                // 解压缩
                using (FileStream fs = new FileStream(filename, FileMode.Open)) {
                    Decompress(fs, stream);
                }
                stream.Position = 0;

                // 读取版本号
                version = StoreBase.ReadValue(stream, version);
                // 读取数据实体
                byte[] dataBytes = StoreBase.ReadValue(stream, new byte[0]);
                this.ActiveObject = ObjectHelper.Deserialize(dataBytes);
                // 读取函数
                _functionDeclares.Clear();
                int typeCount = StoreBase.ReadValue(stream, (int)0);
                for (int i = 0; i < typeCount; i++) {
                    string typeName = StoreBase.ReadValue(stream, string.Empty);
                    int funcCount = StoreBase.ReadValue(stream, (int)0);

                    string funcName = string.Empty;
                    string nameParams = string.Empty;
                    string descrption = string.Empty;
                    for (int j = 0; j < funcCount; j++) {
                        funcName = StoreBase.ReadValue(stream, funcName);
                        nameParams = StoreBase.ReadValue(stream, nameParams);
                        descrption = StoreBase.ReadValue(stream, descrption);

                        this.AddFunctionDeclare(typeName, funcName, nameParams, descrption);
                    }
                }
                // 读取表达式
                string expression = StoreBase.ReadValue(stream, string.Empty);
                // 读取执行历史
                byte[] recordBytes = StoreBase.ReadValue(stream, new byte[0]);
                MemoryStream recordStream = new MemoryStream();
                recordStream.Write(recordBytes, 0, recordBytes.Length);
                recordStream.Position = 0;
                // 读取调试附言
                string postscript = StoreBase.ReadValue(stream, string.Empty);

                handler(expression, recordStream, postscript);
            }
        }

C#数据及文件的压缩解压 介绍System.IO.Compression命名空间中关于数据及文件压缩解压的类库及其方法 阅读详情

相关推荐

GzipStream流读写压缩数据

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.IO.Compression; namespace ConsoleApplication21 { class Program {

2820

C#使用GZipStream解压数据文件的方法

主要介绍了C#使用GZipStream解压数据文件的方法,实例分析了C#GZipStream方法的原理使用技巧,需要的朋友可以参考下

如何压缩多个文件\文件夹(GZipStream and C#)

在.Net Framework 2.0 中添加了System.IO.Compression 类来实现对文件/文件夹的压缩/解压(GZipStream方法),包括文档,代码,类文件

C#GZipStream压缩

第一次接触,对于GZIPStream的两个方法有点疑惑: 该对象的Write方法原型: void GZipStream.Write(byte[] array,int offset,int count) 从指定的字节数组中将压缩的字节写入基础流 这样看解释是先要调用这个方法,通过GZipStream读取后在压缩吗? 该对象的Read方法原型: int GZipStream.Rea...

oooooomit 博客 9752

c# 利用 GZipStream 压缩解压缩文件(所有类型的文档)

c# 利用 GZipStream 压缩解压缩文件(所有类型的文档)

qq_52077555的博客 2552

java gzip xml_Java GZIPInputStreamGZIPOutputStream的使用示例

Java Gzip是一个压缩率非常高的类,它可以将Gzip文件通过GZIPInputStream类读取过来解压缩,也可以将字符串通过GZIPOutputStream类写成Gzip文件,当然了,我们可以把字符串压缩byte[]数组之后网络传输,这样速度会比没有Gzip压缩的要快得多,请看之前的案例:Java GZIP压缩解压缩字符串下面我们将来学习如何使用Java GZIPInputStrea...

weixin_33549115的博客 657

C# 关于GZipStream压缩解压数据丢失问题

网上有很多关于GZipStream压缩解压缩的例子,开始测试没问题,但我不习惯直接用别人的代码,少不了左改右改。改来改去问题来了,数据解压缩还原不出来了。看人家的例子和我的也没差哪去啊,为什么我的就还原不出来了。翻来覆去找原因,发现有的压缩时候就丢失了,有的是还原时候出错了。遇到压缩比较少的内容时候,还读不出压缩内容。难道GZipStream压缩内容大小有限制 0.0  绝对不可能的,继续找原

leedoom的专栏 5073

php gzipstream,C#_C#使用GZipStream解压数据文件的方法,本文实例讲述了C#使用GZipStream - phpStudy...

C#使用GZipStream解压数据文件的方法本文实例讲述了C#使用GZipStream解压数据文件的方法。分享给大家供大家参考。具体分析如下:GZipStream用于从一个流读取数据写入到另一个流,GZipStream不能写入到其它的资源,比如文件或者内存,只能从流到流。GZipStream使用的一般流程如下:打开一个现有的文件打开/创建输出文件创建GZipStream对象逐字节读源文件,并...

weixin_29128689的博客 204

C# 使用GZipStream实现压缩解压

概述之前做项目,涉及到存入到数据库或者http传输的数据量比较大,这个时候,就需要考虑在存入数据库或者发送传输之前,将数据压缩下,当从数据库中取出时,再解压还原数据。特地找了下发现有GZi...

zls365365的博客 1954

使用GZipStream实现压缩解压

概述之前做项目,涉及到存入到数据库或者http传输的数据量比较大,这个时候,就需要考虑在存入数据库或者发送传输之前,将数据压缩下,当从数据库中取出时,再解压还原数据。特地找了下发现有GZi...

dotNET跨平台 405

Win8 Metro App GZipStream 用不明白了有高手否? 压缩数据无法解压

public static string CompressGzip(string st) { List<byte> li = new List<byte>(); using (MemoryStream msf = new MemoryStream(Encoding.UTF8.GetBytes(st))) ...

weixin_30724853的博客 96

用.NET 2.0压缩/解压封装的类

主要功能是可以用来减轻网络数据的传输数据量,using System;using System.Collections.Generic;using System.Collections;using System.Text;using System.IO;using System.IO.Compression;namespace CompressionService{    public c

关注技术 1126

C#文件流进行压缩解压缩的代码

下面代码是关于C#文件流进行压缩解压缩的代码,应该能对码农们也有帮助。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.IO.Compression;...

weixin_44085254的博客 501

C#数据压缩解压

记得using System.IO.Compression; 将byte数组进行压缩 public static byte[] Compress(byte[] inputBytes) { using (MemoryStream outStream = new MemoryStream()) { ...

学习记录 746

C#压缩解压缩字节(GZip)

标题:C#压缩解压缩字节(GZip的使用)作用:此类在 .NET Framework 2.0 版中是新增的。提供用于压缩解压缩流的方法和属性。定义:表示 GZip 数据格式,它使用无损压缩解压缩文件的行业标准算法。这种格式包括一个检测数据损坏的循环冗余校验值。GZip 数据格式使用的算法 DeflateStream 类的算法相同,但它可以扩展以使用其他压缩格式。这种格式可以通过不涉及专利使...

weixin_30493321的博客 832

C#压缩解压缩流类 GZipStream 的使用

C#压缩解压缩流类 GZipStream 的使用   在使用 GZipStream 进行压缩的时候, 在最后必须调用 Close()方法, 否则会发现解压缩后少一个字节, 当压缩的文件小于4kb时, 解压缩到文件长度为0.   下面为一个完整的压缩解压缩文件的代码, 以做参考: private void button1_Click(object s

killcwd的专栏 3367

使用.Net自带的GZipStream进行流压缩解压

1 using System.IO; 2 using System.IO.Compression; 3 using System.Text; 4 5 namespace CS.Utility 6 { 7 /// <summary> 8 /// 压缩解压缩处理 9 /// </summary> 10 ...

weixin_30394633的博客 369

C#字符串压缩解压

1 public static string CompressString(string str) 2 { 3 var compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str); 4 var compressAfterByte = Compress(compressBeforeByte); ...

weixin_34272308的博客 470
上一篇: 分解复杂IronPython表达式为简单表达式
下一篇: 云层之上的天空
西南三少
博客等级 码龄19年 33粉丝 60原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值