1.             //引用
  2.             using System.IO.Compression;
  3.             //解压缩类
  4.             GZipStream
  5.             //解压缩实例
  6.             ......
  7.             HttpWebResponse httpRequest = (HttpWebResponse)httpLogin.GetResponse();
  8.             Stream HttpResStream= httpRequest.GetResponseStream();
  9.             GZipStream gzip = new GZipStream(HttpResStream, CompressionMode.Decompress) ;
  10.             //对解压缩后的字符串信息解析
  11.            while ((len = gzip.Read(bytes, , bytes.Length)) > )
  12.             {
  13.                line =  System.Text.Encoding.Default.GetString(bytes);
  14.             }  http://msdn.microsoft.com/zh-cn/library/system.io.compression.gzipstream(v=vs.80).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1  using System;
  15. using System.IO;
  16. using System.IO.Compression;
  17.  
  18. public class GZipTest
  19. {
  20.     public static int ReadAllBytesFromStream(Stream stream, byte[] buffer) 
  21.     {
  22.     // Use this method is used to read all bytes from a stream.
  23.     int offset = ;
  24.     int totalCount = ;
  25.         while (true) 
  26.         {
  27.         int bytesRead = stream.Read(buffer, offset, ); 
  28.             if ( bytesRead == ) 
  29.             {
  30.             break; 
  31.             }
  32.     offset += bytesRead;
  33.     totalCount += bytesRead; 
  34.         }
  35.     return totalCount;
  36.     } 
  37.  
  38.     public static bool CompareData(byte[] buf1, int len1, byte[] buf2, int len2) 
  39.     {
  40.         // Use this method to compare data from two different buffers.
  41.         if (len1 != len2) 
  42.         { 
  43.         Console.WriteLine("Number of bytes in two buffer are different {0}:{1}", len1, len2);
  44.         return false;
  45.         }
  46.  
  47.         for ( int i= ; i< len1; i++) 
  48.         {
  49.             if ( buf1[i] != buf2[i]) 
  50.             {
  51.             Console.WriteLine("byte {0} is different {1}|{2}", i, buf1[i], buf2[i]);
  52.             return false;
  53.             }
  54.         }
  55.     Console.WriteLine("All bytes compare.");
  56.     return true; 
  57.     }
  58.  
  59.     public static void GZipCompressDecompress(string filename)
  60.     {
  61.     Console.WriteLine("Test compression and decompression on file {0}", filename);
  62.     FileStream infile;
  63.         try
  64.         {
  65.         // Open the file as a FileStream object.
  66.         infile = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
  67.         byte[] buffer = new byte[infile.Length];
  68.         // Read the file to ensure it is readable.
  69.         int count = infile.Read(buffer, , buffer.Length);
  70.             if ( count != buffer.Length) 
  71.             {
  72.             infile.Close();
  73.             Console.WriteLine("Test Failed: Unable to read data from file"); 
  74.             return;
  75.             }
  76.         infile.Close();
  77.         MemoryStream ms = new MemoryStream();
  78.         // Use the newly created memory stream for the compressed data.
  79.         GZipStream compressedzipStream = new GZipStream(ms , CompressionMode.Compress, true);
  80.         Console.WriteLine("Compression");
  81.         compressedzipStream.Write(buffer, , buffer.Length);
  82.         // Close the stream.
  83.         compressedzipStream.Close();
  84.         Console.WriteLine("Original size: {0}, Compressed size: {1}", buffer.Length, ms.Length);
  85.  
  86.         // Reset the memory stream position to begin decompression.
  87.         ms.Position = ;
  88.         GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress);
  89.         Console.WriteLine("Decompression");
  90.         byte[] decompressedBuffer = new byte[buffer.Length + ];
  91.         // Use the ReadAllBytesFromStream to read the stream.
  92.         int totalCount = GZipTest.ReadAllBytesFromStream(zipStream, decompressedBuffer);
  93.         Console.WriteLine("Decompressed {0} bytes", totalCount);
  94.  
  95.         if( !GZipTest.CompareData(buffer, buffer.Length, decompressedBuffer, totalCount) ) 
  96.         {
  97.         Console.WriteLine("Error. The two buffers did not compare.");
  98.         }
  99.     zipStream.Close(); 
  100.         } // end try
  101.         catch (InvalidDataException)
  102.         {
  103.             Console.WriteLine("Error: The file being read contains invalid data.");
  104.         }
  105.         catch (FileNotFoundException)
  106.         {
  107.             Console.WriteLine("Error:The file specified was not found.");
  108.         }
  109.         catch (ArgumentException)
  110.         {
  111.             Console.WriteLine("Error: path is a zero-length string, contains only white space, or contains one or more invalid characters");
  112.         }
  113.         catch (PathTooLongException)
  114.         {
  115.             Console.WriteLine("Error: The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.");
  116.         }
  117.         catch (DirectoryNotFoundException)
  118.         {
  119.             Console.WriteLine("Error: The specified path is invalid, such as being on an unmapped drive.");
  120.         }
  121.         catch (IOException)
  122.         {
  123.             Console.WriteLine("Error: An I/O error occurred while opening the file.");
  124.         }
  125.         catch (UnauthorizedAccessException)
  126.         {
  127.             Console.WriteLine("Error: path specified a file that is read-only, the path is a directory, or caller does not have the required permissions.");
  128.         }
  129.         catch (IndexOutOfRangeException)
  130.         {
  131.             Console.WriteLine("Error: You must provide parameters for MyGZIP.");
  132.         }
  133.     }
  134.     public static void Main(string[] args)
  135.     {
  136.         string usageText = "Usage: MYGZIP <inputfilename>";
  137.         //If no file name is specified, write usage text.
  138.         if (args.Length == )
  139.         {
  140.             Console.WriteLine(usageText);
  141.         }
  142.         else
  143.         {
  144.             if (File.Exists(args[]))
  145.                 GZipCompressDecompress(args[]);
  146.         }
  147.     }
  148. }     

