WebService 数据压缩

本文介绍了一个用于压缩WebService数据的实用工具类CompressionExtension。该类通过在数据传输前后进行压缩与解压操作,有效减小了传输的数据量。文章还提供了具体的实现代码及使用说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很长时间没有更新博客了
一直在忙这做项目,累啊,苦啊
现在刚好忙完了,赶紧看看兄弟姐妹们的博客,其中看了 刚刚兄有关 Web服务初探:用Demo学Web服务系列,使自己才更清楚的了解了WebService,Thx!
然而项目中有时数据大,所以在网上搜了个压缩数据的,特此贴出来,方便自己日后用到,也希望能给大家一些帮助,呵呵
CompressionExtension.cs:
None.gif using  System;
None.gif
using  System.IO;
None.gif
using  System.Text ;
None.gif
using  System.Web.Services;
None.gif
using  System.Web.Services.Protocols ;
None.gif
using  ICSharpCode.SharpZipLib.Checksums;
None.gif
using  ICSharpCode.SharpZipLib.Zip;
None.gif
using  ICSharpCode.SharpZipLib.GZip;
None.gif
using  System.Xml ;
None.gif
None.gif
namespace  BLL
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for ConpressionExtension.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class CompressionExtension : System.Web.Services.Protocols.SoapExtension
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Stream oldStream;
InBlock.gif        Stream newStream;
InBlock.gif    
InBlock.gif
InBlock.gif        
public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return attribute;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Get the Type
InBlock.gif
        public override object GetInitializer(Type t) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return typeof(CompressionExtension);
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
// Get the CompressionExtensionAttribute
InBlock.gif
        public override void Initialize(object initializer) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CompressionExtensionAttribute attribute 
= (CompressionExtensionAttribute) initializer;
InBlock.gif
InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Process the SOAP Message
InBlock.gif
        public override void ProcessMessage(SoapMessage message) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Check for the various SOAP Message Stages 
InBlock.gif
            switch (message.Stage) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                
case SoapMessageStage.BeforeSerialize:
InBlock.gif                    
break;
InBlock.gif
InBlock.gif                
case SoapMessageStage.AfterSerialize:
InBlock.gif                    
// ZIP the contents of the SOAP Body after it has
InBlock.gif                    
// been serialized
InBlock.gif
                    Zip();
InBlock.gif                    
break;
InBlock.gif
InBlock.gif                
case SoapMessageStage.BeforeDeserialize:
InBlock.gif                    
// Unzip the contents of the SOAP Body before it is
InBlock.gif                    
// deserialized
InBlock.gif
                    Unzip();
InBlock.gif                    
break;
InBlock.gif
InBlock.gif                
case SoapMessageStage.AfterDeserialize:
InBlock.gif                    
break;
InBlock.gif
InBlock.gif                
default:
InBlock.gif                    
throw new Exception("invalid stage");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Gives us the ability to get hold of the RAW SOAP message
InBlock.gif
        public override Stream ChainStream( Stream stream ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oldStream 
= stream;
InBlock.gif            newStream 
= new MemoryStream();
InBlock.gif            
return newStream;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Utility method to copy streams
InBlock.gif
        void Copy(Stream from, Stream to) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TextReader reader 
= new StreamReader(from);
InBlock.gif            TextWriter writer 
= new StreamWriter(to);
InBlock.gif            writer.WriteLine(reader.ReadToEnd());
InBlock.gif            writer.Flush();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif    
InBlock.gif        
// Zip the SOAP Body
InBlock.gif
        private void Zip() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            newStream.Position 
= 0;
InBlock.gif            
// Zip the SOAP Body
InBlock.gif
            newStream = ZipSoap(newStream);
InBlock.gif            
// Copy the streams
InBlock.gif
            Copy(newStream, oldStream);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// The actual ZIP method
InBlock.gif
        private byte[] Zip(string stringToZip) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToZip);
InBlock.gif            MemoryStream ms 
= new MemoryStream();
InBlock.gif
InBlock.gif            
// Check the #ziplib docs for more information
InBlock.gif
            ZipOutputStream zipOut = new ZipOutputStream( ms ) ;
InBlock.gif            ZipEntry ZipEntry 
= new ZipEntry("ZippedFile");
InBlock.gif            zipOut.PutNextEntry(ZipEntry);
InBlock.gif            zipOut.SetLevel(
9);
InBlock.gif            zipOut.Write(inputByteArray, 
0 , inputByteArray.Length ) ;     
InBlock.gif            zipOut.Finish();
InBlock.gif            zipOut.Close();
InBlock.gif
InBlock.gif            
// Return the zipped contents
InBlock.gif
            return ms.ToArray();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Select and Zip the appropriate parts of the SOAP message
InBlock.gif
        public MemoryStream ZipSoap(Stream streamToZip) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            streamToZip.Position 
= 0;
InBlock.gif            
// Load a XML Reader
InBlock.gif
            XmlTextReader reader = new XmlTextReader(streamToZip);
InBlock.gif            XmlDocument dom 
= new XmlDocument();
InBlock.gif            dom.Load(reader);
InBlock.gif            
// Load a NamespaceManager to enable XPath selection
InBlock.gif
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(dom.NameTable);
InBlock.gif            nsmgr.AddNamespace(
"soap""http://schemas.xmlsoap.org/soap/envelope/");
InBlock.gif            XmlNode node 
= dom.SelectSingleNode("//soap:Body", nsmgr);
InBlock.gif            
// Select the contents within the method defined in the SOAP body
InBlock.gif
            node = node.FirstChild.FirstChild;
