GZipStream.Write 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
Write(ReadOnlySpan<Byte>) |
从一个只读字节范围将字节的序列写入当前 GZip 流,并按写入的字节数向前移动此 GZip 流中的当前位置。 |
Write(Byte[], Int32, Int32) |
从指定的字节数组中将压缩的字节写入基础 GZip 流。 |
Write(ReadOnlySpan<Byte>)
- Source:
- GZipStream.cs
- Source:
- GZipStream.cs
- Source:
- GZipStream.cs
从一个只读字节范围将字节的序列写入当前 GZip 流,并按写入的字节数向前移动此 GZip 流中的当前位置。
public:
override void Write(ReadOnlySpan<System::Byte> buffer);
public override void Write (ReadOnlySpan<byte> buffer);
override this.Write : ReadOnlySpan<byte> -> unit
Public Overrides Sub Write (buffer As ReadOnlySpan(Of Byte))
参数
- buffer
- ReadOnlySpan<Byte>
内存的区域。 此方法将此区域的内容复制到当前 GZip 流。
注解
CanWrite使用 属性确定当前实例是否支持写入。 WriteAsync使用 方法以异步方式写入当前流。
如果写入操作成功,GZip 流中的位置将按写入的字节数前进。 如果发生异常,GZip 流中的位置保持不变。
适用于
Write(Byte[], Int32, Int32)
- Source:
- GZipStream.cs
- Source:
- GZipStream.cs
- Source:
- GZipStream.cs
从指定的字节数组中将压缩的字节写入基础 GZip 流。
public:
override void Write(cli::array <System::Byte> ^ array, int offset, int count);
public:
override void Write(cli::array <System::Byte> ^ buffer, int offset, int count);
public override void Write (byte[] array, int offset, int count);
public override void Write (byte[] buffer, int offset, int count);
override this.Write : byte[] * int * int -> unit
override this.Write : byte[] * int * int -> unit
Public Overrides Sub Write (array As Byte(), offset As Integer, count As Integer)
Public Overrides Sub Write (buffer As Byte(), offset As Integer, count As Integer)
参数
- arraybuffer
- Byte[]
包含要压缩的数据的缓冲区。
- offset
- Int32
将从中读取字节的字节偏移量。
- count
- Int32
最多写入的字节数。
例外
由于流已关闭,因此无法执行写入操作。
示例
以下示例演示如何使用 Read 和 Write 方法压缩和解压缩字节。
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
public static class MemoryWriteReadExample
{
private const string Message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
private static readonly byte[] s_messageBytes = Encoding.ASCII.GetBytes(Message);
public static void Run()
{
Console.WriteLine($"The original string length is {s_messageBytes.Length} bytes.");
using var stream = new MemoryStream();
CompressBytesToStream(stream);
Console.WriteLine($"The compressed stream length is {stream.Length} bytes.");
int decompressedLength = DecompressStreamToBytes(stream);
Console.WriteLine($"The decompressed string length is {decompressedLength} bytes, same as the original length.");
/*
Output:
The original string length is 445 bytes.
The compressed stream length is 282 bytes.
The decompressed string length is 445 bytes, same as the original length.
*/
}
private static void CompressBytesToStream(Stream stream)
{
using var compressor = new GZipStream(stream, CompressionLevel.SmallestSize, leaveOpen: true);
compressor.Write(s_messageBytes, 0, s_messageBytes.Length);
}
private static int DecompressStreamToBytes(Stream stream)
{
stream.Position = 0;
int bufferSize = 512;
byte[] buffer = new byte[bufferSize];
using var gzipStream = new GZipStream(stream, CompressionMode.Decompress);
int totalRead = 0;
while (totalRead < buffer.Length)
{
int bytesRead = gzipStream.Read(buffer.AsSpan(totalRead));
if (bytesRead == 0) break;
totalRead += bytesRead;
}
return totalRead;
}
}
open System.IO
open System.IO.Compression
open System.Text
let message =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
let s_messageBytes = Encoding.ASCII.GetBytes message
let compressBytesToStream stream =
use compressor =
new GZipStream(stream, CompressionLevel.SmallestSize, leaveOpen = true)
compressor.Write(s_messageBytes, 0, s_messageBytes.Length)
let decompressStreamToBytes (stream: Stream) =
stream.Position <- 0
let bufferSize = 512
let decompressedBytes = Array.zeroCreate bufferSize
use decompressor = new GZipStream(stream, CompressionMode.Decompress)
decompressor.Read(decompressedBytes, 0, bufferSize)
[<EntryPoint>]
let main _ =
printfn $"The original string length is {s_messageBytes.Length} bytes."
use stream = new MemoryStream()
compressBytesToStream stream
printfn $"The compressed stream length is {stream.Length} bytes."
let decompressedLength = decompressStreamToBytes stream
printfn $"The decompressed string length is {decompressedLength} bytes, same as the original length."
0
// Output:
// The original string length is 445 bytes.
// The compressed stream length is 282 bytes.
// The decompressed string length is 445 bytes, same as the original length.
Imports System.IO
Imports System.IO.Compression
Imports System.Text
Module MemoryWriteReadExample
Private Const Message As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Private ReadOnly s_messageBytes As Byte() = Encoding.ASCII.GetBytes(Message)
Sub Main()
Console.WriteLine($"The original string length is {s_messageBytes.Length} bytes.")
Using stream = New MemoryStream()
CompressBytesToStream(stream)
Console.WriteLine($"The compressed stream length is {stream.Length} bytes.")
Dim decompressedLength As Integer = DecompressStreamToBytes(stream)
Console.WriteLine($"The decompressed string length is {decompressedLength} bytes, same as the original length.")
End Using
' Output:
' The original string length is 445 bytes.
' The compressed stream length is 282 bytes.
' The decompressed string length is 445 bytes, same as the original length.
End Sub
Private Sub CompressBytesToStream(ByVal stream As Stream)
Using compressor = New GZipStream(stream, CompressionLevel.SmallestSize, leaveOpen:=True)
compressor.Write(s_messageBytes, 0, s_messageBytes.Length)
End Using
End Sub
Private Function DecompressStreamToBytes(ByVal stream As Stream) As Integer
stream.Position = 0
Dim bufferSize As Integer = 512
Dim decompressedBytes As Byte() = New Byte(bufferSize - 1) {}
Using decompressor = New GZipStream(stream, CompressionMode.Decompress)
Dim length As Integer = decompressor.Read(decompressedBytes, 0, bufferSize)
Return length
End Using
End Function
End Module
注解
写入操作可能不会立即发生,而是在达到缓冲区大小或调用 或 Close 方法之前Flush缓冲。