本文整理汇总了C#中System.IO.Compression.GZipStream.Read方法的典型用法代码示例。如果您正苦于以下问题:C# System.IO.Compression.GZipStream.Read方法的具体用法?C# System.IO.Compression.GZipStream.Read怎么用?C# System.IO.Compression.GZipStream.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Compression.GZipStream
的用法示例。
在下文中一共展示了System.IO.Compression.GZipStream.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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();
}
示例2: 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;
}
示例3: 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
}
示例4: 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);
}
}
示例5: uncompress
public byte[] uncompress(byte[] data)
{
MemoryStream packed = new MemoryStream(data);
System.IO.Stream unpacker = new System.IO.Compression.GZipStream(
packed,
System.IO.Compression.CompressionMode.Decompress
);
MemoryStream unpacked = new MemoryStream();
byte[] chunk = new byte[10240];
int n = unpacker.Read(chunk, 0, chunk.Length);
while (n > 0)
{
unpacked.Write(chunk, 0, n);
n = unpacker.Read(chunk, 0, chunk.Length);
}
return unpacked.ToArray();
}
示例6: Main
static void Main(string[] args)
{
System.IO.Stream unpacker = new System.IO.Compression.GZipStream(
System.Console.OpenStandardInput(),
System.IO.Compression.CompressionMode.Decompress
);
System.IO.Stream stdout = System.Console.OpenStandardOutput();
byte[] chunk = new byte[10240];
try
{
int n = unpacker.Read(chunk, 0, chunk.Length);
while (n > 0)
{
stdout.Write(chunk, 0, n);
n = unpacker.Read(chunk, 0, chunk.Length);
}
} catch (System.IO.InvalidDataException e)
{
}
}
示例7: DecompressGzip
//identify why passing stream instead of bytes returns empty result
public static byte[] DecompressGzip(byte[] gzip)
{
using (var decompressor = new System.IO.Compression.GZipStream(new MemoryStream(gzip), System.IO.Compression.CompressionMode.Decompress))
{
var buffer = new byte[BufferSize];
using (var output = new MemoryStream())
{
int read;
while ((read = decompressor.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
return output.ToArray();
}
}
}
示例8: UnZip
public string UnZip(string compressedText)
{
byte[] gzBuffer = Convert.FromBase64String(compressedText);
using (MemoryStream ms = new MemoryStream())
{
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
byte[] buffer = new byte[msgLength];
ms.Position = 0;
using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
{
zip.Read(buffer, 0, buffer.Length);
}
return System.Text.Encoding.Unicode.GetString(buffer, 0, buffer.Length);
}
}
示例9: receiveFileThread
public void receiveFileThread()
{
Directory.CreateDirectory(Path.GetDirectoryName(this.mStrFilePath));
System.IO.FileStream wout = null;
for (int it = 0; it < 3; ++it)
{
try
{
wout = new System.IO.FileStream(this.mStrFilePath, FileMode.Create);
}
catch (Exception ex)
{
this.mParent.addLog("「" + this.mStrFilePath + "」が上書きできませんでした");
System.Threading.Thread.Sleep(500);
}
if (wout != null)
{
if (it > 0)
{
this.mParent.addLog("「" + this.mStrFilePath + "」に書き込めました");
}
break;
}
}
if (this.mData != null)
{
System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(new MemoryStream(this.mData), System.IO.Compression.CompressionMode.Decompress);
// System.IO.Compression.DeflateStream gzip = new System.IO.Compression.DeflateStream(new MemoryStream(this.mData), System.IO.Compression.CompressionMode.Decompress);
// MemoryStream gzip = new MemoryStream(this.mData);
byte[] buffer = new byte[1024 * 1024];
while (true)
{
int nReadSize = gzip.Read(buffer, 0, buffer.Length);
if (nReadSize == 0)
break;
if (wout != null)
wout.Write(buffer, 0, nReadSize);
}
gzip.Close();
}
this.mParent.addLog("「" + this.mStrFilePath + "」を受信終了");
this.mParent.endReceiveFile(this.mStrFilePath);
}
示例10: Zlib_GZipStream_FileName_And_Comments
public void Zlib_GZipStream_FileName_And_Comments()
{
// select the name of the zip file
string FileToCompress = System.IO.Path.Combine(TopLevelDir, "Zlib_GZipStream.dat");
Assert.IsFalse(System.IO.File.Exists(FileToCompress), "The temporary zip file '{0}' already exists.", FileToCompress);
byte[] working = new byte[WORKING_BUFFER_SIZE];
int n = -1;
int sz = this.rnd.Next(21000) + 15000;
TestContext.WriteLine(" Creating file: {0} sz({1})", FileToCompress, sz);
CreateAndFillFileText(FileToCompress, sz);
System.IO.FileInfo fi1 = new System.IO.FileInfo(FileToCompress);
int crc1 = DoCrc(FileToCompress);
// four trials, all combos of FileName and Comment null or not null.
for (int k = 0; k < 4; k++)
{
string CompressedFile = String.Format("{0}-{1}.compressed", FileToCompress, k);
using (Stream input = File.OpenRead(FileToCompress))
{
using (FileStream raw = new FileStream(CompressedFile, FileMode.Create))
{
using (GZipStream compressor =
new GZipStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression, true))
{
// FileName is optional metadata in the GZip bytestream
if (k % 2 == 1)
compressor.FileName = FileToCompress;
// Comment is optional metadata in the GZip bytestream
if (k > 2)
compressor.Comment = "Compressing: " + FileToCompress;
byte[] buffer = new byte[1024];
n = -1;
while (n != 0)
{
if (n > 0)
compressor.Write(buffer, 0, n);
n = input.Read(buffer, 0, buffer.Length);
}
}
}
}
System.IO.FileInfo fi2 = new System.IO.FileInfo(CompressedFile);
Assert.IsTrue(fi1.Length > fi2.Length, String.Format("Compressed File is not smaller, trial {0} ({1}!>{2})", k, fi1.Length, fi2.Length));
// decompress twice:
// once with System.IO.Compression.GZipStream and once with Alienlab.Zlib.GZipStream
for (int j = 0; j < 2; j++)
{
using (var input = System.IO.File.OpenRead(CompressedFile))
{
Stream decompressor = null;
try
{
switch (j)
{
case 0:
decompressor = new Alienlab.Zlib.GZipStream(input, CompressionMode.Decompress, true);
break;
case 1:
decompressor = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress, true);
break;
}
string DecompressedFile =
String.Format("{0}.{1}.decompressed", CompressedFile, (j == 0) ? "Ionic" : "BCL");
TestContext.WriteLine("........{0} ...", System.IO.Path.GetFileName(DecompressedFile));
using (var s2 = System.IO.File.Create(DecompressedFile))
{
n = -1;
while (n != 0)
{
n = decompressor.Read(working, 0, working.Length);
if (n > 0)
s2.Write(working, 0, n);
}
}
int crc2 = DoCrc(DecompressedFile);
Assert.AreEqual<Int32>(crc1, crc2);
}
finally
{
if (decompressor != null)
decompressor.Dispose();
}
}
}
//.........这里部分代码省略.........
示例11: UnZipFile
static void UnZipFile(string file)
{
var target = file.Substring(0, file.LastIndexOf("."));
using (var fs = System.IO.File.OpenRead(file))
using (var gz = new System.IO.Compression.GZipStream(fs, System.IO.Compression.CompressionMode.Decompress))
using (var ts = System.IO.File.OpenWrite(target))
{
var buffer = new byte[0x400];
var count = 0;
while ((count = gz.Read(buffer, 0, buffer.Length)) > 0)
{
ts.Write(buffer, 0, count);
}
}
}
示例12: Decompress
private void Decompress()
{
if (this._Compressed.Length == 0)
{
this._UnCompressed = string.Empty;
return;
}
string Result = string.Empty;
try
{
byte[] ZippedData = null;
//Convert the compressed string into a byte array and decrypt the array if required
if (this._Passphrase.Length > 0)
{
ZippedData = Decrypt(System.Convert.FromBase64String(this._Compressed));
}
else
{
ZippedData = System.Convert.FromBase64String(this._Compressed);
}
//Decompress the byte array
System.IO.MemoryStream objMemStream = new System.IO.MemoryStream(ZippedData);
System.IO.Compression.GZipStream objGZipStream = new System.IO.Compression.GZipStream(objMemStream, System.IO.Compression.CompressionMode.Decompress);
byte[] sizeBytes = new byte[4];
objMemStream.Position = objMemStream.Length - 4;
objMemStream.Read(sizeBytes, 0, 4);
int iOutputSize = BitConverter.ToInt32(sizeBytes, 0);
objMemStream.Position = 0;
byte[] UnZippedData = new byte[iOutputSize];
objGZipStream.Read(UnZippedData, 0, iOutputSize);
objGZipStream.Dispose();
objMemStream.Dispose();
//Convert the decompressed byte array back to a string
Result = this._TextEncoding.GetString(UnZippedData);
}
catch (Exception)
{
}
finally
{
this._UnCompressed = Result;
}
}
示例13: GetClientPayload
public byte[] GetClientPayload()
{
byte[] retval = null;
string contentencoding = GetHeaderClient("Content-Encoding:");
_clientPayload.Position = 0;
if (contentencoding.ToLower().Contains("deflate"))
{
System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(_clientPayload, System.IO.Compression.CompressionMode.Decompress);
byte[] buffer = new byte[8192];
MemoryStream ms = new MemoryStream();
int bytesread = ds.Read(buffer, 0, buffer.Length);
while (bytesread != 0)
{
ms.Write(buffer, 0, bytesread);
bytesread = ds.Read(buffer, 0, buffer.Length);
}
retval = ms.GetBuffer();
}
else if (contentencoding.ToLower().Contains("gzip"))
{
System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(_clientPayload, System.IO.Compression.CompressionMode.Decompress);
byte[] buffer = new byte[8192];
MemoryStream ms = new MemoryStream();
int bytesread = 0;
try
{
bytesread = gz.Read(buffer, 0, buffer.Length);
}
catch (Exception ex) { _upStreamExceptions.Add(ex); }
while (bytesread != 0 & gz.CanRead)
{
ms.Write(buffer, 0, bytesread);
try
{
bytesread = gz.Read(buffer, 0, buffer.Length);
}
catch (Exception ex) { _upStreamExceptions.Add(ex); break; }
}
retval = new byte[ms.Length];
ms.Position = 0;
ms.Read(retval, 0, (int)ms.Length);
}
else
{
retval = new byte[_clientPayload.Length];
_clientPayload.Position = 0;
_clientPayload.Read(retval, 0, (int)_clientPayload.Length);
}
return retval;
}
示例14: GetMatchClientPayloadString
public string GetMatchClientPayloadString()
{
string thiscontentType = GetHeaderMatchClient("Content-Type:");
if (string.IsNullOrEmpty(thiscontentType))
return "";
string retval = "";
foreach (string contenttype in TextContentTypes)
{
if (thiscontentType.ToLower().Contains(contenttype))
{
string contentencoding = GetHeaderMatchClient("Content-Encoding:");
_matchClientPayload.Position = 0;
if (contentencoding.ToLower().Contains("deflate"))
{
System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(_matchClientPayload, System.IO.Compression.CompressionMode.Decompress);
byte[] buffer = new byte[8192];
MemoryStream ms = new MemoryStream();
int bytesread = ds.Read(buffer, 0, buffer.Length);
while (bytesread != 0)
{
ms.Write(buffer, 0, bytesread);
bytesread = ds.Read(buffer, 0, buffer.Length);
}
retval = System.Text.Encoding.Default.GetString(ms.GetBuffer());
}
else if (contentencoding.ToLower().Contains("gzip"))
{
System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(_matchClientPayload, System.IO.Compression.CompressionMode.Decompress);
byte[] buffer = new byte[8192];
MemoryStream ms = new MemoryStream();
int bytesread = 0;
try
{
bytesread = gz.Read(buffer, 0, buffer.Length);
}
catch (Exception ex) { _downStreamExceptions.Add(ex); }
while (bytesread != 0 & gz.CanRead)
{
ms.Write(buffer, 0, bytesread);
try
{
bytesread = gz.Read(buffer, 0, buffer.Length);
}
catch (Exception ex) { _downStreamExceptions.Add(ex); break; }
}
retval = System.Text.Encoding.Default.GetString(ms.GetBuffer(), 0, (int)ms.Length);
}
else
{
byte[] thebytes = new byte[_matchClientPayload.Length];
_matchClientPayload.Read(thebytes, 0,(int) _matchClientPayload.Length);
retval = System.Text.Encoding.Default.GetString(thebytes, 0, (int)_matchClientPayload.Length);
}
break;
}
}
return retval;
}
示例15: UncompressWmz
private MemoryStream UncompressWmz(MemoryStream input)
{
MemoryStream wmfStream;
input.Position = 0;
if (input.ReadByte() == 0x1f && input.ReadByte() == 0x8b)
{
input.Position = 0;
wmfStream = new MemoryStream();
var buffer = new byte[1024];
var compressedFile = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress, true);
int bytesRead;
while ((bytesRead = compressedFile.Read(buffer, 0, buffer.Length)) > 0)
{
wmfStream.Write(buffer, 0, bytesRead);
}
}
else
{
wmfStream = input;
}
wmfStream.Position = 0;
return wmfStream;
}