GZipStream.EndRead(IAsyncResult) 方法
定义
等待挂起的异步读取完成。Waits for the pending asynchronous read to complete. (请考虑改用 ReadAsync(Byte[], Int32, Int32) 方法。)(Consider using the ReadAsync(Byte[], Int32, Int32) method instead.)
public:
override int EndRead(IAsyncResult ^ asyncResult);
public:
override int EndRead(IAsyncResult ^ async_result);
public override int EndRead (IAsyncResult asyncResult);
public override int EndRead (IAsyncResult async_result);
override this.EndRead : IAsyncResult -> int
override this.EndRead : IAsyncResult -> int
Public Overrides Function EndRead (asyncResult As IAsyncResult) As Integer
Public Overrides Function EndRead (async_result As IAsyncResult) As Integer
参数
- asyncResultasync_result
- IAsyncResult
对要完成的挂起异步请求的引用。The reference to the pending asynchronous request to finish.
返回
从流中读取的字节数,介于 0(零)和你请求的字节数之间。The number of bytes read from the stream, between 0 (zero) and the number of bytes you requested. GZipStream 仅在流的末尾返回零 (0);否则将一直阻塞,只到至少有一个字节可用。GZipStream returns 0 only at the end of the stream; otherwise, it blocks until at least one byte is available.
例外
asyncResult 为 null。asyncResult is null.
asyncResult 不源于当前流的 BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) 方法。asyncResult did not originate from a BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) method on the current stream.
由于流已关闭,因此无法执行结束操作。The end operation cannot be performed because the stream is closed.
示例
下面的代码示例演示如何使用 GZipStream 类压缩和解压缩文件。The following code example shows how to use the GZipStream class to compress and decompress a file.
using System;
using System.IO;
using System.IO.Compression;
public class Program
{
private static string directoryPath = @".\temp";
public static void Main()
{
DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);
Compress(directorySelected);
foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
{
Decompress(fileToDecompress);
}
}
public static void Compress(DirectoryInfo directorySelected)
{
foreach (FileInfo fileToCompress in directorySelected.GetFiles())
{
using (FileStream originalFileStream = fileToCompress.OpenRead())
{
if ((File.GetAttributes(fileToCompress.FullName) &
FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
{
using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
{
using (GZipStream compressionStream = new GZipStream(compressedFileStream,
CompressionMode.Compress))
{
originalFileStream.CopyTo(compressionStream);
}
}
FileInfo info = new FileInfo(directoryPath + Path.DirectorySeparatorChar + fileToCompress.Name + ".gz");
Console.WriteLine($"Compressed {fileToCompress.Name} from {fileToCompress.Length.ToString()} to {info.Length.ToString()} bytes.");
}
}
}
}
public static void Decompress(FileInfo fileToDecompress)
{
using (FileStream originalFileStream = fileToDecompress.OpenRead())
{
string currentFileName = fileToDecompress.FullName;
string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);
using (FileStream decompressedFileStream = File.Create(newFileName))
{
using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedFileStream);
Console.WriteLine($"Decompressed: {fileToDecompress.Name}");
}
}
}
}
}
Imports System.IO
Imports System.IO.Compression
Module Module1
Private directoryPath As String = ".\temp"
Public Sub Main()
Dim directorySelected As New DirectoryInfo(directoryPath)
Compress(directorySelected)
For Each fileToDecompress As FileInfo In directorySelected.GetFiles("*.gz")
Decompress(fileToDecompress)
Next
End Sub
Public Sub Compress(directorySelected As DirectoryInfo)
For Each fileToCompress As FileInfo In directorySelected.GetFiles()
Using originalFileStream As FileStream = fileToCompress.OpenRead()
If (File.GetAttributes(fileToCompress.FullName) And FileAttributes.Hidden) <> FileAttributes.Hidden And fileToCompress.Extension <> ".gz" Then
Using compressedFileStream As FileStream = File.Create(fileToCompress.FullName & ".gz")
Using compressionStream As New GZipStream(compressedFileStream, CompressionMode.Compress)
originalFileStream.CopyTo(compressionStream)
End Using
End Using
Dim info As New FileInfo(directoryPath & Path.DirectorySeparatorChar & fileToCompress.Name & ".gz")
Console.WriteLine($"Compressed {fileToCompress.Name} from {fileToCompress.Length.ToString()} to {info.Length.ToString()} bytes.")
End If
End Using
Next
End Sub
Private Sub Decompress(ByVal fileToDecompress As FileInfo)
Using originalFileStream As FileStream = fileToDecompress.OpenRead()
Dim currentFileName As String = fileToDecompress.FullName
Dim newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length)
Using decompressedFileStream As FileStream = File.Create(newFileName)
Using decompressionStream As GZipStream = New GZipStream(originalFileStream, CompressionMode.Decompress)
decompressionStream.CopyTo(decompressedFileStream)
Console.WriteLine($"Decompressed: {fileToDecompress.Name}")
End Using
End Using
End Using
End Sub
End Module
注解
从 .NET Framework 4.5 开始,可以使用方法执行异步读取操作 Stream.ReadAsync 。Starting with the .NET Framework 4.5, you can perform asynchronous read operations by using the Stream.ReadAsync method. EndRead.NET Framework 4.5 中仍提供了方法来支持旧代码,但你可以使用新的异步方法更轻松地实现异步 i/o 操作。The EndRead method is still available in .NET Framework 4.5 to support legacy code; however, you can implement asynchronous I/O operations more easily by using the new async methods. 有关详细信息,请参阅异步文件 I/O。For more information, see Asynchronous File I/O.
调用此方法以确定从流中读取的字节数。Call this method to determine how many bytes were read from the stream. 可以调用此方法一次,以返回对和的调用之间的读取字节量 BeginRead EndRead 。This method can be called once to return the amount of bytes read between calls to BeginRead and EndRead.
此方法将一直阻塞,直到 I/O 操作已完成。This method blocks until the I/O operation has completed.