您的位置:程序门 -> .net技术 -> web services



需要webservice传输文件,需求1断点续传2加密3压缩


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


需要webservice传输文件,需求1断点续传2加密3压缩[已结贴,结贴人:leitoyn]
发表于:2008-01-10 11:34:38 楼主
需要webservice传输文件,需求1断点续传2加密3压缩
不要求多线程,当然文件大小会限制在10m内,文件校验也是必须的。
不知道能否开发成控件??
如果有相关资料的也可贴到此。最好是原理要点解说,源码或者部分实例段。
不要求完整,部分也可。
发表于:2008-01-10 11:53:371楼 得分:17
    ///   <summary>
              ///   压缩
              ///   </summary>
              ///   <param   name="zipfilepath"> </param>
              ///   <param   name="sourcefilepath"> </param>
              ///   <returns> </returns>
              public   int   zip(string   zipfilepath,   string   sourcefilepath)
              {
                      zipoutputstream   zipstream   =   null;
                      filestream   streamwriter   =   null;


                      try
                      {

                              crc32   crc32   =   new   crc32();


                              zipstream   =   new   zipoutputstream(file.create(zipfilepath));


                              zipstream.setlevel(7);

                              //specify   password
                              //if   (password   !=   null   &&   password.trim().length   >   0)
                              //{
                              //         zipstream.password   =   password;
                              //}


                              //check   whether   the   file   exists
                              if   (!file.exists(sourcefilepath))
                              {
                                      throw   new   filenotfoundexception(sourcefilepath);
                              }

                              //read   the   file   to   stream
                              streamwriter   =   file.openread(sourcefilepath);
                              byte[]   buffer   =   new   byte[streamwriter.length];
                              streamwriter.read(buffer,   0,   buffer.length);
                              streamwriter.close();

                              //specify   zipentry
                              crc32.reset();
                              crc32.update(buffer);
                              zipentry   zipentry   =   new   zipentry(path.combine("错误报告",   path.getfilename(sourcefilepath)));
                              zipentry.datetime   =   datetime.now;
                              zipentry.size   =   buffer.length;
                              zipentry.crc   =   crc32.value;

                              //put   file   info   into   zip   stream
                              zipstream.putnextentry(zipentry);

                              //put   file   data   into   zip   stream
                              zipstream.write(buffer,   0,   buffer.length);


                      }
                      catch
                      {
                              throw;
                      }
                      finally
                      {
                              //clear   resource
                              if   (streamwriter   !=   null)
                              {
                                      streamwriter.close();
                              }
                              if   (zipstream   !=   null)
                              {
                                      zipstream.finish();
                                      zipstream.close();
                              }
                      }

                      return       0;

              }

              ///   <summary>
              ///   解压缩
              ///   </summary>
              ///   <param   name="aimfolder"> </param>
              ///   <param   name="zipfilepath"> </param>
              ///   <returns> </returns>
              public   int   unzip(string   aimfolder,   string   zipfilepath)
              {
                      zipinputstream   zipstream   =   null;
                      zipentry   zipentry   =   null;
                      filestream   streamwriter   =   null;
                      int   count   =   0;

                      try
                      {
                              zipstream   =   new   zipinputstream(file.openread(zipfilepath));
                              //zipstream.password   =   password;

                              while   ((zipentry   =   zipstream.getnextentry())   !=   null)
                              {
                                      string   zipfiledirectory   =   path.getdirectoryname(zipentry.name);
                                      string   destfiledirectory   =   path.combine(aimfolder,   zipfiledirectory);
                                      if   (!directory.exists(destfiledirectory))
                                      {
                                              directory.createdirectory(destfiledirectory);
                                      }

                                      string   filename   =   path.getfilename(zipentry.name);
                                      if   (filename.length   >   0)
                                      {
                                              string   destfilepath   =   path.combine(destfiledirectory,   filename);

                                              streamwriter   =   file.create(destfilepath);
                                              int   size   =   2048;
                                              byte[]   data   =   new   byte[2048];
                                              int   extractcount   =   0;
                                              while   (true)
                                              {
                                                      size   =   zipstream.read(data,   0,   data.length);
                                                      if   (size   >   0)
                                                      {
                                                              streamwriter.write(data,   0,   size);
                                                      }
                                                      else
                                                      {
                                                              break;
                                                      }
                                                      extractcount   +=   size;
                                              }

                                              streamwriter.flush();
                                              streamwriter.close();
                                              count++;
                                      }
                              }
                      }
                      catch
                      {
                              throw;
                      }
                      finally
                      {
                              if   (zipstream   !=   null)
                              {
                                      zipstream.close();
                              }

                              if   (streamwriter   !=   null)
                              {
                                      streamwriter.close();
                              }
                      }

                      return   count;
              }
发表于:2008-01-10 11:55:222楼 得分:0
using   system;
using   system.io;
using   system.security.cryptography;
using   system.windows.forms;
using   system.text;