InBlock.gif            
// Check if there are any nodes selected
InBlock.gif
            while( node != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if( node.InnerXml.Length > 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// Zip the data
InBlock.gif
                    byte[] outData = Zip(node.InnerXml);
InBlock.gif                    
// Convert it to Base64 for transfer over the internet
InBlock.gif
                    node.InnerXml = Convert.ToBase64String(outData) ;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
// Move to the next parameter
InBlock.gif
                node = node.NextSibling ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            MemoryStream ms 
= new MemoryStream();
InBlock.gif            
// Save the updated data
InBlock.gif
            dom.Save(ms);
InBlock.gif            ms.Position 
= 0;
InBlock.gif      
InBlock.gif            
return ms;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Unzip the SOAP Body
InBlock.gif
        private void Unzip() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MemoryStream unzipedStream 
= new MemoryStream();
InBlock.gif        
InBlock.gif            TextReader reader 
= new StreamReader(oldStream);
InBlock.gif            TextWriter writer 
= new StreamWriter(unzipedStream);
InBlock.gif            writer.WriteLine(reader.ReadToEnd());
InBlock.gif            writer.Flush();
InBlock.gif            
// Unzip the SOAP Body
InBlock.gif
            unzipedStream = UnzipSoap(unzipedStream);
InBlock.gif            
// Copy the streams
InBlock.gif
            Copy(unzipedStream, newStream);
InBlock.gif     
InBlock.gif            newStream.Position 
= 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Actual Unzip logic
InBlock.gif
        private byte[] Unzip(string stringToUnzip) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Decode the Base64 encoding
InBlock.gif
            byte[] inputByteArray = Convert.FromBase64String( stringToUnzip ) ;
InBlock.gif            MemoryStream ms 
= new MemoryStream(inputByteArray) ;
InBlock.gif            MemoryStream ret 
= new MemoryStream();
InBlock.gif
InBlock.gif            
// Refer to #ziplib documentation for more info on this
InBlock.gif
            ZipInputStream zipIn = new ZipInputStream(ms);
InBlock.gif            ZipEntry theEntry 
= zipIn.GetNextEntry();
InBlock.gif            Byte[] buffer 
= new Byte[2048] ;
InBlock.gif            
int size = 2048;
InBlock.gif            
while (true
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                size 
= zipIn.Read(buffer, 0, buffer.Length);
InBlock.gif                
if (size > 0
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ret.Write(buffer, 
0, size);
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return ret.ToArray();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Unzip the SOAP Body
InBlock.gif
        public MemoryStream UnzipSoap(Stream streamToUnzip) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            streamToUnzip.Position 
= 0;
InBlock.gif            
// Load a XmlReader
InBlock.gif
            XmlTextReader reader = new XmlTextReader(streamToUnzip);
InBlock.gif            XmlDocument dom 
= new XmlDocument();
InBlock.gif            dom.Load(reader);
InBlock.gif
InBlock.gif            XmlNamespaceManager nsmgr 
= new XmlNamespaceManager(dom.NameTable);
InBlock.gif            nsmgr.AddNamespace(
"soap""http://schemas.xmlsoap.org/soap/envelope/");
InBlock.gif            
// Select the SOAP Body node 
InBlock.gif
            XmlNode node = dom.SelectSingleNode("//soap:Body", nsmgr);
InBlock.gif            node 
= node.FirstChild.FirstChild;
InBlock.gif
InBlock.gif            
// Check if node exists
InBlock.gif
            while( node != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if( node.InnerXml.Length >0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// Send the node's contents to be unziped
InBlock.gif
                    byte[] outData = Unzip(node.InnerXml);
InBlock.gif                    
string sTmp = Encoding.UTF8.GetString(outData);
InBlock.gif                    node.InnerXml 
= sTmp;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
// Move to the next parameter 
InBlock.gif
                node = node.NextSibling ;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            MemoryStream ms 
= new MemoryStream();
InBlock.gif      
InBlock.gif            dom.Save(ms);
InBlock.gif            ms.Position 
= 0;
InBlock.gif
InBlock.gif            
return ms;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
CompressionExtensionAttribute.cs:
None.gif using  System;
None.gif
using  System.Web.Services;
None.gif
using  System.Web.Services.Protocols;
None.gif
None.gif
namespace  BLL
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for CompressionExtensionAttribute.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    
InBlock.gif    
// Make the Attribute only Applicable to Methods
InBlock.gif
    [AttributeUsage(AttributeTargets.Method)]
InBlock.gif    
public class CompressionExtensionAttribute : System.Web.Services.Protocols.SoapExtensionAttribute
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
InBlock.gif        
private int priority;
InBlock.gif
InBlock.gif        
// Override the base class properties
InBlock.gif
        public override Type ExtensionType 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn typeof(CompressionExtension); }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override int Priority 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
return priority; 
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                priority 
= value; 
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
在server | Client 都需要
调用很简单,只要在代理类中web方法加个:
None.gif [CompressionExtension]

原文地址: http://www.123aspx.com/redir.aspx?res=29459

转载于:https://www.cnblogs.com/Bryant2008/archive/2007/04/10/707199.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值