首页 > 学院 > 开发设计 > 正文

使用C#语言进行多个文件的压缩打包

2019-11-08 19:41:54
字体:
来源:转载
供稿:网友

这里借助了第三方程序集ICSharpCode,请自行检索下载。

下面上码

/// <summary>/// 制作压缩包(多个文件压缩到一个压缩包,支持加密、注释)/// </summary>/// <param name="fileNames">要压缩的文件</param>/// <param name="topDirectoryName">压缩文件目录</param>/// <param name="zipedFileName">压缩包文件名</param>/// <param name="comPResssionLevel">压缩级别 1-9</param>/// <param name="passWord">密码</param>/// <param name="comment">注释</param>public static void ZipFiles(string[] fileNames, string topDirectoryName, string zipedFileName, int? compresssionLevel, string password="", string comment=""){ using (ZipOutputStream zos = new ZipOutputStream(File.Open(zipedFileName, FileMode.OpenOrCreate))) { if (compresssionLevel.HasValue) { zos.SetLevel(compresssionLevel.Value);//设置压缩级别 } if (!string.IsNullOrEmpty(password)) { zos.Password = password;//设置zip包加密密码 } if (!string.IsNullOrEmpty(comment)) { zos.SetComment(comment);//设置zip包的注释 } foreach (string file in fileNames) { string fileName = string.Format("{0}/{1}", topDirectoryName, file); if (File.Exists(fileName)) { FileInfo item = new FileInfo(fileName); FileStream fs = File.OpenRead(item.FullName); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); ZipEntry entry = new ZipEntry(item.Name); zos.PutNextEntry(entry); zos.Write(buffer, 0, buffer.Length); } } }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表