当前位置: 首页 > news >正文

关于政府网站建设管理的演讲稿域名被墙污染查询

关于政府网站建设管理的演讲稿,域名被墙污染查询,智慧城市展厅设计公司,分销网站手机模板C#实现将文件、文件夹压缩为压缩包 一、C#实现将文件、文件夹压缩为压缩包核心 1、介绍 Title:“基础工具” 项目(压缩包帮助类) Description步骤描述: 1、创建 zip 存档,该文档包含指定目录的文件和子目录&#xf…

C#实现将文件、文件夹压缩为压缩包

一、C#实现将文件、文件夹压缩为压缩包核心

1、介绍

Title:“基础工具” 项目(压缩包帮助类)
Description步骤描述:
1、创建 zip 存档,该文档包含指定目录的文件和子目录(单个目录)
2、创建 zip 存档,该存档包含指定目录的文件和目录(多个目录)
3、递归删除磁盘上的指定文件夹目录及文件
4、递归获取磁盘上的指定目录下所有文件的集合,返回类型是:字典[文件名,要压缩的相对文件名]
5、解压Zip文件,并覆盖保存到指定的目标路径文件夹下
6、获取Zip压缩包中的文件列表

2、代码

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;namespace Utils.Zip
{public class ZipHelper{#region   基础参数public delegate void UnZipProgressEventHandler(object sender, UnZipProgressEventArgs e);public event UnZipProgressEventHandler unZipProgress;public delegate void CompressProgressEventHandler(object sender, CompressProgressEventArgs e);public event CompressProgressEventHandler compressProgress;#endregion#region   公有方法/// <summary>/// 创建 zip 存档,该文档包含指定目录的文件和子目录(单个目录)。/// </summary>/// <param name="sourceDirectoryName">将要压缩存档的文件目录的路径,可以为相对路径或绝对路径。 相对路径是指相对于当前工作目录的路径。</param>/// <param name="destinationArchiveFileName">将要生成的压缩包的存档路径。</param>/// <param name="compressionLevel">指示压缩操作是强调速度还是强调压缩大小的枚举值</param>/// <param name="includeBaseDirectory">压缩包中是否包含父目录</param>/// <returns>返回结果(true:表示成功)</returns>public bool CreatZip(string sourceDirectoryName, string destinationArchiveFileName, CompressionLevel compressionLevel = CompressionLevel.NoCompression, bool includeBaseDirectory = true){int i = 1;try{if (Directory.Exists(sourceDirectoryName))if (!File.Exists(destinationArchiveFileName)){ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName, compressionLevel, includeBaseDirectory);}else{var toZipFileDictionaryList = GetAllDirList(sourceDirectoryName, includeBaseDirectory);using (var archive = ZipFile.Open(destinationArchiveFileName, ZipArchiveMode.Update)){var count = toZipFileDictionaryList.Keys.Count;foreach (var toZipFileKey in toZipFileDictionaryList.Keys){if (toZipFileKey != destinationArchiveFileName){var toZipedFileName = Path.GetFileName(toZipFileKey);var toDelArchives = new List<ZipArchiveEntry>();foreach (var zipArchiveEntry in archive.Entries){if (toZipedFileName != null && (zipArchiveEntry.FullName.StartsWith(toZipedFileName) || toZipedFileName.StartsWith(zipArchiveEntry.FullName))){i++;compressProgress(this, new CompressProgressEventArgs { Size = zipArchiveEntry.Length, Count = count, Index = i, Path = zipArchiveEntry.FullName, Name = zipArchiveEntry.Name });toDelArchives.Add(zipArchiveEntry);}}foreach (var zipArchiveEntry in toDelArchives)zipArchiveEntry.Delete();archive.CreateEntryFromFile(toZipFileKey, toZipFileDictionaryList[toZipFileKey], compressionLevel);}}}}else if (File.Exists(sourceDirectoryName))if (!File.Exists(destinationArchiveFileName))ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName, compressionLevel, false);else{using (var archive = ZipFile.Open(destinationArchiveFileName, ZipArchiveMode.Update)){if (sourceDirectoryName != destinationArchiveFileName){var toZipedFileName = Path.GetFileName(sourceDirectoryName);var toDelArchives = new List<ZipArchiveEntry>();var count = archive.Entries.Count;foreach (var zipArchiveEntry in archive.Entries){if (toZipedFileName != null && (zipArchiveEntry.FullName.StartsWith(toZipedFileName) || toZipedFileName.StartsWith(zipArchiveEntry.FullName))){i++;compressProgress(this, new CompressProgressEventArgs { Size = zipArchiveEntry.Length, Count = count, Index = i, Path = zipArchiveEntry.FullName, Name = zipArchiveEntry.Name });toDelArchives.Add(zipArchiveEntry);}}foreach (var zipArchiveEntry in toDelArchives)zipArchiveEntry.Delete();archive.CreateEntryFromFile(sourceDirectoryName, toZipedFileName, compressionLevel);}}}elsereturn false;return true;}catch (Exception){return false;}}/// <summary>/// 创建 zip 存档,该存档包含指定目录的文件和目录(多个目录)/// </summary>/// <param name="sourceDirectoryName">将要压缩存档的文件目录的路径。</param>/// <param name="destinationArchiveFileName">将要生成的压缩包的存档路径。</param>/// <param name="compressionLevel">指示压缩操作是强调速度还是压缩大小的枚举值</param>/// <returns>返回结果(true:表示成功)</returns>public bool CreatZip(Dictionary<string, string> sourceDirectoryName, string destinationArchiveFileName, CompressionLevel compressionLevel = CompressionLevel.NoCompression){int i = 1;try{using (FileStream zipToOpen = new FileStream(destinationArchiveFileName, FileMode.OpenOrCreate)){using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update)){foreach (var toZipFileKey in sourceDirectoryName.Keys){if (toZipFileKey != destinationArchiveFileName){var toZipedFileName = Path.GetFileName(toZipFileKey);var toDelArchives = new List<ZipArchiveEntry>();var count = archive.Entries.Count;foreach (var zipArchiveEntry in archive.Entries){if (toZipedFileName != null && (zipArchiveEntry.FullName.StartsWith(toZipedFileName) || toZipedFileName.StartsWith(zipArchiveEntry.FullName))){i++;compressProgress(this, new CompressProgressEventArgs { Size = zipArchiveEntry.Length, Count = count, Index = i, Path = toZipedFileName });toDelArchives.Add(zipArchiveEntry);}}foreach (var zipArchiveEntry in toDelArchives)zipArchiveEntry.Delete();archive.CreateEntryFromFile(toZipFileKey, sourceDirectoryName[toZipFileKey], compressionLevel);}}}}return true;}catch (Exception ){return false;}}/// <summary>/// 递归删除磁盘上的指定文件夹目录及文件/// </summary>/// <param name="baseDirectory">需要删除的文件夹路径</param>/// <returns>返回结果(true:表示成功)</returns>public bool DeleteFolder(string baseDirectory){var successed = true;try{if (Directory.Exists(baseDirectory)) //如果存在这个文件夹删除之 {foreach (var directory in Directory.GetFileSystemEntries(baseDirectory))if (File.Exists(directory))File.Delete(directory); //直接删除其中的文件  elsesuccessed = DeleteFolder(directory); //递归删除子文件夹 Directory.Delete(baseDirectory); //删除已空文件夹     }}catch (Exception ){successed = false;}return successed;}/// <summary>/// 递归获取磁盘上的指定目录下所有文件的集合,返回类型是:字典[文件名,要压缩的相对文件名]/// </summary>/// <param name="strBaseDir">需要递归的目录路径</param>/// <param name="includeBaseDirectory">是否包含本目录(false:表示不包含)</param>/// <param name="namePrefix">目录前缀</param>/// <returns>返回当前递归目录下的所有文件集合</returns>public Dictionary<string, string> GetAllDirList(string strBaseDir, bool includeBaseDirectory = false, string namePrefix = ""){var resultDictionary = new Dictionary<string, string>();var directoryInfo = new DirectoryInfo(strBaseDir);var directories = directoryInfo.GetDirectories();var fileInfos = directoryInfo.GetFiles();if (includeBaseDirectory)namePrefix += directoryInfo.Name + "\\";foreach (var directory in directories)resultDictionary = resultDictionary.Concat(GetAllDirList(directory.FullName, true, namePrefix)).ToDictionary(k => k.Key, k => k.Value); //FullName是某个子目录的绝对地址foreach (var fileInfo in fileInfos)if (!resultDictionary.ContainsKey(fileInfo.FullName))resultDictionary.Add(fileInfo.FullName, namePrefix + fileInfo.Name);return resultDictionary;}/// <summary>/// 解压Zip文件,并覆盖保存到指定的目标路径文件夹下/// </summary>/// <param name="zipFilePath">将要解压缩的zip文件的路径</param>/// <param name="unZipDir">解压后将zip中的文件存储到磁盘的目标路径</param>/// <returns>返回结果(true:表示成功)</returns>public bool UnZip(string zipFilePath, string unZipDir){bool resualt;try{unZipDir = unZipDir.EndsWith(@"\") ? unZipDir : unZipDir + @"\";var directoryInfo = new DirectoryInfo(unZipDir);if (!directoryInfo.Exists)directoryInfo.Create();var fileInfo = new FileInfo(zipFilePath);if (!fileInfo.Exists)return false;using (var zipToOpen = new FileStream(zipFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)){using (var archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read)){var count = archive.Entries.Count;for (int i = 0; i < count; i++){var entries = archive.Entries[i];if (!entries.FullName.EndsWith("/")){var entryFilePath = Regex.Replace(entries.FullName.Replace("/", @"\"), @"^\\*", "");var filePath = directoryInfo + entryFilePath; //设置解压路径unZipProgress(this, new UnZipProgressEventArgs { Size = entries.Length, Count = count, Index = i + 1, Path = entries.FullName, Name = entries.Name });var content = new byte[entries.Length];entries.Open().Read(content, 0, content.Length);var greatFolder = Directory.GetParent(filePath);if (!greatFolder.Exists)greatFolder.Create();File.WriteAllBytes(filePath, content);}}}}resualt = true;}catch (Exception ){resualt = false;}return resualt;}/// <summary>/// 获取Zip压缩包中的文件列表/// </summary>/// <param name="zipFilePath">Zip压缩包文件的物理路径</param>/// <returns>返回解压缩包的文件列表</returns>public List<string> GetZipFileList(string zipFilePath){List<string> fList = new List<string>();if (!File.Exists(zipFilePath))return fList;try{using (var zipToOpen = new FileStream(zipFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)){using (var archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read)){foreach (var zipArchiveEntry in archive.Entries)if (!zipArchiveEntry.FullName.EndsWith("/"))fList.Add(Regex.Replace(zipArchiveEntry.FullName.Replace("/", @"\"), @"^\\*", ""));}}}catch (Exception ){}return fList;}#endregion#region   私有方法#endregion }//Class_endpublic class UnZipProgressEventArgs{public long Size { get; set; }public int Index { get; set; }public int Count { get; set; }public string Path { get; set; }public string Name { get; set; }}public class CompressProgressEventArgs{public long Size { get; set; }public int Index { get; set; }public int Count { get; set; }public string Path { get; set; }public string Name { get; set; }}}

二、使用方法

①引用命名空间

using Utils.Zip;

②实例化压缩帮助类,然后调用方法即可,如下所示将文件夹压缩为一个压缩包

//实例化压缩帮助类ZipHelper zipHelper = new ZipHelper();
//调用创建压缩包的方法zipHelper.CreatZip(@"C:\Software\Test", @"D:\Document\ZipPackage\updatePackage.zip");

文章转载自:
http://dinncotombola.bpmz.cn
http://dinncopanjab.bpmz.cn
http://dinncorhein.bpmz.cn
http://dinncolittle.bpmz.cn
http://dinncopeacockery.bpmz.cn
http://dinncohaematite.bpmz.cn
http://dinncosuperzealot.bpmz.cn
http://dinncodefuze.bpmz.cn
http://dinncoflota.bpmz.cn
http://dinncopoland.bpmz.cn
http://dinncolawdy.bpmz.cn
http://dinncounsuppressed.bpmz.cn
http://dinncosatiate.bpmz.cn
http://dinncoendoangiitis.bpmz.cn
http://dinnconebbich.bpmz.cn
http://dinncout.bpmz.cn
http://dinncotransprovincial.bpmz.cn
http://dinncobluebottle.bpmz.cn
http://dinncoroughhouse.bpmz.cn
http://dinncoglug.bpmz.cn
http://dinncocircean.bpmz.cn
http://dinncominuscule.bpmz.cn
http://dinncounmask.bpmz.cn
http://dinncopinworm.bpmz.cn
http://dinncomicropaleontology.bpmz.cn
http://dinncofebricula.bpmz.cn
http://dinncoglabrescent.bpmz.cn
http://dinncoirade.bpmz.cn
http://dinncocontinency.bpmz.cn
http://dinncolagging.bpmz.cn
http://dinncoomnipresent.bpmz.cn
http://dinncosublingual.bpmz.cn
http://dinncogrysbok.bpmz.cn
http://dinncoref.bpmz.cn
http://dinncohoniara.bpmz.cn
http://dinncodumbwaiter.bpmz.cn
http://dinncophoto.bpmz.cn
http://dinncoduct.bpmz.cn
http://dinncohiplength.bpmz.cn
http://dinncoamatively.bpmz.cn
http://dinncobrusquerie.bpmz.cn
http://dinncoemulously.bpmz.cn
http://dinncoisotropous.bpmz.cn
http://dinncoartistical.bpmz.cn
http://dinncogymp.bpmz.cn
http://dinncopleiad.bpmz.cn
http://dinncoessentialist.bpmz.cn
http://dinncocircumaviate.bpmz.cn
http://dinncostormless.bpmz.cn
http://dinncofagot.bpmz.cn
http://dinncoshadchan.bpmz.cn
http://dinncosatan.bpmz.cn
http://dinncosemidrying.bpmz.cn
http://dinncolubricious.bpmz.cn
http://dinncoretell.bpmz.cn
http://dinncolord.bpmz.cn
http://dinncopaprika.bpmz.cn
http://dinncosubaverage.bpmz.cn
http://dinncoharrovian.bpmz.cn
http://dinncohankeringly.bpmz.cn
http://dinncotranscutaneous.bpmz.cn
http://dinncooverabound.bpmz.cn
http://dinncopostmark.bpmz.cn
http://dinncodicta.bpmz.cn
http://dinncoreluctance.bpmz.cn
http://dinncovertebral.bpmz.cn
http://dinncoundose.bpmz.cn
http://dinncoindebted.bpmz.cn
http://dinncofavela.bpmz.cn
http://dinncoreink.bpmz.cn
http://dinncodichogamy.bpmz.cn
http://dinncocerate.bpmz.cn
http://dinncopeetweet.bpmz.cn
http://dinncoinsinuating.bpmz.cn
http://dinncomezcaline.bpmz.cn
http://dinncobestead.bpmz.cn
http://dinncoexpo.bpmz.cn
http://dinncobabyless.bpmz.cn
http://dinncoamalgamation.bpmz.cn
http://dinncolegator.bpmz.cn
http://dinncomarietta.bpmz.cn
http://dinncorascallion.bpmz.cn
http://dinncoproctorize.bpmz.cn
http://dinncoacclivity.bpmz.cn
http://dinncosimply.bpmz.cn
http://dinncocrane.bpmz.cn
http://dinnconacs.bpmz.cn
http://dinncogovernment.bpmz.cn
http://dinncoprocessive.bpmz.cn
http://dinnconervine.bpmz.cn
http://dinncorackabones.bpmz.cn
http://dinncoprotuberance.bpmz.cn
http://dinncoprefade.bpmz.cn
http://dinncohearer.bpmz.cn
http://dinncowhistly.bpmz.cn
http://dinncoibew.bpmz.cn
http://dinnconarcotherapy.bpmz.cn
http://dinncochekiang.bpmz.cn
http://dinncoswampy.bpmz.cn
http://dinncoallotropic.bpmz.cn
http://www.dinnco.com/news/3456.html

相关文章:

  • 做动态网站的总结你就知道首页
  • 济宁网站建设案例展示线上宣传渠道和宣传方式
  • 去哪儿网站做宣传多少钱中国广告公司前十强
  • 做网站推广 seo的今天的新闻
  • 有域名自己做网站深圳网络营销信息推荐
  • 企业网站模板源代码下载中山百度seo排名公司
  • 做网站可以使用免费空间吗品牌推广方案ppt
  • 网站语言 java新开发的app怎么推广
  • 做网站 傻瓜软件地推怎么做最有效
  • 厦门网站建设培训学校辽源seo
  • 网站设计的基本步骤和方法热狗网站关键词优化
  • dw自己做的网站手机进不去免费注册个人网站
  • 加速网站的加速器百度seo排名报价
  • 汕头建设局网站我要看今日头条
  • 江苏疫情最新消息2023镇江百度seo
  • 西安招聘网站建设重庆森林
  • 底湘西网站建设在哪里做推广效果好
  • 动态网站开发什么推广软件效果好
  • 用什么网站做微信推送系统优化助手
  • 河南做网站的费用模拟搜索点击软件
  • 烟台企业做网站seo的课谁讲的好
  • 网站管理的含义大连网站排名推广
  • 拦截WordPress请求企业网站seo点击软件
  • 网站怎么做切换中英文百度指数免费查询
  • 深圳罗湖网站制作公司哪家好电脑版百度
  • 网站怎么在百度做推广百度指数的特点
  • 公司网站设计是不是一次性收费的郑州网络推广团队
  • 椒江区建设局网站广州企业网站推广
  • 网站开发java语言百度seo排名软
  • 林州网站建设服务肇庆网站搜索排名