c# gzip解压缩
//引用
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解压缩的更多相关文章
- Windows API方式直接调用C#的DLL,支持多音字转拼音、Gzip解压缩、公式计算(VBA、C++、VB、Delphi甚至java都可以)
原始链接 https://www.cnblogs.com/Charltsing/p/DllExport.html 这两年,我在VBA应用方面一直有几大痛点:1.多音字转拼音:2.64位下的GZIP解压 ...
- VB6之借助zlib实现gzip解压缩
这是个简版的,可以拿来做下网页gzip的解压缩,整好我的webserver还不支持这个,有时间了就加上. zlib.dll下载请点击我! 模块zlib.bas的代码如下: 'code by lichm ...
- http gzip 解压缩
var sContentEncoding = httpRespone.Headers["Content-Encoding"]; if(sContentEncoding == &qu ...
- c语言使用zlib实现文本字符的gzip压缩与gzip解压缩
网络上找到的好多方法在解压缩字符串的时候会丢失字符,这里是解决方法: http://stackoverflow.com/questions/21186535/compressing-decompres ...
- AXIS2调用web service,返回结果用GZIP解压缩
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...
- C#使用Gzip解压缩完整读取网页内容
using System; using System.Threading; using System.Text; using System.Text.RegularExpressions; using ...
- 对数据进行GZIP压缩或解压缩
/** * 对data进行GZIP解压缩 * @param data * @return * @throws Exception */ public static String unCompress( ...
- java GZIP压缩与解压缩
1.GZIP压缩 public static byte[] compress(String str, String encoding) { if (str == null || str.length( ...
- AIX 文件 打包 与 压缩 tar gzip compress 的使用
今天在Aix用tar -cvf 备份,打成tar包,占有硬盘空间过大,没有压缩比, 尝试使用tar -zcvf linux系统下可以用-z 命令 (z 用gzip来压缩/解压缩文件,加上该选项后可以 ...
随机推荐
- 匹配url - 正则表达式
/^\/(.+)\/$/g.exec(req.url)
- c++基础语法 构造函数 析构函数 类的组合
1 构造函数 1.不能指定任何返回值,甚至连void都不能有. 2.与Java不同,c++不同new对象,对于无参的构造函数声明对象时括号应该省略. 2 析构函数 1. 前加~,不能有参数,不能有返回 ...
- iOS之定位与地图
概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用 和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一 ...
- bit,byte,char,string区别与基本类型认识
bit.byte.位.字节.汉字的关系 1 bit = 1 二进制数据 1 byte = 8 bit 1 字母 = 1 byte = 8 bit ...
- oracle根据pid查询出正在执行的执行语句
今天数据库访问突然很慢,通过top命令发现oracle的cpu使用率很高.同事建议查询一下看看是什么语句导致的oracle运行变慢.于是从网上查了一下,可以根据pid查询出正在执行的查询语句,发现是一 ...
- volatile--共享数据必须保证可见性
在Effective Java中看到的,试了一下,有点意思,考查的知识点是volatile关键字. 下面这段代码,预期是打印it takes xxxx miliseconds. .但实际上,陷入了死循 ...
- JavaScript入门(5)
一.什么是数组? 数组是一个值的集合,每一个值都有一个索引号,从0开始,每个索引都有一个相应的值,根据需要添加更多数值. 好比一个团,团里有很多人.如下使用数组存储5个学生成绩: 二.如何创建数组 使 ...
- Spring、struts、webwork2三者MVC的比较
http://blog.sina.com.cn/s/blog_4a69fa43010005il.html 在web应用方面,Spring有独立的MVC实现,与struts和webwork2相比毫不逊色 ...
- (转) ASP.NET反射
原文:http://www.cnblogs.com/zizo/p/3509895.html 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理情况.这是如何 ...
- thinkphp 模板中赋值
在项目开发的时候,有时候希望直接在模板中调用 一些自定义方法,或者内置方法来,处理获得一些数据,并且赋值给一个变量给后面调用,这个时候如果用原生Php 的方式调用如下:<?php $abc = ...