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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Last Watchdog
The Last Watchdog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Troy Hunt's Blog
L
LINUX DO - 最新话题
C
Check Point Blog
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
S
Security @ Cisco Blogs
IT之家
IT之家
T
The Exploit Database - CXSecurity.com
The GitHub Blog
The GitHub Blog
D
Docker
Engineering at Meta
Engineering at Meta
AWS News Blog
AWS News Blog
S
Security Affairs
U
Unit 42
P
Palo Alto Networks Blog
V
Visual Studio Blog
Y
Y Combinator Blog
D
DataBreaches.Net
Forbes - Security
Forbes - Security
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
Security Latest
Security Latest
aimingoo的专栏
aimingoo的专栏
Simon Willison's Weblog
Simon Willison's Weblog
A
Arctic Wolf
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hacker News: Front Page
博客园 - 司徒正美
博客园 - Franky
宝玉的分享
宝玉的分享
TaoSecurity Blog
TaoSecurity Blog
Latest news
Latest news
Scott Helme
Scott Helme
MongoDB | Blog
MongoDB | Blog
量子位
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Cisco Blogs
P
Privacy International News Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - skyfei

Xcode 文档注释方法 查看iOS模拟器应用的沙箱文件 Show in Finder OC代码 C# x86应用x64系统上读取x64位应用的注册表 Make webclient support upload the large file which are larger than 1G Shortcut key for WPF get Android information with adb, the build version Decodes a QuotedPrintable encoded string C# USB Detection - winform and WPF [转] 线程同步 C# 常见面试题(2) 转:C# Interview Questions Openxml: 导出excel 设置 cell的格式 - skyfei OpenXML: excel 插入BarChart图表 OpenXML: Asp.net利用OpenXML 导出Excel. TRIGGERS :Cannot use text, ntext, or image columns in the 'inserted' and ' deleted' tables. 'String or binary data would be truncated' error message .net Create Excel 2007 file with open xml C#中文和UNICODE字符转换方法 - skyfei - 博客园
利用.Net Framework2.0 zip压缩、解压 string 数据
skyfei · 2008-08-07 · via 博客园 - skyfei

利用System.IO.Compression 压缩,解压文件网上能搜索到很多文档了, 但对字符串直接压缩还是很少, 这种压缩还是在webservice返回大量数据时还是用用途的

看code吧:

using System.Text;

using System.IO.Compression;

public  static string CompressString(string str)
      {
          byte[] buffer = Encoding.Unicode.GetBytes(str);
          MemoryStream ms = new MemoryStream();

          GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
          zip.Write(buffer, 0, buffer.Length);
          zip.Close();
          ms.Position = 0;
          byte[] zipBuffer = new byte[ms.Length];
          ms.Read(zipBuffer, 0, zipBuffer.Length);
          ms.Close();
          string zipstr =Convert.ToBase64String(zipBuffer);
          return zipstr;
      }


      public static string DeCompressString(string str)
      {
          byte[] buffer = Convert.FromBase64String(str);
          MemoryStream ms = new MemoryStream();
          ms.Write(buffer, 0, buffer.Length);
          ms.Position = 0;
          GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true);
           byte[] zipBuffer = new byte[1024];
           MemoryStream ms2 = new MemoryStream();
           while (true)
           {
               int bytesRead = zip.Read(zipBuffer, 0, zipBuffer.Length);
               if (bytesRead == 0)
               {
                   break;
               }
               ms2.Write(zipBuffer, 0, bytesRead); 
           }
          zip.Close();

          string zipstr = Encoding.Unicode.GetString(ms2.ToArray(), 0, (int)ms2.Length);
          return zipstr;
      }

特别提示, 如果压缩方法直接返回byte[], 会节省一些空间, 而且byte[]可以直接存储到sql server binary数据类型里, 但考虑到webservcie传输, 我又把他转成string, 用Base64转成string会多花销一些空间, 但相比为压缩的string, 已经小了很多了。 如果用别的编码(比如utf8, unioncode) 会产生一些问题, 我想是因为一些特殊字符处理的问题吧,网上搜索base64编码是解决字符是7位的带符号位, 可能是这个问题