本文整理汇总了C#中System.IO.Compression.GZipStream.ReadAsync方法的典型用法代码示例。如果您正苦于以下问题:C# GZipStream.ReadAsync方法的具体用法?C# GZipStream.ReadAsync怎么用?C# GZipStream.ReadAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Compression.GZipStream
的用法示例。
在下文中一共展示了GZipStream.ReadAsync方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decompress
/// <summary>
/// decompress gzip data
/// </summary>
/// <param name="data">compressed data</param>
/// <returns></returns>
public async static Task<byte[]> Decompress(byte[] data)
{
MemoryStream ms = null;
GZipStream compressedzipStream = null;
MemoryStream outBuffer = new MemoryStream();
try
{
ms = new MemoryStream(data);
compressedzipStream = new GZipStream(ms, CompressionMode.Decompress);
byte[] block = new byte[1024 * 16];
while (true)
{
int bytesRead = await compressedzipStream.ReadAsync(block, 0, block.Length);
if (bytesRead <= 0)
{
break;
}
else
{
outBuffer.Write(block, 0, bytesRead);
}
}
}
finally
{
if (null != compressedzipStream) compressedzipStream.Close();
if (null != ms) ms.Close();
}
return outBuffer.ToArray();
}
示例2: LoadAsync
public async Task LoadAsync(Stream input, ZipHeader header, CancellationToken cancellationToken)
{
if (input == null) throw new ArgumentNullException(nameof(input));
if (header == null) throw new ArgumentNullException(nameof(header));
// Clear the files
Files.Clear();
var buffer = new byte[64 * 1024];
foreach (var file in header.Files)
{
var fileSize = file.Item2;
using (var output = new MemoryStream(fileSize))
{
int readBytes;
while ((readBytes = await input.ReadAsync(buffer, 0, Math.Min(fileSize, buffer.Length), cancellationToken)) != 0)
{
await output.WriteAsync(buffer, 0, readBytes, cancellationToken);
fileSize -= readBytes;
}
output.Position = 0;
using (var data = new MemoryStream(fileSize))
{
using (var zip = new GZipStream(output, CompressionMode.Decompress, true))
{
while ((readBytes = await zip.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) != 0)
{
await data.WriteAsync(buffer, 0, readBytes, cancellationToken);
}
Files.Add(data.ToArray());
}
}
}
}
}
示例3: DecompressAsync
// Making this async since regular read/write are tested below
private static async Task DecompressAsync(MemoryStream compareStream, MemoryStream gzStream)
{
var ms = new MemoryStream();
var zip = new GZipStream(gzStream, CompressionMode.Decompress);
var GZipStream = new MemoryStream();
int _bufferSize = 1024;
var bytes = new Byte[_bufferSize];
bool finished = false;
int retCount;
while (!finished)
{
retCount = await zip.ReadAsync(bytes, 0, _bufferSize);
if (retCount != 0)
await GZipStream.WriteAsync(bytes, 0, retCount);
else
finished = true;
}
GZipStream.Position = 0;
compareStream.Position = 0;
byte[] compareArray = compareStream.ToArray();
byte[] writtenArray = GZipStream.ToArray();
Assert.Equal(compareArray.Length, writtenArray.Length);
for (int i = 0; i < compareArray.Length; i++)
{
Assert.Equal(compareArray[i], writtenArray[i]);
}
}
示例4: Decompress
async Task<MemoryStream> Decompress(FilterDownloadResult filter, CancellationToken cancellationToken, IProgress<ProgressModel> progress)
{
using (var stream = filter.Stream)
{
var result = new MemoryStream();
stream.Seek(0, SeekOrigin.Begin);
cancellationToken.ThrowIfCancellationRequested();
switch (filter.CompressionFormat)
{
case CompressionFormat.GZip:
using(var gzipFile = new GZipStream(stream, CompressionMode.Decompress))
{
var buffer = new byte[1024 * 64];
int bytesRead;
while ((bytesRead = await gzipFile.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0)
{
cancellationToken.ThrowIfCancellationRequested();
result.Write(buffer, 0, bytesRead);
progress.Report(new ProgressModel(UpdateState.Decompressing, "Decompressing...", -1));
}
}
break;
case CompressionFormat.Zip:
using (var zipFile = new ZipArchive(stream,ZipArchiveMode.Read))
{
progress.Report(new ProgressModel(UpdateState.Decompressing, "Decompressing...", -1));
if (zipFile.Entries.Count == 0) throw new IOException("There are no entries in the zip file.");
if (zipFile.Entries.Count > 1) throw new IOException("There is more than one file in the zip file. This application will need to be updated to support this.");
var entry = zipFile.Entries.First();
using (var entryStream = entry.Open())
{
cancellationToken.ThrowIfCancellationRequested();
await entryStream.CopyToAsync(result);
}
}
break;
default:
await stream.CopyToAsync(result);
break;
}
return result;
}
}
示例5: Decompress
private static async Task<DecompressionDetails> Decompress(DecompressionDetails compressionDetails)
{
byte[] compressedData = new byte[compressionDetails.ChunkSize];
// uncompressed the chunk
using (MemoryStream uncompressedDataStream = new MemoryStream(compressionDetails.Bytes))
using (GZipStream streamUncompressed = new GZipStream(uncompressedDataStream, CompressionMode.Decompress))
{
int index = 0;
int count = compressedData.Length;
while (index < compressedData.Length - 1)
{
// read the chunk in the compressed stream
var bytesRead = await streamUncompressed.ReadAsync(compressedData, index, count);
index += bytesRead;
count -= bytesRead;
}
}
compressionDetails = new DecompressionDetails
{
Bytes = compressedData,
ChunkSize = compressionDetails.ChunkSize,
Sequence = compressionDetails.Sequence,
IsProcessed = compressionDetails.IsProcessed
};
return compressionDetails;
}
示例6: DecompressAsync
/// <summary>
/// Decompresses a byte array containing compressed data.
/// </summary>
/// <param name="value">
/// The byte array of compressed data.
/// </param>
/// <returns>
/// A byte array containing decompressed data.
/// </returns>
public async Task<byte[]> DecompressAsync(byte[] value)
{
using (MemoryStream ms = new MemoryStream(value))
{
using (GZipStream gs = new GZipStream(ms, CompressionMode.Decompress))
{
using (MemoryStream fin = new MemoryStream())
{
byte[] buffer = new byte[1024];
int numberRead;
while ((numberRead = await gs.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await fin.WriteAsync(buffer, 0, numberRead);
}
return fin.ToArray();
}
}
}
}
示例7: Decompress
async Task<MemoryStream> Decompress(FilterDownloadResult filter, CancellationToken cancellationToken, IProgress<ProgressModel> progress)
{
using (var stream = filter.Stream)
{
var result = new MemoryStream();
stream.Seek(0, SeekOrigin.Begin);
cancellationToken.ThrowIfCancellationRequested();
switch (filter.CompressionFormat)
{
case CompressionFormat.GZip:
using(var gzipFile = new GZipStream(stream, CompressionMode.Decompress))
{
var buffer = new byte[1024 * 64];
int bytesRead;
while ((bytesRead = await gzipFile.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0)
{
cancellationToken.ThrowIfCancellationRequested();
result.Write(buffer, 0, bytesRead);
progress.Report(new ProgressModel(UpdateState.Decompressing, "Decompressing...", -1));
}
}
break;
case CompressionFormat.Zip:
EventHandler<ReadProgressEventArgs> reportProgress = (sender, args) =>
{
cancellationToken.ThrowIfCancellationRequested();
if (args.EventType != ZipProgressEventType.Extracting_EntryBytesWritten) return;
var percentage = (int)Math.Floor( (double)args.BytesTransferred / args.TotalBytesToTransfer * 100);
progress.Report(new ProgressModel(UpdateState.Decompressing, "Unzipping...", percentage));
};
using (var zipFile = ZipFile.Read(stream, reportProgress))
{
if (zipFile.Entries.Count == 0) throw new ZipException("There are no entries in the zip file.");
if (zipFile.Entries.Count > 1) throw new ZipException("There is more than one file in the zip file. This application will need to be updated to support this.");
var entry = zipFile.Entries.First();
entry.Extract(result);
}
break;
default:
await stream.CopyToAsync(result);
break;
}
return result;
}
}