c# gzip解压缩的更多相关文章

  1. Windows API方式直接调用C#的DLL,支持多音字转拼音、Gzip解压缩、公式计算(VBA、C++、VB、Delphi甚至java都可以)

    原始链接 https://www.cnblogs.com/Charltsing/p/DllExport.html 这两年,我在VBA应用方面一直有几大痛点:1.多音字转拼音:2.64位下的GZIP解压 ...

  2. VB6之借助zlib实现gzip解压缩

    这是个简版的,可以拿来做下网页gzip的解压缩,整好我的webserver还不支持这个,有时间了就加上. zlib.dll下载请点击我! 模块zlib.bas的代码如下: 'code by lichm ...

  3. http gzip 解压缩

    var sContentEncoding = httpRespone.Headers["Content-Encoding"]; if(sContentEncoding == &qu ...

  4. c语言使用zlib实现文本字符的gzip压缩与gzip解压缩

    网络上找到的好多方法在解压缩字符串的时候会丢失字符,这里是解决方法: http://stackoverflow.com/questions/21186535/compressing-decompres ...

  5. AXIS2调用web service,返回结果用GZIP解压缩

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

  6. C#使用Gzip解压缩完整读取网页内容

    using System; using System.Threading; using System.Text; using System.Text.RegularExpressions; using ...

  7. 对数据进行GZIP压缩或解压缩

    /** * 对data进行GZIP解压缩 * @param data * @return * @throws Exception */ public static String unCompress( ...

  8. java GZIP压缩与解压缩

    1.GZIP压缩 public static byte[] compress(String str, String encoding) { if (str == null || str.length( ...

  9. AIX 文件 打包 与 压缩 tar gzip compress 的使用

    今天在Aix用tar -cvf 备份,打成tar包,占有硬盘空间过大,没有压缩比, 尝试使用tar -zcvf  linux系统下可以用-z 命令 (z 用gzip来压缩/解压缩文件,加上该选项后可以 ...

随机推荐

  1. js数组&&字符串&&定时器2

    一.系统时间对象Date 方法 描述 Date() 返回当日的日期和时间. getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31). getDay() 从 Date 对象返回一周 ...

  2. Git之路--2

  3. Git 常用配置和使用

    Git:是一个分布式的源代码管理工具,Linux内核的代码就是用Git管理的所以它很强,也很快, 和 Vss/SVN比起来 本地Git初始化配置及其使用: 1. 初始化本地Git库:打开Git Bas ...

  4. mysql insert语句错误问题解决

    好久没有复习数据库了,竟然忘记了mysql中的关键字(保留字),导致今天一晚上都在查找sql语句错误,特此记录此错误,教训啊. 我在mysql数据库中有一个名为order 的表,啊啊啊啊啊,为啥我给他 ...

  5. 20151217jqueryUI学习笔记

    工具提示(tooltip),是一个非常实用的 UI.它彻底扩展了 HTML 中的 title 属性,让提示更加丰富,更加可控制,全面提升了用户体验.一. 调用 tooltip()方法在调用 toolt ...

  6. 在 ServiceModel 客户端配置部分中,找不到引用协定“PmWs.PmWebServiceSoap”的默认终结点元素

    System.Exception: ConfigManager.LoadConfigurationFromDb ServiceFactory.GetPmWebService 在 ServiceMode ...

  7. spring定时任务的配置

    定时任务配置分为三个步骤: 1.定义任务 2.任务执行策略配置 3.启动任务 1.定义任务 <!--要定时执行的方法--> <bean id="testTaskJob&qu ...

  8. jsp文件怎么打开呢

    jsp是一种嵌入式网页脚本,正常情况下可以用记事本等文本工具直接打开,也可用DREAMWEAVER等网页设计工具友好编辑.不过这样只能看到程序的源代码.当然,我们也可以用IE等浏览器直接打开浏览,前提 ...

  9. iOS开发——免证书调试(Xcode7,iOS9)

    (资料已做好,待整理成文章……)

  10. 获取键盘输入或者USB扫描枪数据

    /// <summary> /// 获取键盘输入或者USB扫描枪数据 可以是没有焦点 应为使用的是全局钩子 /// USB扫描枪 是模拟键盘按下 /// 这里主要处理扫描枪的值,手动输入的 ...