asp.net 文件打包成压缩包
2016-03-23 15:56
423 查看
using ICSharpCode.SharpZipLib.Zip; using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.Text = DateTime.Now.ToLocalTime().ToString(); } /// <summary> /// 生成压缩文件 /// </summary> /// <param name="strZipPath">生成的zip文件的路径</param> /// <param name="strZipTopDirectoryPath">源文件的上级目录</param> /// <param name="intZipLevel">T压缩等级</param> /// <param name="strPassword">压缩包解压密码</param> /// <param name="filesOrDirectoriesPaths">源文件路径</param> /// <returns></returns> private bool Zip(string strZipPath, string strZipTopDirectoryPath, int intZipLevel, string strPassword, string[] filesOrDirectoriesPaths) { try { List<string> AllFilesPath = new List<string>(); if (filesOrDirectoriesPaths.Length > 0) // get all files path { for (int i = 0; i < filesOrDirectoriesPaths.Length; i++) { if (File.Exists(filesOrDirectoriesPaths[i])) { AllFilesPath.Add(filesOrDirectoriesPaths[i]); } else if (Directory.Exists(filesOrDirectoriesPaths[i])) { GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath); } } } if (AllFilesPath.Count > 0) { ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(strZipPath)); zipOutputStream.SetLevel(intZipLevel); zipOutputStream.Password = strPassword; for (int i = 0; i < AllFilesPath.Count; i++) { string strFile = AllFilesPath[i].ToString(); try { if (strFile.Substring(strFile.Length) == "") //folder { string strFileName = strFile.Replace(strZipTopDirectoryPath, ""); if (strFileName.StartsWith("")) { strFileName = strFileName.Substring(1); } ZipEntry entry = new ZipEntry(strFileName); entry.DateTime = DateTime.Now; zipOutputStream.PutNextEntry(entry); } else //file { FileStream fs = File.OpenRead(strFile); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string strFileName = strFile.Replace(strZipTopDirectoryPath, ""); if (strFileName.StartsWith("")) { strFileName = strFileName.Substring(0); } ZipEntry entry = new ZipEntry(strFileName); entry.DateTime = DateTime.Now; zipOutputStream.PutNextEntry(entry); zipOutputStream.Write(buffer, 0, buffer.Length); fs.Close(); fs.Dispose(); } } catch { continue; } } zipOutputStream.Finish(); zipOutputStream.Close(); return true; } else { return false; } } catch { return false; } } /// <summary> /// Gets the directory files. /// </summary> /// <param name="strParentDirectoryPath">源文件路径</param> /// <param name="AllFilesPath">所有文件路径</param> private void GetDirectoryFiles(string strParentDirectoryPath, List<string> AllFilesPath) { string[] files = Directory.GetFiles(strParentDirectoryPath); for (int i = 0; i < files.Length; i++) { AllFilesPath.Add(files[i]); } string[] directorys = Directory.GetDirectories(strParentDirectoryPath); for (int i = 0; i < directorys.Length; i++) { GetDirectoryFiles(directorys[i], AllFilesPath); } if (files.Length == 0 && directorys.Length == 0) //empty folder { AllFilesPath.Add(strParentDirectoryPath); } } private void timer1_Tick(object sender, EventArgs e) { this.Text = DateTime.Now.ToLocalTime().ToString(); string date = ConfigurationManager.AppSettings["daochu"]; string date2 = DateTime.Now.ToLongDateString().ToString(); if (DateTime.Parse(this.Text) == DateTime.Parse(date2 + " " + date)) { string wname1 = System.DateTime.Now.ToString("yyyyMMdd"); string lj = @"D:\ebayzip\"; string strZipPath = lj + wname1 + ".zip"; if (!Directory.Exists(lj)) { Directory.CreateDirectory(lj); //cl_initPage.v_DeBugLog("path创建成功:" + path); } else { //cl_initPage.v_DeBugLog("path创建失败:" + path); } string strZipTopDirectoryPath = @"D:\EbayWishProj\"; int intZipLevel = 6; string strPassword = ""; string[] filesOrDirectoriesPaths = new string[] { @"D:\EbayWishProj\" + wname1 }; bool a = Zip(strZipPath, strZipTopDirectoryPath, intZipLevel, strPassword, filesOrDirectoriesPaths); } } } }
相关文章推荐
- 使用MiniProfiler调试ASP.NET MVC网站性能
- ASP.NET MVC的TextBoxFor()和TextBox()
- ASP.NET Boilerplate Castle容器无缝添加日志功能
- ASP.NET MVC系列:添加模型
- asp.net如何定时执行任务
- 问题解决---未能将网站配置为使用ASP.NET4.0
- ASP.NET 使用Session,避免用户F5刷新时重复提交(转)
- Raspkate - 基于.NET的可运行于树莓派的轻量型Web服务器
- Java 8: 从永久代(PermGen)到元空间(Metaspace)
- 禁用树莓派Raspberry Pi休眠 ,更换树莓派软件源, 如何更换树莓派软件源
- ASP.NET-Request对象
- ASP.NET-ActionResutlt
- ASP.NET-MVC中Entity和Model之间的关系
- ASP.NET-viewBag Viewdata Tempdata
- ASP.NET-使用json
- ASP.NET-属性与过滤器
- Spring的AOP自动代理(含AspectJ的AOP开发)
- 无法激活服务,因为它不支持 ASP.NET 兼容性
- 无法激活服务,因为它不支持 ASP.NET 兼容性
- 无法激活服务,因为它不支持 ASP.NET 兼容性