当前位置: 首页 > news >正文

建设商城网站公司/广州企业网站推广

建设商城网站公司,广州企业网站推广,公司网站如何做维护,做汽车网站怎么挣钱1、将服务器文件打包下载至本地(压缩包缓存到服务器下载) (1)文件批量压缩成压缩包并缓存到服务器(公共方法) using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; using Syst…

1、将服务器文件打包下载至本地(压缩包缓存到服务器下载)

(1)文件批量压缩成压缩包并缓存到服务器(公共方法)

using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;/// <summary>
/// 压缩文件(并保存至服务器)
/// </summary>
/// <param name="fileName">要压缩的所有文件(完全路径)</param>
/// <param name="name">压缩后文件路径</param>
/// <param name="Level">压缩级别</param>
public static bool ZipFileMain(List<string> filenames, string name, int Level){ZipOutputStream s = new ZipOutputStream(File.Create(name));Crc32 crc = new Crc32();//压缩级别s.SetLevel(Level); try{foreach (string file in filenames){//打开压缩文件FileStream fs = File.OpenRead(file);byte[] buffer = new byte[fs.Length];fs.Read(buffer, 0, buffer.Length);//建立压缩实体ZipEntry entry = new ZipEntry(System.IO.Path.GetFileName(file));//时间entry.DateTime = DateTime.Now;//空间大小entry.Size = fs.Length;fs.Close();crc.Reset();crc.Update(buffer);entry.Crc = crc.Value;s.PutNextEntry(entry);s.Write(buffer, 0, buffer.Length);}return true;}catch (Exception ex){return false;}finally{s.Finish();s.Close();}return true;
}

(2)实现代码

调用公共方法将文件批量压缩打包成压缩包并下载到服务器,从服务器读取压缩包下载至本地。

List<string> paths=new List<string>(){"1.txt","2.txt"
};
string strSavePath=Server.MapPath("/");
foreach(var path in paths){path=strSavePath+path;
}
string rarName="test.rar";
//调用公共方法
ZipFileMain(paths, strSavePath + rarName, 1);//下载压缩包至本地
string strServerRarPath = strSavePath + rarName;
//文件不存在重新创建
if (!File.Exists(strServerRarPath)){File.Create(strServerRarPath);
}
byte[] bytes = File.ReadAllBytes(strServerRarPath);
//通知浏览器下载文件而不是打开
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment;filename="+HttpUtility.UrlEncode(strRarName, System.Text.Encoding.UTF8));
context.Response.BinaryWrite(bytes);
context.Response.Flush();

2、将服务器文件打包下载至本地(压缩包缓存到内存下载)

(1)将文件流压缩返回压缩后的流(公共方法)

/// <summary>
/// 将多个流进行zip压缩,返回压缩后的流.
/// </summary>
/// <param name="streams">key:文件名;value:文件名对应的要压缩的流.</param>
/// <returns>压缩后的流.</returns>
static MemoryStream PackageManyZip(Dictionary<string, Stream> streams){byte[] buffer = new byte[6500];MemoryStream returnStream = new MemoryStream();var zipMs = new MemoryStream();using (ZipOutputStream zipStream = new ZipOutputStream(zipMs)){zipStream.SetLevel(9);foreach (var kv in streams){string fileName = kv.Key;using (var streamInput = kv.Value){zipStream.PutNextEntry(new ZipEntry(fileName));while (true){var readCount = streamInput.Read(buffer, 0, buffer.Length);if (readCount > 0) zipStream.Write(buffer, 0, readCount);else break;}zipStream.Flush();}}zipStream.Finish();zipMs.Position = 0;zipMs.CopyTo(returnStream, 5600);}returnStream.Position = 0;return returnStream;
}

(2)实现代码

将服务器的批量文件以流的形式压缩成一个压缩包流,再将压缩包流下载至本地。

List<string> paths=new List<string>(){"1.txt","2.txt"
};
string strSavePath=Server.MapPath("/");
Dictionary<string, Stream> streamDic = new Dictionary<string, Stream>();
//文件转化成流
foreach(var path in paths){string FileName = path;path=strSavePath+path;//文件不存在if (!File.Exists(path)) continue;//处理文件数据Stream streamWriter = File.Open(path, FileMode.Open);if(streamDic.ContainsKey(FileName)==false){//文件不重名streamDic.Add(FileName, streamWriter);}
}
//压缩
MemoryStream ms = new MemoryStream();
ms = PackageManyZip(streamDic);
byte[] bytes = new byte[(int)ms.Length];
ms.Read(bytes, 0, bytes.Length);
ms.Close();
//通知浏览器下载文件而不是打开
string zipName = DateTime.Today.ToString("yyyy-MM-dd") + ".zip";
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(zipName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();

注:streamDic添加数据时,需要先判断key是否存在,如果key在streamDic中已经存在,在Add相同的key会出现异常导致程序终止运行。

http://www.jmfq.cn/news/4849705.html

相关文章:

  • 网站建设开发用什么软件/婚恋网站排名
  • 张艺兴粉丝做的网站/怎么样推广自己的网站
  • 做网站对比报告/广告发布
  • 沈阳唐朝网站建设/厦门seo顾问
  • 北京软件开发学校哪个好/seo伪原创工具
  • 企业logo设计说明/宁波seo推广方式排名
  • 委托建设网站账务处理/免费域名申请个人网站
  • p2p网站建设 上海/电商网站建设定制
  • 做网站的要求/促销活动推广方法有哪些
  • 建设 互动 网站 模式/北京培训学校
  • 网站日志类型/产品网络营销分析
  • 蒲公英网站建设/郑州seo培训
  • 安阳专业做网站公司/百度移动端排名
  • 湖南营销型网站建设磐石网络/网络视频营销
  • 做外发的网站/微博指数查询入口
  • 自己建网站 wordpress/湘潭网站定制
  • 宁波网站建设设计制作方案与价格/写文章在哪里发表挣钱
  • 国内最好的网站建设/理发培训专业学校
  • 钟表东莞网站建设/上海优化seo排名
  • 政府门户网站如何做预算/搜索引擎优化的具体操作
  • 什么网站做电脑系统好/百度联盟广告点击一次收益
  • 呼和浩特网站建设SEO优化/seo关键词优化系统
  • 上街三屏网站建设/优化的意思
  • 动态网站的格式/seo智能优化公司
  • 网站域名怎么写/seo教程下载
  • 宝塔做的网站能不能访问/如何自己做网站
  • 做企业网站公司/百度投诉电话
  • 肃州区住房和城乡建设局网站/产品网络营销
  • 好的做网站的公司/颜色广告
  • wordpress图片主题免费/seo外链优化