首页 > 代码库 > C# 下利用ICSharpCode.SharpZipLib.dll实现文件/文件夹压缩、解压缩
C# 下利用ICSharpCode.SharpZipLib.dll实现文件/文件夹压缩、解压缩
ICSharpCode.SharpZipLib.dll下载地址
1、压缩某个指定目录下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志。
#region 压缩logs目录下日志 public static void CompresslogDic() { try { string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "logs"; DirectoryInfo logsDic = new DirectoryInfo(logFilePath); FileInfo[] bipLog = logsDic.GetFiles(); DateTime dt = DateTime.Now; List<FileInfo> logsInOneDay = new List<FileInfo>(); for (int i = 0; i < bipLog.Length; i++) { if (bipLog[i].Name.Substring(bipLog[i].Name.Length - 3) != "zip") { logsInOneDay.Add(bipLog[i]); } } if (logsInOneDay.Count > 0) { try { if (!Directory.Exists(logsDic.FullName + "\\CompressionDirectory")) { Directory.CreateDirectory(logsDic.FullName + "\\CompressionDirectory"); } string compressFileName = dt.ToString("yyyy-MM-dd"); if (File.Exists(logsDic.FullName + "\\CompressionDirectory\\" + dt.ToString("yyyy-MM-dd") + ".zip")) { Guid guid = Guid.NewGuid(); compressFileName = compressFileName + "-" + guid.ToString(); } compressFileName += ".zip"; Compress(logsInOneDay, logsDic.FullName + "\\CompressionDirectory\\" + compressFileName, 9, 100); foreach (FileInfo fileInfo in logsInOneDay) { try { fileInfo.Delete(); } catch (Exception e) { //错误信息记录处理 } } } catch (Exception e) { //错误信息记录处理 } } } catch (Exception e) { //错误信息记录处理 } } #endregion2、压缩指定目录子目录下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志。
#region 压缩logs子目录下日志 public static void CompresslogsDic() { try { string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "logs"; DirectoryInfo logsDic = new DirectoryInfo(logFilePath); FileInfo[] bipLog = logsDic.GetFiles(); DateTime dt = DateTime.Now; DirectoryInfo[] subLosgDic = logsDic.GetDirectories(); foreach (DirectoryInfo bankDic in subLosgDic) { dt = DateTime.Now; bipLog = bankDic.GetFiles(); List<FileInfo> logsInOneDay = new List<FileInfo>(); for (int i = 0; i < bipLog.Length; i++) { if (bipLog[i].Name.Substring(bipLog[i].Name.Length - 3) != "zip") { logsInOneDay.Add(bipLog[i]); } } if (logsInOneDay.Count > 0) { try { if (!Directory.Exists(bankDic.FullName + "\\CompressionDirectory")) { Directory.CreateDirectory(bankDic.FullName + "\\CompressionDirectory"); } string compressFileName = dt.ToString("yyyy-MM-dd"); if (File.Exists(bankDic.FullName + "\\CompressionDirectory\\" + dt.ToString("yyyy-MM-dd") + ".zip")) { Guid guid = Guid.NewGuid(); compressFileName = compressFileName + "-" + guid.ToString(); } compressFileName += ".zip"; Compress(logsInOneDay, bankDic.FullName + "\\CompressionDirectory\\" + compressFileName, 9, 100); foreach (FileInfo fileInfo in logsInOneDay) { try { fileInfo.Delete(); } catch (Exception e) { //错误信息记录处理 } } } catch (Exception e) { //错误信息记录处理 } } } } catch (Exception e) { //错误信息记录处理 } } #endregion3、压缩文件
#region 压缩文件 /// <summary> /// 压缩文件 /// </summary> /// <param name="fileNames">要打包的文件列表</param> /// <param name="GzipFileName">目标文件名</param> /// <param name="CompressionLevel">压缩品质级别(0~9)</param> /// <param name="SleepTimer">休眠时间(单位毫秒)</param> public static void Compress(List<FileInfo> fileNames, string GzipFileName, int CompressionLevel, int SleepTimer) { ZipOutputStream s = new ZipOutputStream(File.Create(GzipFileName)); try { s.SetLevel(CompressionLevel); //0 - store only to 9 - means best compression foreach (FileInfo file in fileNames) { FileStream fs = null; try { fs = file.Open(FileMode.Open, FileAccess.ReadWrite); } catch { continue; } // 将文件分批读入缓冲区 byte[] data = http://www.mamicode.com/new byte[2048];>4、解压缩#region 解压缩文件 /// <summary> /// 解压缩文件 /// </summary> /// <param name="GzipFile">压缩包文件名</param> /// <param name="targetPath">解压缩目标路径</param> public static void Decompress(string GzipFile, string targetPath) { //string directoryName = Path.GetDirectoryName(targetPath + "//") + "//"; string directoryName = targetPath; if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);//生成解压目录 string CurrentDirectory = directoryName; byte[] data = http://www.mamicode.com/new byte[2048];>5、压缩文件夹#region 压缩文件夹 /// <summary> /// 压缩文件夹 /// </summary> /// <param name="dirPath">要打包的文件夹</param> /// <param name="GzipFileName">目标文件名</param> /// <param name="CompressionLevel">压缩品质级别(0~9)</param> /// <param name="deleteDir">是否删除原文件夹</param> public static void CompressDirectory(string dirPath, string GzipFileName, int CompressionLevel, bool deleteDir) { //压缩文件为空时默认与压缩文件夹同一级目录 if (GzipFileName == string.Empty) { GzipFileName = dirPath.Substring(dirPath.LastIndexOf("//") + 1); GzipFileName = dirPath.Substring(0, dirPath.LastIndexOf("//")) + "//" + GzipFileName + ".zip"; } //if (Path.GetExtension(GzipFileName) != ".zip") //{ // GzipFileName = GzipFileName + ".zip"; //} using (ZipOutputStream zipoutputstream = new ZipOutputStream(File.Create(GzipFileName))) { zipoutputstream.SetLevel(CompressionLevel); ICSharpCode.SharpZipLib.Checksums.Crc32 crc = new ICSharpCode.SharpZipLib.Checksums.Crc32(); Dictionary<string, DateTime> fileList = GetAllFies(dirPath); foreach (KeyValuePair<string, DateTime> item in fileList) { FileStream fs = File.OpenRead(item.Key.ToString()); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); ZipEntry entry = new ZipEntry(item.Key.Substring(dirPath.Length)); entry.DateTime = item.Value; entry.Size = fs.Length; fs.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; zipoutputstream.PutNextEntry(entry); zipoutputstream.Write(buffer, 0, buffer.Length); } } if (deleteDir) { Directory.Delete(dirPath, true); } } #endregion6、获取所有文件#region 获取所有文件 /// <summary> /// 获取所有文件 /// </summary> /// <returns></returns> private static Dictionary<string, DateTime> GetAllFies(string dir) { Dictionary<string, DateTime> FilesList = new Dictionary<string, DateTime>(); DirectoryInfo fileDire = new DirectoryInfo(dir); if (!fileDire.Exists) { throw new System.IO.FileNotFoundException("目录:" + fileDire.FullName + "没有找到!"); } GetAllDirFiles(fileDire, FilesList); GetAllDirsFiles(fileDire.GetDirectories(), FilesList); return FilesList; } #endregion7、获取一个文件夹下的所有文件夹里的文件#region 获取一个文件夹下的所有文件夹里的文件 /// <summary> /// 获取一个文件夹下的所有文件夹里的文件 /// </summary> /// <param name="dirs"></param> /// <param name="filesList"></param> private static void GetAllDirsFiles(DirectoryInfo[] dirs, Dictionary<string, DateTime> filesList) { foreach (DirectoryInfo dir in dirs) { foreach (FileInfo file in dir.GetFiles("*.*")) { filesList.Add(file.FullName, file.LastWriteTime); } GetAllDirsFiles(dir.GetDirectories(), filesList); } } #endregion8、获取一个文件夹下的文件#region 获取一个文件夹下的文件 /// <summary> /// 获取一个文件夹下的文件 /// </summary> /// <param name="dir">目录名称</param> /// <param name="filesList">文件列表HastTable</param> private static void GetAllDirFiles(DirectoryInfo dir, Dictionary<string, DateTime> filesList) { foreach (FileInfo file in dir.GetFiles("*.*")) { filesList.Add(file.FullName, file.LastWriteTime); } } #endregion
C# 下利用ICSharpCode.SharpZipLib.dll实现文件/文件夹压缩、解压缩
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。