C# 压缩源文件(导出源文件word文件)

说明 1 : 在webUI 公共类,存放 ZipHelper 类

说明  2 :判断文件路径是否存在,不存在则创建文件夹

说明 3  : 引用类方法,判断压缩文件,返回的,是true/false

 

  
引用常用公共压缩方法类
public class CLeopardZip { /// <summary> /// 适用与ZIP压缩 /// </summary> public class ZipHelper { #region 压缩 /// <summary> /// 递归压缩文件夹的内部方法 /// </summary> /// <param name="folderToZip">要压缩的文件夹路径</param> /// <param name="zipStream">压缩输出流</param> /// <param name="parentFolderName">此文件夹的上级文件夹</param> /// <returns></returns> private static bool ZipDirectory(string folderToZip, ZipOutputStream zipStream, string parentFolderName) { bool result = true; string[] folders, files; ZipEntry ent = null; FileStream fs = null; Crc32 crc = new Crc32(); try { ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/")); zipStream.PutNextEntry(ent); zipStream.Flush(); //从复制后的路径,获取的当前文件夹的,所有源文件 files = Directory.GetFiles(folderToZip); foreach (string file in files) { fs = System.IO.File.OpenRead(file); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "//" + Path.GetFileName(file))); ent.DateTime = DateTime.Now; ent.Size = fs.Length; fs.Close(); crc.Reset(); crc.Update(buffer); ent.Crc = crc.Value; zipStream.PutNextEntry(ent); zipStream.Write(buffer, 0, buffer.Length); } } catch { result = false; } finally { if (fs != null) { fs.Close(); fs.Dispose(); } if (ent != null) { ent = null; } GC.Collect(); GC.Collect(1); } folders = Directory.GetDirectories(folderToZip); //此,不需要遍历其他的文件 //遍历doc中的所有文件, //foreach (string folder in folders) // if (!ZipDirectory(folder, zipStream, folderToZip,HtmlFiles)) // return false; return result; } /// <summary> /// 压缩文件夹 /// </summary> /// <param name="folderToZip">要压缩的文件夹路径</param> /// <param name="zipedFile">压缩文件完整路径</param> /// <param name="password">密码</param> /// <returns>是否压缩成功</returns> public static bool ZipDirectory(string folderToZip, string zipedFile, string password,string HtmlFiles) { bool result = false; if (!Directory.Exists(folderToZip)) return result; ZipOutputStream zipStream = new ZipOutputStream(System.IO.File.Create(zipedFile)); zipStream.SetLevel(6); if (!string.IsNullOrEmpty(password)) zipStream.Password = password; result = ZipDirectory(folderToZip, zipStream, ""); zipStream.Finish(); zipStream.Close(); return result; } /// <summary> /// 压缩文件夹 /// </summary> /// <param name="folderToZip">要压缩的文件夹路径</param> /// <param name="zipedFile">压缩文件完整路径</param> /// <returns>是否压缩成功</returns> public static bool ZipDirectory(string folderToZip, string zipedFile,string HtmlFiles) { bool result = ZipDirectory(folderToZip, zipedFile, null,HtmlFiles); return result; } /// <summary> /// 压缩文件 /// </summary> /// <param name="fileToZip">要压缩的文件全名</param> /// <param name="zipedFile">压缩后的文件名</param> /// <param name="password">密码</param> /// <returns>压缩结果</returns> public static bool ZipFile(string fileToZip, string zipedFile, string password) { bool result = true; ZipOutputStream zipStream = null; FileStream fs = null; ZipEntry ent = null; if (!System.IO.File.Exists(fileToZip)) return false; try { fs = System.IO.File.OpenRead(fileToZip); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); fs = System.IO.File.Create(zipedFile); zipStream = new ZipOutputStream(fs); if (!string.IsNullOrEmpty(password)) zipStream.Password = password; ent = new ZipEntry(Path.GetFileName(fileToZip)); zipStream.PutNextEntry(ent); zipStream.SetLevel(6); zipStream.Write(buffer, 0, buffer.Length); } catch { result = false; } finally { if (zipStream != null) { zipStream.Finish(); zipStream.Close(); } if (ent != null) { ent = null; } if (fs != null) { fs.Close(); fs.Dispose(); } } GC.Collect(); GC.Collect(1); return result; } /// <summary> /// 压缩文件 /// </summary> /// <param name="fileToZip">要压缩的文件全名</param> /// <param name="zipedFile">压缩后的文件名</param> /// <returns>压缩结果</returns> public static bool ZipFile(string fileToZip, string zipedFile) { bool result = ZipFile(fileToZip, zipedFile, null); return result; } /// <summary> /// 压缩文件或文件夹 /// </summary> /// <param name="fileToZip">要压缩的路径</param> /// <param name="zipedFile">压缩后的文件名</param> /// <param name="password">密码</param> /// <param name="HtmlFiles">比对文件</param> /// <returns>压缩结果</returns> public static bool Zip(string fileToZip, string zipedFile, string password) { bool result = false; if (Directory.Exists(fileToZip)) result = ZipDirectory(fileToZip, zipedFile, password); else if (System.IO.File.Exists(fileToZip)) result = ZipFile(fileToZip, zipedFile, password); return result; } /// <summary> /// 压缩文件或文件夹 /// </summary> /// <param name="fileToZip">要压缩的路径</param> /// <param name="zipedFile">压缩后的文件名</param> /// <returns>压缩结果</returns> public static bool Zip(string fileToZip, string zipedFile) { bool result = Zip(fileToZip, zipedFile, null); return result; } #endregion } }

 

 


说明 2 判断文件路径是否存在,不存在则创建文件夹

1
2 3 if (!System.IO.Directory.Exists(newPath)) 4 { 5 System.IO.Directory.CreateDirectory(newPath); 6 }

 

 
说明 3  : 引用类方法,判断返回的,是true/false

1
#region FilesZIP 2 public string FilesZIP(List<Files> _list) 3 { 4 string password = ""; 5 var Path = HttpContext.Current.Server.MapPath("~/doc/"); 6 string newPath = Path + Guid.NewGuid().ToString(); 7 if (!System.IO.Directory.Exists(newPath)) 8 { 9 System.IO.Directory.CreateDirectory(newPath); 10 } 11 foreach (Exx.QMS.Model.File.FileModel item in _list) 12 { 13 var _files = Directory.GetFiles(Path); 14 if (_files.Contains(Path + item.FileContent)) 15 { 16 var ext = item.FileContent.Split(.)[1];
//第一个参数:要复制的路径 ; 第二个参数 给复制的路径文件,重新命名 eg:源文件名称:DHFDSJF.docx 命名后的:统计说明.docx
17 System.IO.File.Copy(Path + item.FileContent, newPath + "/" + item.FileName+"."+ ext, true); 18 } 19 } 20 string fileToZip = newPath; 21 var zipedFile = newPath + ".zip"; 22 var fileZip = Common.CLeopardZip.ZipHelper.Zip(fileToZip, zipedFile, password); 23 var IsHtml = fileZip == true ? zipedFile : "Error"; 24 return IsHtml; 25 } 26 #endregion

 

C# 压缩源文件(导出源文件word文件)

上一篇:MySQL中索引注意事项和JDBC的应用


下一篇:window.open关闭后刷新父页面