下面是我的代码。
我设置了内容编码 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/