C#中将list进行序列化并使用SharpZipLib进行压缩

场景

ICSharpCode.SharpZipLib.dll 下载:

https://download.csdn.net/download/badao_liumang_qizhi/11586902

实现

新建Winform窗体程序。

打开资源管理器-引用-右键-添加--浏览

选择刚才上面下载的ICSharpCode.SharpZipLib.dll,点击确定。

 

右击项目-添加-类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProtoBufTest
{
    [Serializable]
    class Person
    {

        public Person(int id ,string password) {
            this.Id = id;
            this.Password = password;
        }
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        private string password;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }
    }
}

在窗体上拖拽一个Button按钮,然后双击进其点击事件。

 private void button7_Click(object sender, EventArgs e)
        {
            DateTime begin = DateTime.Now;
            Console.WriteLine("二进制压缩开始" + begin.ToString("yyyy-MM-dd HH:mm:ss"));
            //初始化数据
            for (int i = 0; i < 10000; i++)
            {
                personList.Add(new Person(i,"密码"+i));
            }
            try
            {
                //创建内存流对象
                MemoryStream ms = new MemoryStream();                 
                //序列化对象
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(ms, this.personList);
                //把内存流对象写入字节数组                  
                byte[] buffer = ms.ToArray();
                //关闭内存流对象
                ms.Close();
                //释放资源                                 
                ms.Dispose();                                                            
               FileStream fs = File.Create(@"E:\testdata1\Record3.zip");//创建文件
                //创建zip输出流
               ZipOutputStream zipOutputStream = new ZipOutputStream(fs, buffer.Length);
                //ZipEntry用于表示Zip文件条目 --将会在压缩文件中创建Record3.data文件
               ZipEntry entry = new ZipEntry("Record3.data");
                //将其放进压缩文件中
                zipOutputStream.PutNextEntry(entry);
                //将字节数组写入文件
                zipOutputStream.Write(buffer, 0, buffer.Length);
                zipOutputStream.Finish();
                zipOutputStream.Close();
                zipOutputStream.Dispose();
                //关闭流
                fs.Close();
                //释放对象
                fs.Dispose();                                                          
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            DateTime end = DateTime.Now;
            TimeSpan ts = end - begin;
            Console.WriteLine("二进制压缩结束" + end.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine("共花费 " + ts.TotalSeconds);
        }

具体使用见注释。

运行效果

 

将其解压

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值