c# - 通过 WCF 发送 GZIP 流

标签 c# wcf gzip gzipoutputstream

下面是我的代码。

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

但是,android、IOS 和网络浏览器都收到流的损坏副本。他们都无法完全读取另一侧的解压缩流。我缺少哪个重要部分?

   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;
 }

最佳答案

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

关于c# - 通过 WCF 发送 GZIP 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23217673/

相关文章:

c# - 如何在不更改记录数据的情况下更新sql server时间戳列

c# 为一组对象获取属性并确保所有对象的值相同

c# - 如何在 WCF 服务中注入(inject)依赖项

php - 在 Android 中对字符串进行编码,以便 PHP 能够对其进行 gz 解压缩?

c# - Microsoft 报表查看器对象

c# - : UITableView and keyboard scrolling issue 的 Xamarin 解决方案

c# - Wcf 从操作中获取原始请求

c# - 使用 IParameterInspector.BeforeCall(string operationName, object[] inputs) 中止调用

javascript - 如何在 javascript 或 jquery 中转换图像字符串?

ant - 如何将参数值传递给maven gzip ant任务?