我正在尝试通过WCF(一些GB)发送一大块数据。我想在使用流读取文件时压缩文件,但看起来 DeflateStream 有两种模式:
- 压缩(写入流)
- 解压缩(读取流)
这些模式在我的情况下都不起作用。我想从磁盘读取未压缩的文件,并通过 WCF 返回压缩流。
有没有办法这样做,或者我必须使用临时文件(或内存流)?
是缺少功能还是由于某种原因无法实现?
看起来您在读取文件时正在尝试压缩。 在写入放气流的方式中,压缩必须作为写入的一部分进行。 尝试包装通过网络发送的流,而不是从磁盘读取的流。 如果它们相同,则需要一个中间流。
尝试使用这些方法来压缩和解压缩字节数组。
private static byte[] Compress(byte[] data)
{
byte[] retVal;
using (MemoryStream compressedMemoryStream = new MemoryStream())
{
DeflateStream compressStream = new DeflateStream(compressedMemoryStream, CompressionMode.Compress, true);
compressStream.Write(data, 0, data.Length);
compressStream.Close();
retVal = new byte[compressedMemoryStream.Length];
compressedMemoryStream.Position = 0L;
compressedMemoryStream.Read(retVal, 0, retVal.Length);
compressedMemoryStream.Close();
compressStream.Close();
}
return retVal;
}
private static byte[] Decompress(byte[] data)
{
byte[] retVal;
using (MemoryStream compressedMemoryStream = new MemoryStream())
{
compressedMemoryStream.Write(data, 0, data.Length);
compressedMemoryStream.Position = 0L;
MemoryStream decompressedMemoryStream = new MemoryStream();
DeflateStream decompressStream = new DeflateStream(compressedMemoryStream, CompressionMode.Decompress);
decompressStream.CopyTo(decompressedMemoryStream);
retVal = new byte[decompressedMemoryStream.Length];
decompressedMemoryStream.Position = 0L;
decompressedMemoryStream.Read(retVal, 0, retVal.Length);
compressedMemoryStream.Close();
decompressedMemoryStream.Close();
decompressStream.Close();
}
return retVal;
}
你应该有这样的东西:
public void CompressData(Stream uncompressedSourceStream, Stream compressedDestinationStream)
{
using (DeflateStream compressionStream = new DeflateStream(compressedDestinationStream, CompressionMode.Compress))
{
uncompressedSourceStream.CopyTo(compressionStream);
}
}
public void DecompressData(Stream compressedSourceStream, Stream uncompressedDestinationStream)
{
using (DeflateStream decompressionStream = new DeflateStream(uncompressedDestinationStream, CompressionMode.Decompress))
{
compressedSourceStream.CopyTo(decompressionStream);
}
}
using (FileStream sourceStream = File.OpenRead(@"C:MyDirMyFile.txt))
using (FileStream destinationStream = File.OpenWrite(@"C:MyDirMyCompressedFile.txt.cp"))
{
CompressData(sourceStream, destinationStream)
}
另外,请注意,您可能必须更改应用程序的 .config 文件中的 WCF 设置,以允许传输非常大的内容。
您可以将 DeflateStream 包装在您自己的流中。每次要从压缩流中读取时,都必须将字节馈送到压缩流中,直到它写入缓冲区。然后,可以从该缓冲区返回字节。
public class CompressingStream : Stream
{
private readonly DeflateStream _deflateStream;
private readonly MemoryStream _buffer;
private Stream _inputStream;
private readonly byte[] _fileBuffer = new byte[64 * 1024];
public CompressingStream(Stream inputStream)
{
_inputStream = inputStream;
_buffer = new MemoryStream();
_deflateStream = new DeflateStream(_buffer, CompressionMode.Compress, true);
}
public override int Read(byte[] buffer, int offset, int count)
{
while (true)
{
var read = _buffer.Read(buffer, offset, count);
if (read > 0) return read;
if (_inputStream == null) return 0;
_buffer.Position = 0;
read = _inputStream.Read(_fileBuffer, 0, _fileBuffer.Length);
if (read == 0)
{
_inputStream.Close();
_inputStream = null;
_deflateStream.Close();
}
else
{
_deflateStream.Write(_fileBuffer, 0, read);
}
_buffer.SetLength(_buffer.Position);
_buffer.Position = 0;
}
}
public override bool CanRead
{
get { return true; }
}
#region Remaining overrides...
}
每当 wcf 从流中读取时,压缩流都会写入压缩 DeflateStream,直到它从输出缓冲区读取 (_buffer)。这很丑陋,但它有效。
我试图创建一个 Stream,每当调用Read
时:
- 从源文件中读取数据块
- 将区块写入连接到内存流的
DeflateStream
- 将
MemoryStream
的内容复制到读取buffer
参数。
当然,这将更加困难,因为两个流的大小并不相似。
最后,我忽略了此选项,因为我发现如果不在开始时完全压缩它,就无法预测生成的压缩文件的大小。
但是,读取文件能够预测文件大小,因此也许可以通过另一种实现DeflateStream
来实现。
希望它能帮助其他迷失的灵魂......
API for blobs 有一个替代 UploadStream(stream) 的替代方案。您可以使用 OpenWrite() 获取流。因此,现在您可以控制推送字节,因此可以在将内容流式传输到服务时进行压缩
using (var uploadStream = blob.OpenWrite())
using (var deflateStream = new DeflateStream(uploadStream, CompressionMode.Compress))
{
stream.CopyTo(deflateStream);
}
我没有检查过 WCF API,但如果你不能做同样的事情,我会感到惊讶。