惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Visual Studio Blog
宝玉的分享
宝玉的分享
IT之家
IT之家
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
I
InfoQ
B
Blog RSS Feed
T
Threatpost
博客园_首页
M
MIT News - Artificial intelligence
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Know Your Adversary
Know Your Adversary
U
Unit 42
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
月光博客
月光博客
Scott Helme
Scott Helme
T
Tor Project blog
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
S
Schneier on Security
Vercel News
Vercel News
博客园 - Franky
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
L
LangChain Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
The GitHub Blog
The GitHub Blog
雷峰网
雷峰网
Latest news
Latest news
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
S
Security Affairs
A
About on SuperTechFans
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
大猫的无限游戏
大猫的无限游戏
W
WeLiveSecurity
Cisco Talos Blog
Cisco Talos Blog
罗磊的独立博客

博客园 - 阿齐

2013年1月18日 星期五 如何修改Panorama控件的Title 分享好用的截屏软件:SnagIt [转]如何在Windows Server 2012中安装.Net Framework 3.5? 屌丝笔记本玩Windows Phone 8开发(在Windows Server 2012中安装WP8 SDK) 在Ubuntu下的Firefox地址栏点击全选网页地址 Ubuntu下的Firefox差点让我崩溃 如何判断文本文件的编码方式? 使用Base64编码数据量变大33.4% 旧文一篇:7年项目总结 在.Net中将MailMessage保存为本地eml文件 创业之三要素 让MSN Messager(Windows Live Messager)重回托盘 创业的Idea是怎样产生的? 我的恩师白玉玺先生-----转自北京崇文汇通武术网站,作者:柳宗林 记录一把,两个截取HTTP请求的工具 标记一下:两个免费的SVN服务 需要使用计算器才能做的投资,通常都不是好投资 解决“A problem has been encountered while loading the setup components. Canceling setup.”的问题 己丑年正月十九北京随想
使用ICSharpCode.SharpZLib压缩/解压给定的数据
阿齐 · 2010-11-08 · via 博客园 - 阿齐

先介绍一下ICSharpCode.SharpZLib的几个代码入口

  1. ZipOutputStream,提供数据压缩功能代码入口;
  2. ZipInputStream,提供数据解压功能代码入口;
  3. ZipEntry,压缩和解药的数据单元描述。

下面是简单的代码:

/*压缩数据*/

public static byte[] Compress(byte[] content)
{
    using (var memory = new MemoryStream())
    {
        var entry = new ZipEntry("email") { DateTime = DateTime.Now };
        var output = new ZipOutputStream(memory);
        output.SetLevel(9);
        output.PutNextEntry(entry);
        output.Write(content, 0, content.Length);
        output.Flush();
        output.Close();

        return memory.ToArray();
    }
}

/*解压数据*/

public static Dictionary<string, byte[]> Decompress(byte[] content)
{
    var dictionary = new Dictionary<string, byte[]>();
    var input = new MemoryStream(content);
    var zip = new ZipInputStream(input);
    try
    {
        ZipEntry entry;
        while ((entry = zip.GetNextEntry()) != null)
        {
            using (var output = new MemoryStream())
            {
                var buffer = new byte[2048];
                while (true)
                {
                    var size = zip.Read(buffer, 0, buffer.Length);
                    if (size == 0)
                    {
                        output.Flush();
                        dictionary[entry.Name] = output.ToArray();
                    }

                    output.Write(buffer, 0, size);
                }
            }
        }

        return dictionary;
    }
    finally
    {
        zip.Dispose();
        input.Dispose();
    }
}