c# ICSharpCode.SharpZipLib压缩文件时附带空文件夹
2018-06-11 10:09
585 查看
0x00 问题由来
做项目的时候需要导出一个模板,用于填写数据后在导入数据库中,此模板中需要有一个导出一个文件夹来存放资源文件。
但是在使用 ICSharpCode.SharpZipLib这个DLL的时候半天也没找到导出空文件夹的内容,决定自己琢磨写一个。。。
0x00 代码
/// <summary> /// ZIP:压缩文件夹 /// add yuangang by 2016-06-13 /// </summary> /// <param name="DirectoryToZip">需要压缩的文件夹(绝对路径)</param> /// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param> /// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件夹同名)</param> /// <param name="IsEncrypt">是否加密(默认 加密)</param> public static void ZipDirectory(string DirectoryToZip, string ZipedPath, string ZipedFileName = "", bool IsEncrypt = false) { //如果目录不存在,则报错 if (!System.IO.Directory.Exists(DirectoryToZip)) { throw new System.IO.FileNotFoundException("指定的目录: " + DirectoryToZip + " 不存在!"); } //文件名称(默认同源文件名称相同) string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new DirectoryInfo(DirectoryToZip).Name + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip"; using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName)) { using (ZipOutputStream s = new ZipOutputStream(ZipFile)) { if (IsEncrypt) { //压缩文件加密 s.Password = "123"; } ZipSetp(DirectoryToZip, s, ""); } } } /// <summary> /// 递归遍历目录 /// add yuangang by 2016-06-13 /// </summary> private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath) { if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar) { strDirectory += Path.DirectorySeparatorChar; } Crc32 crc = new Crc32(); string[] filenames = Directory.GetFileSystemEntries(strDirectory); if (filenames.Length <= 0) { //如果文件夹下没有文件则压缩文件夹 string fileName = parentPath + strDirectory.Substring(strDirectory.LastIndexOf("\\") + 1); ZipEntry entry = new ZipEntry(fileName + "/"); s.PutNextEntry(entry); } foreach (string file in filenames)// 遍历所有的文件和目录 { if (file.Contains(".zip")) continue; if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件 { string pPath = parentPath; pPath += file.Substring(file.LastIndexOf("\\") + 1); pPath += "\\"; ZipSetp(file, s, pPath); } else // 否则直接压缩文件 { //打开压缩文件 using (FileStream fs = File.OpenRead(file)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1); ZipEntry entry = new ZipEntry(fileName); 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); } } } }
相关文章推荐
- C#/ASP.NET/WinForm 通过ICSharpCode.SharpZipLib.dll实现文件夹、文件的压缩与解压
- C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用
- C# 压缩文件 ICSharpCode.SharpZipLib.dll
- ICSharpCode.SharpZipLib 实现文件压缩功能,可以压缩空文件夹
- C#文件压缩:ICSharpCode.SharpZipLib生成zip、tar、tar.gz
- C# 下利用ICSharpCode.SharpZipLib.dll实现文件/文件夹压缩、解压缩
- 基于(ICSharpCode.SharpZipLib.dll)的文件压缩方法,类文件
- C#+ICSharpCode.SharpZipLib.dll实现压缩解压缩功能
- ICSharpCode.SharpZipLib压缩的文件不能解压的原因
- 使用 ICSharpCode.SharpZipLib.dll 压缩文件 方法实例
- ICSharpCode.SharpZipLib.dll 压缩多文件
- 压缩解压目录和文件--ICSharpCode.SharpZipLib.dll 算法 (转)
- ASP.NET生成压缩文件(rar打包)ICSharpCode.SharpZipLib.dll
- C# 压缩解压缩操作,ICSharpCode.SharpZipLib
- C# 下利用ICSharpCode.SharpZipLib.dll实现文件/文件夹压缩、解压缩
- C# 下利用ICSharpCode.SharpZipLib.dll实现文件/文件夹压缩、解压缩
- zip (ICSharpCode.SharpZipLib.dll文件需要下载)
- ICSharpCode.SharpZipLib.dll压缩的zip包,7zip解压时出错
- C# zip/unzip with ICSharpCode.SharpZipLib
- (转载)C#利用ICSharpCode.SharpZipLib.dll压缩文件和解压文件