通过WCF发送GZIP流

时间:2014-04-22 11:04:23

标签: c# wcf gzip gzipoutputstream

以下是我的代码。

我设置了内容编码标头。然后使用gzip编码将文件流写入内存流。然后最后返回内存流。

然而,android,IOS和webbrowser都收到了流的损坏副本。它们都不能完全读取另一侧的解压缩流。我错过了哪个重要部分?

   public Stream GetFileStream(String path, String basePath)
    {
        FileInfo fi = new FileInfo(basePath + path);

        //WebOperationContext.Current.OutgoingResponse.ContentType = "application/x-gzip";
        WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Encoding","gzip");

        MemoryStream ms = new MemoryStream();
        GZipStream CompressStream = new GZipStream(ms, CompressionMode.Compress);

        // Get the stream of the source file.
        FileStream inFile = fi.OpenRead();

        // Prevent compressing hidden and   already compressed files.
        if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden)
            != FileAttributes.Hidden & fi.Extension != ".gz")
        { 
                    // Copy the source file into the compression stream.
                    inFile.CopyTo(CompressStream);

                    Log.d(String.Format("Compressed {0} from {1} to {2} bytes.",
                        fi.Name, fi.Length.ToString(), ms.Length.ToString()));        
        }
        ms.Position = 0;
        inFile.Close();
        return ms;
 }

1 个答案:

答案 0 :(得分:1)

我强烈建议发送字节数组。然后在客户端根据收到的字节数组创建一个zip流。

相关问题