本文整理汇总了C#中System.IO.Compression.GZipStream.Write方法的典型用法代码示例。如果您正苦于以下问题:C# System.IO.Compression.GZipStream.Write方法的具体用法?C# System.IO.Compression.GZipStream.Write怎么用?C# System.IO.Compression.GZipStream.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Compression.GZipStream
的用法示例。
在下文中一共展示了System.IO.Compression.GZipStream.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GZIP
/// <summary>
/// Compress contents to gzip
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <remarks></remarks>
public static byte[] GZIP(ref byte[] input)
{
try
{
byte[] compressedData = null;
MemoryStream oMemoryStream = null;
System.IO.Compression.GZipStream compressedzipStream = null;
oMemoryStream = new MemoryStream();
compressedzipStream = new System.IO.Compression.GZipStream(oMemoryStream, System.IO.Compression.CompressionMode.Compress, false);
compressedzipStream.Write(input, 0, input.Length);
compressedzipStream.Close();
compressedData = oMemoryStream.ToArray();
compressedzipStream.Dispose();
compressedzipStream.Close();
oMemoryStream.Close();
return compressedData;
}
catch (Exception ex)
{
return null;
}
}
示例2: 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();
}
}
示例3: 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();
}
示例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: 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();
}
示例6: 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);
}
}
示例7: Compress
private byte[] Compress(string data)
{
using (System.IO.MemoryStream mem = new System.IO.MemoryStream())
{
using (System.IO.Compression.GZipStream g = new System.IO.Compression.GZipStream(mem, System.IO.Compression.CompressionLevel.Optimal))
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
g.Write(bytes, 0, bytes.Length);
}
return mem.ToArray();
}
}
示例8: compress
public byte[] compress(byte[] data)
{
MemoryStream packed = new MemoryStream();
System.IO.Stream packer = new System.IO.Compression.GZipStream(
packed,
System.IO.Compression.CompressionMode.Compress
);
packer.Write(data, 0, data.Length);
packer.Close();
return packed.ToArray();
}
示例9: start
private void start()
{
//Debug.Assert(MAPSIZE == 64, "The BlockBulkTransfer message requires a map size of 64.");
for (byte x = 0; x < MAPSIZE; x++)
for (byte y = 0; y < MAPSIZE; y += 16)
{
NetBuffer msgBuffer = infsN.CreateBuffer();
msgBuffer.Write((byte)Infiniminer.InfiniminerMessage.BlockBulkTransfer);
if (!compression)
{
msgBuffer.Write(x);
msgBuffer.Write(y);
for (byte dy = 0; dy < 16; dy++)
for (byte z = 0; z < MAPSIZE; z++)
msgBuffer.Write((byte)(infs.blockList[x, y + dy, z]));
if (client.Status == NetConnectionStatus.Connected)
infsN.SendMessage(msgBuffer, client, NetChannel.ReliableUnordered);
}
else
{
//Compress the data so we don't use as much bandwith - Xeio's work
var compressedstream = new System.IO.MemoryStream();
var uncompressed = new System.IO.MemoryStream();
var compresser = new System.IO.Compression.GZipStream(compressedstream, System.IO.Compression.CompressionMode.Compress);
//Send a byte indicating that yes, this is compressed
msgBuffer.Write((byte)255);
//Write everything we want to compress to the uncompressed stream
uncompressed.WriteByte(x);
uncompressed.WriteByte(y);
for (byte dy = 0; dy < 16; dy++)
for (byte z = 0; z < MAPSIZE; z++)
uncompressed.WriteByte((byte)(infs.blockList[x, y + dy, z]));
//Compress the input
compresser.Write(uncompressed.ToArray(), 0, (int)uncompressed.Length);
//infs.ConsoleWrite("Sending compressed map block, before: " + uncompressed.Length + ", after: " + compressedstream.Length);
compresser.Close();
//Send the compressed data
msgBuffer.Write(compressedstream.ToArray());
if (client.Status == NetConnectionStatus.Connected)
infsN.SendMessage(msgBuffer, client, NetChannel.ReliableUnordered);
}
}
conn.Abort();
}
示例10: perror
public static string lerror = ""; // Gets the last error that occurred in this struct. Similar to the C perror().
#endregion Fields
#region Methods
public static bool compress(string infile, string outfile)
{
try {
byte[] ifdata = System.IO.File.ReadAllBytes(infile);
System.IO.StreamWriter sw = new System.IO.StreamWriter(outfile);
System.IO.Compression.GZipStream gzs = new System.IO.Compression.GZipStream(sw.BaseStream, System.IO.Compression.CompressionMode.Compress);
gzs.Write(ifdata, 0, ifdata.Length);
gzs.Close();
gzs.Dispose();
} catch (System.Exception ex) {
lerror = ex.Message;
return false;
}
return true;
}
示例11: GZIP
/// <summary>
/// Compress contents to gzip
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
/// <remarks></remarks>
public static byte[] GZIP(ref string s)
{
byte[] buffer = null;
byte[] compressedData = null;
MemoryStream oMemoryStream = null;
System.IO.Compression.GZipStream compressedzipStream = null;
oMemoryStream = new MemoryStream();
buffer = System.Text.Encoding.UTF8.GetBytes(s);
compressedzipStream = new System.IO.Compression.GZipStream(oMemoryStream, System.IO.Compression.CompressionMode.Compress, true);
compressedzipStream.Write(buffer, 0, buffer.Length);
compressedzipStream.Dispose();
compressedzipStream.Close();
compressedData = oMemoryStream.ToArray();
oMemoryStream.Close();
return compressedData;
}
示例12: GZip_Compress
/// <summary>
/// In Memory Compression with Gzip
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] GZip_Compress(this byte[] data)
{
byte[] res = null;
MemoryStream ms = null;
System.IO.Compression.GZipStream gz = null;
using (ms = new MemoryStream())
{
using (gz = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, false))
{
gz.Write(data, 0, data.Length);
}
res = ms.ToArray();
}
return res;
}
示例13: Compress
/// <summary>
/// �������л���ѹ��
/// </summary>
/// <param name="ds"></param>
/// <returns></returns>
public static byte[] Compress(object obj, Type type)
{
byte[] compressedBuf;
CompressionSerialize compressionSerialize = new CompressionSerialize();
#region serialize
byte[] buf = compressionSerialize.Serialize(obj, type);
#endregion
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.Compression.GZipStream gs = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true);
gs.Write(buf, 0, buf.Length);
gs.Close();
compressedBuf = ms.ToArray();
return compressedBuf;
}
示例14: CompressDataSet
/// <summary>
/// DataSet���л���ѹ��
/// </summary>
/// <param name="ds"></param>
/// <returns></returns>
public static byte[] CompressDataSet(DataSet ds)
{
byte[] compressedBuf;
#region serialize
RawSerializer rs = new RawSerializer();
byte[] buf = rs.Serialize(ds);
#endregion
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.Compression.GZipStream gs = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true);
gs.Write(buf, 0, buf.Length);
gs.Close();
compressedBuf = ms.ToArray();
return compressedBuf;
}
示例15: Compress
// <summary>
/// 对byte数组进行压缩
/// </summary>
/// <param name="data">待压缩的byte数组</param>
/// <returns>压缩后的byte数组</returns>
public static byte[] Compress(byte[] data)
{
try
{
MemoryStream ms = new MemoryStream();
System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true);
zip.Write(data, 0, data.Length);
zip.Close();
byte[] buffer = new byte[ms.Length];
ms.Position = 0;
ms.Read(buffer, 0, buffer.Length);
ms.Close();
return buffer;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}