//引用
            using System.IO.Compression;
            //解压缩类
            GZipStream
            //解压缩实例
            ......
            HttpWebResponse httpRequest = (HttpWebResponse)httpLogin.GetResponse();
            Stream HttpResStream= httpRequest.GetResponseStream();
            GZipStream gzip = new GZipStream(HttpResStream, CompressionMode.Decompress) ;
            //对解压缩后的字符串信息解析
           while ((len = gzip.Read(bytes, , bytes.Length)) > )
            {
               line =  System.Text.Encoding.Default.GetString(bytes);
            }  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;
using System.IO;
using System.IO.Compression; public class GZipTest
{
    public static int ReadAllBytesFromStream(Stream stream, byte[] buffer) 
    {
    // Use this method is used to read all bytes from a stream.
    int offset = ;
    int totalCount = ;
        while (true) 
        {
        int bytesRead = stream.Read(buffer, offset, ); 
            if ( bytesRead == ) 
            {
            break; 
            }
    offset += bytesRead;
    totalCount += bytesRead; 
        }
    return totalCount;
    }      public static bool CompareData(byte[] buf1, int len1, byte[] buf2, int len2) 
    {
        // Use this method to compare data from two different buffers.
        if (len1 != len2) 
        { 
        Console.WriteLine("Number of bytes in two buffer are different {0}:{1}", len1, len2);
        return false;
        }         for ( int i= ; i< len1; i++) 
        {
            if ( buf1[i] != buf2[i]) 
            {
            Console.WriteLine("byte {0} is different {1}|{2}", i, buf1[i], buf2[i]);
            return false;
            }
        }
    Console.WriteLine("All bytes compare.");
    return true; 
    }     public static void GZipCompressDecompress(string filename)
    {
    Console.WriteLine("Test compression and decompression on file {0}", filename);
    FileStream infile;
        try
        {
        // Open the file as a FileStream object.
        infile = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
        byte[] buffer = new byte[infile.Length];
        // Read the file to ensure it is readable.
        int count = infile.Read(buffer, , buffer.Length);
            if ( count != buffer.Length) 
            {
            infile.Close();
            Console.WriteLine("Test Failed: Unable to read data from file"); 
            return;
            }
        infile.Close();
        MemoryStream ms = new MemoryStream();
        // Use the newly created memory stream for the compressed data.
        GZipStream compressedzipStream = new GZipStream(ms , CompressionMode.Compress, true);
        Console.WriteLine("Compression");
        compressedzipStream.Write(buffer, , buffer.Length);
        // Close the stream.
        compressedzipStream.Close();
        Console.WriteLine("Original size: {0}, Compressed size: {1}", buffer.Length, ms.Length);         // Reset the memory stream position to begin decompression.
        ms.Position = ;
        GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress);
        Console.WriteLine("Decompression");
        byte[] decompressedBuffer = new byte[buffer.Length + ];
        // Use the ReadAllBytesFromStream to read the stream.
        int totalCount = GZipTest.ReadAllBytesFromStream(zipStream, decompressedBuffer);
        Console.WriteLine("Decompressed {0} bytes", totalCount);         if( !GZipTest.CompareData(buffer, buffer.Length, decompressedBuffer, totalCount) ) 
        {
        Console.WriteLine("Error. The two buffers did not compare.");
        }
    zipStream.Close(); 
        } // end try
        catch (InvalidDataException)
        {
            Console.WriteLine("Error: The file being read contains invalid data.");
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("Error:The file specified was not found.");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("Error: path is a zero-length string, contains only white space, or contains one or more invalid characters");
        }
        catch (PathTooLongException)
        {
            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.");
        }
        catch (DirectoryNotFoundException)
        {
            Console.WriteLine("Error: The specified path is invalid, such as being on an unmapped drive.");
        }
        catch (IOException)
        {
            Console.WriteLine("Error: An I/O error occurred while opening the file.");
        }
        catch (UnauthorizedAccessException)
        {
            Console.WriteLine("Error: path specified a file that is read-only, the path is a directory, or caller does not have the required permissions.");
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine("Error: You must provide parameters for MyGZIP.");
        }
    }
    public static void Main(string[] args)
    {
        string usageText = "Usage: MYGZIP <inputfilename>";
        //If no file name is specified, write usage text.
        if (args.Length == )
        {
            Console.WriteLine(usageText);
        }
        else
        {
            if (File.Exists(args[]))
                GZipCompressDecompress(args[]);
        }
    }
}     

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. html笔记05:html、css、javascript综合案例

    1.首先是html代码: <!-- markup time --> <div class="wrapper wb"> <div class=" ...

  2. Redhat YUM U盘源配置

    Redhat YUM U盘源配置 1)在U盘创建目录 #mkdir /yum/Server 并从光盘Server.Packages 目录的所有文件拷贝到/yum/Server 2)安装 creater ...

  3. Spring 3.0 + Atomikos构建jta分布式事务

    Spring3.0已经不再支持jtom了,不过我们可以用第三方开源软件atomikos(http://www.atomikos.com/)来实现.Atomikos是目前在分布式事务管理中做得相当不错的 ...

  4. SQL Developer 4.1.3

    http://www.oracle.com/technetwork/developer-tools/sql-developer/downloads/index.html SQL DeveloperDo ...

  5. ActiveMQ系列(1) - 使用入门

    没网的日子真的不好过啊 1.背景:                   对于常见业务中,数据并发是一个很头疼的问题,很多时候,会出现资源共享导致线程阻塞的问题,这时候问题就来了,,,老板也尾随来了,来 ...

  6. 20151211Jquery Ajax进阶学习笔记

    四.JSON 和 JSONP 如果在同一个域下,$.ajax()方法只要设置 dataType 属性即可加载 JSON 文件.而在非 同域下,可以使用 JSONP,但也是有条件的. //$.ajax( ...

  7. ios Toll-Free Bridging

    有一些数据类型是能够在 Core Foundation Framework 和 Foundation Framework 之间交换使用的.这意味着,对于同一个数据类型,你既可以将其作为参数传入 Cor ...

  8. Java线程间通信-回调的实现方式

    Java线程间通信-回调的实现方式   Java线程间通信是非常复杂的问题的.线程间通信问题本质上是如何将与线程相关的变量或者对象传递给别的线程,从而实现交互.   比如举一个简单例子,有一个多线程的 ...

  9. iOS zipzap读取压缩文件

    最近在公司遇到一项需求,在不解压zip文件的情况下读取其中的文件,因为之前使用的ziparchive不能满足现在的需求,所以在网上一阵狂搜,终于找到了zipzap,实话说还真的难找. 之前读取本地zi ...

  10. swift-08-使用键值对儿统计字符在字符串中出现的次数

    // //  main.swift //  12- // //  Created by wanghy on 15/8/9. //  Copyright (c) 2015年 wanghy. All ri ...