namespace   fomlogon
{
///   <summary>
///   class1   的摘要说明。
///   </summary>
public   class   cencrypt  
{
public   cencrypt()
{
}
///   <summary>
///   对文件加密采用算法
///   </summary>
///   <param   name="sy"> 创建一个公钥和一个私钥   </param>
private   static   void   passwordtobyte(symmetricalgorithm   sy)
{
byte[]   b=   new   byte[8];
byte[]   c   =   new   byte[8];
encoding   ascii=   encoding.ascii;
byte[]   encodebyte   =   ascii.getbytes("zhonghlning");
byte[]   encodebyte2   =   ascii.getbytes("zhonghlning");

topassbyte(   b,encodebyte);
topassbyte(   c,encodebyte2);

sy.key=b;
sy.iv   =   c   ;
}
///   <summary>
///  
///   </summary>
///   <param   name="destination"> 字节数 </param>
///   <param   name="source"> 编码字符数组 </param>
public   static   void   topassbyte(   byte[]   destination,byte[]   source)
{
if   (destination.length> =source.length)
{
for(int   i=0;i <source.length;   i++)
{
destination[i]   =   source[i];
}
}
else
{
for(int   i=0;i <destination.length;i++)
{
destination[i]   =   source[i];
}
}
}

///   <summary>
///   将一个文件加密并生成一个新文件
///   </summary>
///   <param   name="inname"> 需要加密文件的全路径 </param>
///   <param   name="outname"> 输出加密文件的全路径 </param>
public   static   void   encryptdata(string   inname,   string   outname)
{        
//创建输入流和输出流
filestream   fin   =   new   filestream(inname,   filemode.open,   fileaccess.read);
filestream   fout   =   new   filestream(outname,   filemode.openorcreate,   fileaccess.write);
fout.setlength(0);
             
//读写的缓存区
byte[]   bin   =   new   byte[1000];   //加密的临时存储区
long   rdlen   =   0;                             //初始化写出流的长度
long   totlen   =   fin.length;         //输入流的长度
int   len;                                           //某一个时间段完成的写出流长度
 
/*
  *     symmetricalgorithm为算法类型类(抽象)
  *   descryptoserviceprovider为算法实现类   ,是算法类des的子类
  *     类层次关系如下
  *     system.security.cryptography.symmetricalgorithm
    system.security.cryptography.des
system.security.cryptography.descryptoserviceprovider
*/
symmetricalgorithm   des   =   new   descryptoserviceprovider();        

//给key和iv赋值
passwordtobyte(des);
//创建加密器
icryptotransform   transform   =     des.createencryptor(des.key,des.iv);
//使用加密器和输出流,创建一个加密输出流
cryptostream   encstream   =   new   cryptostream(fout,   transform,   cryptostreammode.write);
 
//循环读出读出输入流到内存中,然后加密流将一段内存加密后写到输出流
while(rdlen   <   totlen)
{
len   =   fin.read(bin,   0,   1000);
encstream.write(bin,   0,   len);
rdlen   =   rdlen   +   len;

}
                        //关闭流,释放资源
encstream.close();    
fout.close();
fin.close();                                      
}

                ///   <summary>
                ///解密
                ///   </summary>
                ///   <param   name="inname"> </param>
                ///   <param   name="outname"> </param>
public   static   void   decryptdata(string   inname,string   outname)
{        
//创建输入流和输出流
filestream   fin   =   new   filestream(inname,   filemode.open,   fileaccess.read);
filestream   fout   =   new   filestream(outname,   filemode.openorcreate,   fileaccess.write);
fout.setlength(0);
             
//读写的缓存区
byte[]   bin   =   new   byte[10240];   //加密的临时存储区
        long   rdlen   =   0;                             //初始化写出流的长度
long   totlen   =   fin.length;         //输入流的长度
int   len;                                           //某一个时间段完成的写出流长度
 
symmetricalgorithm   des   =   new   descryptoserviceprovider();        
//给key和iv赋值
passwordtobyte(des);
//   创建解密器
icryptotransform   transform   =     des.createdecryptor(des.key,des.iv);
//使用解密器和输出流,创建一个解密输出流
cryptostream   encstream   =   new   cryptostream(fout,   transform,   cryptostreammode.write);
      //读出输入流,解密和写输出流
while(rdlen   <   totlen)
{
len   =   fin.read(bin,   0,   10240);
encstream.write(bin,   0,   len);
rdlen   =   rdlen   +   len;
}
 
encstream.close();    
fout.close();
fin.close();                                      
}


}
}
发表于:2008-01-10 13:05:193楼 得分:0
谢谢   zhonghlning   。加密和压缩很不错。
还有断点续传的么?
发表于:2008-01-11 10:56:044楼 得分:0
我从网上下载了一份   基于webservice的文件传输.pdf   吴晓春写的,很不错。
地址忘了,搜索可以找到,如果谁要的话留下email。
发表于:2008-01-11 11:28:285楼 得分:1
up
changjiangzhibin@gmail.com
发表于:2008-01-13 22:02:196楼 得分:1
很厉害。1楼。收藏。
发表于:2008-01-14 18:25:097楼 得分:1
1楼,很好。
lz请发zzuyongp@163.com   谢谢
发表于:2008-01-18 15:22:458楼 得分:0
从http://www.codeproject.com   网上找到资源,需要注册才能下载/mtomwebservices/article_source.zip

本人改写了,他使用了wse3.0技术,很不错,但是wse需要数字签证,这些是需要花钱的,当然微软提供了免费使用的,可以从微软网上找。
可是我不想那么麻烦,过于绑定微软的技术,所以没有采用wse,仅仅使用了其他相关技术理念实现。
不过学习倒是很好的材料。


快速检索

最新资讯
热门点击