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

推荐订阅源

NISL@THU
NISL@THU
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threatpost
Cloudbric
Cloudbric
H
Heimdal Security Blog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tor Project blog
A
Arctic Wolf
W
WeLiveSecurity
SecWiki News
SecWiki News
S
Security Affairs
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
K
Kaspersky official blog
P
Privacy & Cybersecurity Law Blog
AWS News Blog
AWS News Blog
T
The Exploit Database - CXSecurity.com
V2EX - 技术
V2EX - 技术
AI
AI
Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
有赞技术团队
有赞技术团队
C
Cybersecurity and Infrastructure Security Agency CISA
腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
H
Hacker News: Front Page
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
The Hacker News
The Hacker News
阮一峰的网络日志
阮一峰的网络日志
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
月光博客
月光博客
博客园 - 【当耐特】
Recorded Future
Recorded Future
O
OpenAI News
Hacker News: Ask HN
Hacker News: Ask HN
Scott Helme
Scott Helme
N
News and Events Feed by Topic
Help Net Security
Help Net Security
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
小众软件
小众软件

博客园 - 阿齐

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();
    }
}