本文整理汇总了C#中System.IO.Compression.GZipStream.WriteAsync方法的典型用法代码示例。如果您正苦于以下问题:C# GZipStream.WriteAsync方法的具体用法?C# GZipStream.WriteAsync怎么用?C# GZipStream.WriteAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Compression.GZipStream
的用法示例。
在下文中一共展示了GZipStream.WriteAsync方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteAsync
/// <summary>
/// Compress a JSON string with base-64 encoded gzip compressed string.
/// </summary>
/// <param name="json">The JSON to write.</param>
/// <param name="stream">The stream.</param>
public static async Task WriteAsync(string json, Stream stream)
{
byte[] data = Encoding.UTF8.GetBytes(json);
using (GZipStream gzip = new GZipStream(stream, CompressionMode.Compress))
{
await gzip.WriteAsync(data, 0, data.Length);
}
}
示例2: CompressAsync
/// <summary>
/// Compresses a byte array.
/// </summary>
/// <param name="value">
/// The value.
/// </param>
/// <returns>
/// A byte array containing the compressed data.
/// </returns>
public async Task<byte[]> CompressAsync(byte[] value)
{
MemoryStream ms = new MemoryStream();
using (GZipStream gs = new GZipStream(ms, CompressionMode.Compress))
{
await gs.WriteAsync(value, 0, value.Length);
}
return ms.ToArray();
}
示例3: CompressAsync
public static async Task<byte[]> CompressAsync(this byte[] data, CancellationToken cancellationToken = default(CancellationToken)) {
byte[] compressesData;
using (var outputStream = new MemoryStream()) {
using (var zip = new GZipStream(outputStream, CompressionMode.Compress, true)) {
await zip.WriteAsync(data, 0, data.Length, cancellationToken);
}
await outputStream.FlushAsync(cancellationToken);
compressesData = outputStream.ToArray();
}
return compressesData;
}
示例4: CompressAsync
public static async Task<string> CompressAsync(string input)
{
using (var outputStream = new MemoryStream())
{
using (var stream = new GZipStream(outputStream, CompressionMode.Compress))
{
var bytes = Encoding.UTF8.GetBytes(input);
await stream.WriteAsync(bytes, 0, bytes.Length);
}
var output = outputStream.ToArray();
return Convert.ToBase64String(output);
}
}
示例5: Memory_Compress_Decompress_Async
public void Memory_Compress_Decompress_Async() {
var input = (byte[])PlainBytes.Clone();
byte[] compressed;
using(var outStream = new MemoryStream(input.Length)) {
using(var gzip = new GZipStream(outStream, CompressionMode.Compress)) {
gzip.WriteAsync(input, 0, input.Length).Wait();
}
compressed = outStream.ToArray();
Assert.IsNotNull(compressed);
Assert.IsTrue(compressed.Length > 0);
}
using(var gzip = new GZipStream(new MemoryStream(compressed), CompressionMode.Decompress)) {
var decompressed = With.TryFunctionAsync(() => gzip.ReadAllBytesAsync().Result);
Assert.IsTrue(decompressed.Length > 0);
Assert.AreEqual(PlainBytes, decompressed);
}
}
示例6: AddFileAsync
public async Task AddFileAsync(string name, Stream input, CancellationToken cancellationToken)
{
if (name == null) throw new ArgumentNullException(nameof(name));
if (input == null) throw new ArgumentNullException(nameof(input));
using (var data = new MemoryStream())
{
using (var zip = new GZipStream(data, CompressionMode.Compress, true))
{
var buffer = new byte[64 * 1024];
int readBytes;
while ((readBytes = await input.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) != 0)
{
await zip.WriteAsync(buffer, 0, readBytes, cancellationToken);
}
}
this.Data.Add(data.ToArray());
this.Header.Add(name, (int)data.Length);
}
}
示例7: CompressDataAsync
private static async Task<byte[]> CompressDataAsync(byte[] data)
{
using (var targetStream = new MemoryStream())
{
using (var stream = new GZipStream(targetStream, CompressionMode.Compress))
{
await stream.WriteAsync(data, 0, data.Length);
}
// Don't return earlier, or else the GZipStream isn't closed and we get corrupted data
return targetStream.ToArray();
}
}
示例8: FileAsync_Compress_Decompress_Async
public void FileAsync_Compress_Decompress_Async() {
var filename = FileTool.GetTempFileName();
using(var fs = FileAsync.OpenWrite(filename))
using(var gzip = new GZipStream(fs, CompressionMode.Compress)) {
gzip.WriteAsync(PlainBytes, 0, PlainBytes.Length).Wait();
}
var fi = new FileInfo(filename);
Assert.IsTrue(fi.Exists);
Assert.IsTrue(PlainBytes.Length > fi.Length);
using(var fs = FileAsync.OpenRead(filename))
using(var gzip = new GZipStream(fs, CompressionMode.Decompress, true)) {
var output = With.TryFunctionAsync(() => gzip.ReadAllBytesAsync().Result);
Assert.AreEqual(PlainBytes.Length, output.Length);
Assert.AreEqual(PlainBytes, output);
}
fi = new FileInfo(filename);
fi.Delete();
}
示例9: Compress
private static async Task<CompressionDetails> Compress(CompressionDetails compressionDetails)
{
byte[] data = compressionDetails.Bytes;
using (MemoryStream compressedDataStream = new MemoryStream())
{
using (GZipStream streamCompressed = new GZipStream(compressedDataStream, CompressionMode.Compress))
{
// write chunk in the compressed stream
await streamCompressed.WriteAsync(data, 0, data.Length);
}
byte[] compressedData = compressedDataStream.GetBuffer();
byte[] compressedDatasize = BitConverter.GetBytes(compressedData.Length);
CompressionDetails v = new CompressionDetails
{
Bytes = compressedData,
ChunkSize = compressionDetails.ChunkSize,
Sequence = compressionDetails.Sequence,
CompressedDataSize = compressedDatasize
};
return v;
}
}