本文整理汇总了C#中System.IO.Compression.GZipStream.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# GZipStream.Dispose方法的具体用法?C# GZipStream.Dispose怎么用?C# GZipStream.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Compression.GZipStream
的用法示例。
在下文中一共展示了GZipStream.Dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Deserialize
/// <summary>
/// 反序列化 解压缩
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static object Deserialize(byte[] bytes)
{
MemoryStream msNew = new MemoryStream(bytes);
msNew.Position = 0;
GZipStream gzipStream = new GZipStream(msNew, CompressionMode.Decompress);//创建解压对象
byte[] buffer = new byte[4096];//定义数据缓冲
int offset = 0;//定义读取位置
MemoryStream ms = new MemoryStream();//定义内存流
while ((offset = gzipStream.Read(buffer, 0, buffer.Length)) != 0)
{
ms.Write(buffer, 0, offset);//解压后的数据写入内存流
}
BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化object对象
ms.Position = 0;//设置内存流的位置
object obj;
try
{
obj = (object)sfFormatter.Deserialize(ms);//反序列化
}
catch
{
throw;
}
finally
{
ms.Close();//关闭内存流
ms.Dispose();//释放资源
}
gzipStream.Close();//关闭解压缩流
gzipStream.Dispose();//释放资源
msNew.Close();
msNew.Dispose();
return obj;
}
示例2: DeCompress
/// <summary>
/// 解压数据
/// </summary>
/// <param name="aSourceStream"></param>
/// <returns></returns>
public static byte[] DeCompress(Stream aSourceStream)
{
byte[] vUnZipByte = null;
GZipStream vUnZipStream;
using (MemoryStream vMemory = new MemoryStream())
{
vUnZipStream = new GZipStream(aSourceStream, CompressionMode.Decompress);
try
{
byte[] vTempByte = new byte[1024];
int vRedLen = 0;
do
{
vRedLen = vUnZipStream.Read(vTempByte, 0, vTempByte.Length);
vMemory.Write(vTempByte, 0, vRedLen);
}
while (vRedLen > 0);
vUnZipStream.Close();
}
finally
{
vUnZipStream.Dispose();
}
vUnZipByte = vMemory.ToArray();
}
return vUnZipByte;
}
示例3: backgroundWorker1DoWork
void backgroundWorker1DoWork(object sender, DoWorkEventArgs e)
{
try
{
var movimentoBancarioService = ResolveComponent<IMovimentoBancarioService>();
var infile = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
var buffer = new byte[infile.Length];
infile.Read(buffer, 0, buffer.Length);
var ms = new MemoryStream();
var compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true);
compressedzipStream.Write(buffer, 0, buffer.Length);
compressedzipStream.Close();
_id = movimentoBancarioService.Importazione(ms.ToArray(), infile.Length);
compressedzipStream.Close();
compressedzipStream.Dispose();
}
catch (FileNotFoundException ex)
{
_log.ErrorFormat("Errore nell'importazione di un file CBI - FILE NON TROVATO - {0} - azienda:{1}", ex, Utility.GetMethodDescription(), Security.Login.Instance.CurrentLogin().Azienda);
e.Result = string.Format("Il file '{0}' non è stato trovato", _fileName);
}
catch (Exception ex)
{
_log.ErrorFormat("Errore nell'importazione di un file CBI - {0} - azienda:{1}", ex, Utility.GetMethodDescription(), Security.Login.Instance.CurrentLogin().Azienda);
throw;
}
}
示例4: Decompress
public static string Decompress(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();
}
示例5: GZipDecompress
/// <summary>
/// GZip解压函数
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] GZipDecompress(byte[] data)
{
int bufferSize = 256;
using (MemoryStream stream = new MemoryStream())
{
using (GZipStream gZipStream = new GZipStream(new MemoryStream(data), CompressionMode.Decompress))
{
byte[] bytes = new byte[bufferSize];
int n;
while ((n = gZipStream.Read(bytes, 0, bytes.Length)) != 0)
{
stream.Write(bytes, 0, n);
}
gZipStream.Close();
gZipStream.Dispose();
}
byte[] rdata = stream.ToArray();
stream.Close();
stream.Dispose();
return rdata;
}
}
示例6: CompressDataPackage
/// <summary>
/// Compresses an Epi Info 7 data package.
/// </summary>
/// <param name="fi">The file info for the raw data file that will be compressed.</param>
public static void CompressDataPackage(FileInfo fi)
{
using (FileStream inFile = fi.OpenRead())
{
// Prevent compressing hidden and already compressed files.
if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden)
!= FileAttributes.Hidden & fi.Extension != ".gz")
{
using (FileStream outFile = File.Create(fi.FullName + ".gz"))
{
using (GZipStream Compress = new GZipStream(outFile,
CompressionMode.Compress))
{
// Copy the source file into the compression stream.
byte[] buffer = new byte[4096];
int numRead;
while ((numRead = inFile.Read(buffer, 0, buffer.Length)) != 0)
{
Compress.Write(buffer, 0, numRead);
}
Compress.Close();
Compress.Dispose();
}
}
}
}
}
示例7: SerializeAsync
public async Task<SerializerParameters> SerializeAsync(object value, Stream stream, CancellationToken cancellationToken = new CancellationToken())
{
if (value == null) throw new ArgumentNullException(nameof(value));
if (stream == null) throw new ArgumentNullException(nameof(stream));
Stream innerStream = stream;
string contentEncoding = null;
switch (Algorithm)
{
case CompressAlgorithm.Identity:
break;
case CompressAlgorithm.Deflate:
innerStream = new DeflateStream(stream, CompressionLevel, true);
contentEncoding = "deflate";
break;
case CompressAlgorithm.GZip:
innerStream = new GZipStream(stream, CompressionLevel, true);
contentEncoding = "gzip";
break;
default:
throw new ArgumentOutOfRangeException(nameof(Algorithm));
}
try
{
var parameters = await InnerSerializer.SerializeAsync(value, innerStream, cancellationToken);
return new SerializerParameters(parameters.ContentType, contentEncoding);
}
finally
{
if (innerStream != stream)
innerStream.Dispose();
}
}
示例8: GetStream
public static Stream GetStream(Stream fileStream)
{
Stream objFileStream = null;
try
{
GZipStream gzip = new GZipStream(fileStream, CompressionMode.Decompress);
MemoryStream memStream = new MemoryStream();
byte[] bytes = new byte[1024];
int bytesRead = -1;
while (bytesRead != 0)
{
bytesRead = gzip.Read(bytes, 0, 1024);
memStream.Write(bytes, 0, bytesRead);
memStream.Flush();
}
memStream.Position = 0;
objFileStream = memStream;
gzip.Close();
gzip.Dispose();
fileStream.Dispose();
}
catch (Exception)
{
fileStream.Position = 0;
objFileStream = fileStream;
}
return objFileStream;
}
示例9: CompressHandHistory
public string CompressHandHistory(string fullHandText)
{
if (fullHandText == null) return null;
//Transform string into byte[]
byte[] byteArray = new byte[fullHandText.Length];
int indexBA = 0;
foreach (char item in fullHandText.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();
}
示例10: UncompressHandHistory
public string UncompressHandHistory(string compressedHandHistory)
{
if (compressedHandHistory == null) return null;
byte[] byteArray = new byte[compressedHandHistory.Length];
int indexBa = 0;
foreach (char item in compressedHandHistory)
byteArray[indexBa++] = (byte)item;
MemoryStream memoryStream = new MemoryStream(byteArray);
GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress);
byteArray = new byte[1024];
StringBuilder stringBuilder = new StringBuilder();
int readBytes;
while ((readBytes = gZipStream.Read(byteArray, 0, byteArray.Length)) != 0)
{
for (int i = 0; i < readBytes; i++)
stringBuilder.Append((char)byteArray[i]);
}
gZipStream.Close();
memoryStream.Close();
gZipStream.Dispose();
memoryStream.Dispose();
return stringBuilder.ToString();
}
示例11: DecompressStream
/// <summary>
/// Decompresses stream using GZip. Returns decompressed Stream.
/// Returns null if stream isn't compressed.
/// </summary>
/// <param name="compressedStream">Stream compressed with GZip.</param>
public static MemoryStream DecompressStream(Stream compressedStream)
{
MemoryStream newStream = new MemoryStream();
compressedStream.Seek(0, SeekOrigin.Begin);
GZipStream Decompressor = null;
try
{
Decompressor = new GZipStream(compressedStream, CompressionMode.Decompress, true);
Decompressor.CopyTo(newStream);
}
catch (InvalidDataException invdata)
{
return null;
}
catch(Exception e)
{
throw;
}
finally
{
if (Decompressor != null)
Decompressor.Dispose();
}
return newStream;
}
示例12: CompressCanWrite
public static void CompressCanWrite()
{
var ms = new MemoryStream();
var zip = new GZipStream(ms, CompressionMode.Compress);
Assert.True(zip.CanWrite, "GZipStream not CanWrite with CompressionMode.Compress");
zip.Dispose();
Assert.False(zip.CanWrite, "GZipStream CanWrite after dispose");
}
示例13: GetTextForGzip
public static string GetTextForGzip(Stream stream, Encoding coding)
{
GZipStream gzip = new GZipStream(stream, CompressionMode.Decompress);
StreamReader reader = new StreamReader(gzip, coding);
string html = reader.ReadToEnd();
gzip.Close();
gzip.Dispose();
return html;
}
示例14: Write
public override void Write(long pageAddr, byte[] buf)
{
long pageOffs = 0;
if (pageAddr != 0)
{
Debug.Assert(buf.Length == Page.pageSize);
Debug.Assert((pageAddr & (Page.pageSize-1)) == 0);
long pagePos = pageMap.get(pageAddr);
bool firstUpdate = false;
if (pagePos == 0)
{
long bp = (pageAddr >> (Page.pageSizeLog - 3));
byte[] posBuf = new byte[8];
indexFile.Read(bp, posBuf);
pagePos = Bytes.unpack8(posBuf, 0);
firstUpdate = true;
}
int pageSize = ((int)pagePos & (Page.pageSize-1)) + 1;
MemoryStream ms = new MemoryStream();
GZipStream stream = new GZipStream(ms, CompressionMode.Compress, true);
stream.Write(buf, 0, buf.Length);
#if WINRT_NET_FRAMEWORK
stream.Dispose();
#else
stream.Close();
#endif
if (ms.Length < Page.pageSize)
{
buf = ms.ToArray();
}
else
{
byte[] copy = new byte[buf.Length];
Array.Copy(buf, 0, copy, 0, buf.Length);
}
crypt(buf, buf.Length);
int newPageBitSize = (buf.Length + ALLOCATION_QUANTUM - 1) >> ALLOCATION_QUANTUM_LOG;
int oldPageBitSize = (pageSize + ALLOCATION_QUANTUM - 1) >> ALLOCATION_QUANTUM_LOG;
if (firstUpdate || newPageBitSize != oldPageBitSize)
{
if (!firstUpdate)
{
Bitmap.free(bitmap, pagePos >> (Page.pageSizeLog + ALLOCATION_QUANTUM_LOG),
oldPageBitSize);
}
pageOffs = allocate(newPageBitSize);
}
else
{
pageOffs = pagePos >> Page.pageSizeLog;
}
pageMap.put(pageAddr, (pageOffs << Page.pageSizeLog) | (buf.Length-1), pagePos);
}
base.Write(pageOffs, buf);
}
示例15: DecompressCanRead
public static void DecompressCanRead()
{
var ms = new MemoryStream();
var zip = new GZipStream(ms, CompressionMode.Decompress);
Assert.True(zip.CanRead, "GZipStream not CanRead in Decompress");
zip.Dispose();
Assert.False(zip.CanRead, "GZipStream CanRead after dispose in Decompress");
}