3

我正在尝试通过WCF(一些 GB)发送大量数据。我想在使用 Streams 读取文件时压缩文件,但看起来 DeflateStream 有两种模式:

  • 压缩(写入流)
  • 解压(读取流)

这些模式都不适用于我的情况。我想从磁盘读取一个未压缩的文件并通过 WCF 返回一个压缩流。

有没有办法这样做,或者我必须使用临时文件(或 MemoryStream)?

是缺少功能还是由于某种原因不可能?

4

6 回答 6

3

看起来您正在尝试在读取文件时进行压缩。deflatestream 的写入方式,压缩必须作为写入的一部分进行。尝试包装您通过线路发送的流,而不是您从磁盘读取的流。如果它们相同,则需要一个中间流。

于 2012-05-09T16:23:03.110 回答
3

尝试使用这些方法来压缩和解压缩字节数组。

    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;
}
于 2012-05-09T16:33:41.010 回答
2

你应该有类似的东西:

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:\MyDir\MyFile.txt))
using (FileStream destinationStream = File.OpenWrite(@"C:\MyDir\MyCompressedFile.txt.cp"))
{
    CompressData(sourceStream, destinationStream)
}

另外,请注意,您可能必须更改应用程序的 .config 文件中的 WCF 设置,以允许传输非常大的内容。

于 2012-05-09T16:18:21.540 回答
1

您可以将 DeflateStream 包装在您自己的流中。每次您想从压缩流中读取数据时,都必须将字节输入 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) 中读取。这很丑陋,但它有效。

于 2014-04-10T23:08:04.910 回答
0

我试图创建一个 Stream ,无论何时Read被调用:

  • 从源文件中读取一大块数据
  • 将块写入DeflateStream 连接到 MemoryStream 的
  • 将 的内容复制MemoryStream到 Readbuffer参数。

当然,这将更加困难,因为两个流的大小并不相似。

最后我取消了这个选项,因为我没有办法在不完全压缩的情况下预测生成的压缩文件的大小。

但是,读取文件能够预测文件大小,因此也许可以使用另一种实现DeflateStream

希望它可以帮助其他迷失的灵魂......

于 2012-05-09T18:00:50.773 回答
0

适用于 blob 的 Azure API 具有 UploadStream(stream) 的替代方法。您可以使用 OpenWrite() 获取流。因此,现在您可以控制推送字节,因此可以在将内容流式传输到服务时进行压缩

using (var uploadStream = blob.OpenWrite())
using (var deflateStream = new DeflateStream(uploadStream, CompressionMode.Compress))
{
    stream.CopyTo(deflateStream);
}

我没有检查 WCF API,但如果你不能这样做,我会感到惊讶。

于 2018-03-02T15:12:05.183 回答