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来压缩/解压缩文件,加上该选项后可以 ...
随机推荐
- iOS类初始化
类继承下来的初始化有三种: +(void)load: +(void)initialize: -(instancetype)init: +(void)load:会自动调用(也可手动调用),只要有引用 ...
- css制作小三角
视觉稿中经常有些小三角,如下图.每次用图片做太不方便了,我们看看用css实现的效果(支持ie6,7哦) <style> /*border实现三角*/ /*箭头向上*/ .arrow-top ...
- javascript进击(八)JSON
JSON 是存储和交换文本信息的语法.类似 XML. JSON 比 XML 更小.更快,更易解析. 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Ob ...
- Unity3D 之UGUI 文本框和编辑框
这里来讲解一下unity3D自带的UI功能,自带的UI也叫UGUI功能非常的强大,比起NGUI,更加的灵活,让用户能够更加容易的去使用. 首先创建一个文本Text 然后是文本相对应的属性 这里的属性比 ...
- 20160328 javaweb Cookie 小练习
利用cookie实现历史记录浏览: 由于是简单演示,所以直接用javabean 取代数据库了 数据存储类: package com.dzq.dao; import java.util.*; impor ...
- Android手机出现"已安装了存在签名冲突的同名数据包"的原因及解决办法
http://blog.csdn.net/dyllove98/article/details/8830264 如果你不是开发者:如果你在android上更新一个已经安装过较早版本软件时,安装到最后一步 ...
- 转载---SQL Server XML基础学习<2>之--FOR XML AUTO/RAW
本文主要介绍FOR XML 的 AUTO 模式和 RAW 模式 --AUTO 模式将查询结果以嵌套 XML 元素的方式返回.这不能较好地控制从查询结果生成的 XML 的形式. --如果要生成简单的层次 ...
- node.js 访问sql server的 node_modules “msnodesql"的安装编译方法
http://stackoverflow.com/questions/19661811/unable-to-download-msnodesql-0-2-1-v0-10-x64-msi 首先要满足以下 ...
- oracle级联删除 触发器
CREATE TABLE STUDENT( --创建学生表 ID NUMBER(10) PRIMARY KEY, --主键ID SNAME VARCHAR2(20), CLASSNAME V ...
- stm32中.bss和.data段是在哪里初始化的
https://segmentfault.com/q/1010000004829859/a-1020000004850311 Q: STM32的启动文件startup_stm32f10x_hd.s中的 ...