本文整理汇总了C#中System.IO.Compression.GZipStream类的典型用法代码示例。如果您正苦于以下问题:C# System.IO.Compression.GZipStream类的具体用法?C# System.IO.Compression.GZipStream怎么用?C# System.IO.Compression.GZipStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
System.IO.Compression.GZipStream类属于命名空间,在下文中一共展示了System.IO.Compression.GZipStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CompressOrDecompressFile
/// <summary>
/// Компрессия или декомпрессия файла
/// </summary>
/// <param name="fromFile">Исходный файл для компрессии или декомпрессии</param>
/// <param name="toFile">Целевой файл</param>
/// <param name="compressionMode">Указывает на компрессию или декомпрессию</param>
private static void CompressOrDecompressFile(string fromFile, string toFile, System.IO.Compression.CompressionMode compressionMode)
{
System.IO.FileStream toFs = null;
System.IO.Compression.GZipStream gzStream = null;
System.IO.FileStream fromFs = new System.IO.FileStream(fromFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
try
{
toFs = new System.IO.FileStream(toFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
gzStream = new System.IO.Compression.GZipStream(toFs, compressionMode);
byte[] buf = new byte[fromFs.Length];
fromFs.Read(buf, 0, buf.Length);
gzStream.Write(buf, 0, buf.Length);
}
finally
{
if (gzStream != null)
gzStream.Close();
if (toFs != null)
toFs.Close();
fromFs.Close();
}
}
示例2: ZipCompress
public static string ZipCompress(this string value)
{
//Transform string into byte[]
byte[] byteArray = new byte[value.Length];
int indexBA = 0;
foreach (char item in value.ToCharArray())
{
byteArray[indexBA++] = (byte)item;
}
//Prepare for compress
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
System.IO.Compression.CompressionMode.Compress);
//Compress
sw.Write(byteArray, 0, byteArray.Length);
//Close, DO NOT FLUSH cause bytes will go missing...
sw.Close();
//Transform byte[] zip data to string
byteArray = ms.ToArray();
System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
foreach (byte item in byteArray)
{
sB.Append((char)item);
}
ms.Close();
sw.Dispose();
ms.Dispose();
return sB.ToString();
}
示例3: Decompress
public static byte[] Decompress(byte[] data)
{
try
{
MemoryStream ms = new MemoryStream(data);
System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress, true);
MemoryStream msreader = new MemoryStream();
byte[] buffer = new byte[0x1000];
while (true)
{
int reader = zip.Read(buffer, 0, buffer.Length);
if (reader <= 0)
{
break;
}
msreader.Write(buffer, 0, reader);
}
zip.Close();
ms.Close();
msreader.Position = 0;
buffer = msreader.ToArray();
msreader.Close();
return buffer;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
示例4: Add3
// GET: Api/Post/Add3
public JsonResult Add3()
{
PostModel model = new PostModel();
model.PDate = DateTime.Now;
string fileName = Server.MapPath("~/App_Data/test3.json");
model.PText = fileName;
System.Runtime.Serialization.Json.DataContractJsonSerializer ser =
new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(PostModel));
System.IO.MemoryStream stream1 = new System.IO.MemoryStream();
ser.WriteObject(stream1, model);
using (System.IO.FileStream f2 = new System.IO.FileStream(fileName, System.IO.FileMode.Create))
{
byte[] jsonArray = stream1.ToArray();
using (System.IO.Compression.GZipStream gz =
new System.IO.Compression.GZipStream(f2, System.IO.Compression.CompressionMode.Compress))
{
gz.Write(jsonArray, 0, jsonArray.Length);
}
}
return Json(model, JsonRequestBehavior.AllowGet);
}
示例5: Decompress
/// <summary>
/// �����ѹ���������л�
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static object Decompress(byte[] buffer, Type type)
{
System.IO.MemoryStream ms3 = new System.IO.MemoryStream();
System.IO.MemoryStream ms2 = new System.IO.MemoryStream(buffer);
System.IO.Compression.GZipStream gs = new System.IO.Compression.GZipStream(ms2, System.IO.Compression.CompressionMode.Decompress);
byte[] writeData = new byte[4096];
while (true)
{
int size = gs.Read(writeData, 0, writeData.Length);
if (size > 0)
{
ms3.Write(writeData, 0, size);
}
else
{
break;
}
}
gs.Close();
ms3.Flush();
byte[] DecompressBuf = ms3.ToArray();
#region deserialize
CompressionSerialize compressionSerialize = new CompressionSerialize();
return compressionSerialize.Deserialize(DecompressBuf);
#endregion
}
示例6: GetWebPageContent
// use the cookiecontainter to retrieve the content of the given url
public static string GetWebPageContent(string url, CookieContainer cookieContainer = null)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
request.Method = "GET";
request.Credentials = CredentialCache.DefaultCredentials;
request.CookieContainer = cookieContainer;
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
if (response.ContentEncoding.ToLower().Contains("gzip"))
responseStream = new System.IO.Compression.GZipStream(responseStream, System.IO.Compression.CompressionMode.Decompress);
else if (response.ContentEncoding.ToLower().Contains("deflate"))
responseStream = new System.IO.Compression.DeflateStream(responseStream, System.IO.Compression.CompressionMode.Decompress);
StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8);
string html = readStream.ReadToEnd();
response.Close();
responseStream.Close();
return html;
}
示例7: UnZip
public static string UnZip(string value)
{
//Transform string into byte[]
byte[] byteArray = new byte[value.Length];
int indexBA = 0;
foreach (char item in value.ToCharArray())
{
byteArray[indexBA++] = (byte)item;
}
//Prepare for decompress
System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray);
System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms,
System.IO.Compression.CompressionMode.Decompress);
//Reset variable to collect uncompressed result
byteArray = new byte[byteArray.Length];
//Decompress
int rByte = sr.Read(byteArray, 0, byteArray.Length);
//Transform byte[] unzip data to string
System.Text.StringBuilder sB = new System.Text.StringBuilder(rByte);
//Read the number of bytes GZipStream red and do not a for each bytes in
//resultByteArray;
for (int i = 0; i < rByte; i++)
{
sB.Append((char)byteArray[i]);
}
sr.Close();
ms.Close();
sr.Dispose();
ms.Dispose();
return sB.ToString();
}
示例8: GetResponseAsString
private static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
{
Stream stream = null;
StreamReader reader = null;
try {
// 以字符流的方式读取HTTP响应
stream = rsp.GetResponseStream();
switch (rsp.ContentEncoding) {
case "gzip":
stream = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress);
break;
case "deflate":
stream = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Decompress);
break;
}
reader = new StreamReader(stream, encoding);
return reader.ReadToEnd();
}
finally {
// 释放资源
if (reader != null) reader.Close();
if (stream != null) stream.Close();
if (rsp != null) rsp.Close();
}
}
示例9: GZip_Decompress
/// <summary>
/// In Memory GZip Decompressor
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] GZip_Decompress(this byte[] data)
{
int length = 100000; //10Kb
byte[] Ob = new byte[length];
byte[] result = null;
using (var ms = new MemoryStream(data))
{
using (var gz = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
{
int a = 0;
while ((a = gz.Read(Ob, 0, length)) > 0)
{
if (a == length)
result = result.Concat(Ob);
else
result = result.Concat(Ob.Substring(0, a));
}
gz.Close();
}
ms.Close();
}
return result;
}
示例10: SaveWebPost
private void SaveWebPost(string fileName, PostModel model)
{
WebPostModel wPost = new WebPostModel()
{
PTitle = model.PTitle,
PText = model.PText,
PLink = model.PLink,
PImage = model.PImage,
PDate = model.PDate.ToString("yyyy-MM-ddTHH:mm:ss"),
PPrice = model.PPrice
};
System.IO.MemoryStream msPost = new System.IO.MemoryStream();
System.Runtime.Serialization.Json.DataContractJsonSerializer dcJsonPost =
new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(WebPostModel));
dcJsonPost.WriteObject(msPost, wPost);
using (System.IO.FileStream f2 = new System.IO.FileStream(fileName, System.IO.FileMode.Create))
{
byte[] jsonArray = msPost.ToArray();
using (System.IO.Compression.GZipStream gz =
new System.IO.Compression.GZipStream(f2, System.IO.Compression.CompressionMode.Compress))
{
gz.Write(jsonArray, 0, jsonArray.Length);
}
}
}
示例11: Decode
/// <summary>
/// Decode the content
/// </summary>
/// <param name="data">Content to decode</param>
/// <returns>Decoded content</returns>
public byte[] Decode(byte[] data)
{
var output = new MemoryStream();
var input = new MemoryStream(data);
using (var stream = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress))
stream.CopyTo(output);
return output.ToArray();
}
示例12: CompressGZIP
/// <summary>
/// This function return a byte array compressed by GZIP algorithm.
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] CompressGZIP(byte[] data)
{
System.IO.MemoryStream streamoutput = new System.IO.MemoryStream();
System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(streamoutput, System.IO.Compression.CompressionMode.Compress, false);
gzip.Write(data, 0, data.Length);
gzip.Close();
return streamoutput.ToArray();
}
示例13: Decompress
/// <summary>
/// Decompress with Stream
/// </summary>
public static void Decompress(Stream input)
{
System.IO.Compression.GZipStream stream =
new System.IO.Compression.GZipStream(
input, System.IO.Compression.CompressionMode.Decompress);
stream.Flush();
}
示例14: Deserialize
public override object Deserialize(Type type, Stream stream)
{
using (var gzip = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress))
using (var sr = new StreamReader(gzip, this.Encoding))
{
return new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(sr.ReadToEnd(), type);
}
}
示例15: Serialize
public override void Serialize(Stream stream, object obj)
{
var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(obj);
var data = this.Encoding.GetBytes(json);
using (var gzip = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionLevel.Fastest))
{
gzip.Write(data, 0, data.Length);
}
}