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

推荐订阅源

人人都是产品经理
人人都是产品经理
MyScale Blog
MyScale Blog
Y
Y Combinator Blog
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
N
News and Events Feed by Topic
B
Blog RSS Feed
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 【当耐特】
N
Netflix TechBlog - Medium
博客园 - 叶小钗
B
Blog
Vercel News
Vercel News
T
Tenable Blog
T
The Exploit Database - CXSecurity.com
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
F
Fortinet All Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Microsoft Security Blog
Microsoft Security Blog
S
Securelist
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Palo Alto Networks Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
D
DataBreaches.Net
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
Martin Fowler
Martin Fowler
G
GRAHAM CLULEY
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
C
CERT Recently Published Vulnerability Notes
L
LangChain Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Check Point Blog
A
About on SuperTechFans
W
WeLiveSecurity
The GitHub Blog
The GitHub 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位的带符号位, 可能是这个问题