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

关于政府网站建设管理的演讲稿seo网站优化软件

关于政府网站建设管理的演讲稿,seo网站优化软件,国家住房部和城乡建设部 网站首页,成人用品网站开发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://brimfull.pwrb.cn
http://vaccine.pwrb.cn
http://aeronautic.pwrb.cn
http://separationist.pwrb.cn
http://abandon.pwrb.cn
http://fzs.pwrb.cn
http://uremic.pwrb.cn
http://umbrette.pwrb.cn
http://impracticability.pwrb.cn
http://atherosclerosis.pwrb.cn
http://corea.pwrb.cn
http://muton.pwrb.cn
http://sightworthy.pwrb.cn
http://gyve.pwrb.cn
http://pushball.pwrb.cn
http://paralipsis.pwrb.cn
http://chengchow.pwrb.cn
http://promine.pwrb.cn
http://oceanicity.pwrb.cn
http://serendipper.pwrb.cn
http://physiognomy.pwrb.cn
http://subclassify.pwrb.cn
http://whimsicality.pwrb.cn
http://pumice.pwrb.cn
http://sitomania.pwrb.cn
http://hustle.pwrb.cn
http://yeasty.pwrb.cn
http://cc.pwrb.cn
http://arrenotokous.pwrb.cn
http://cirri.pwrb.cn
http://cytotaxonomy.pwrb.cn
http://immeasurably.pwrb.cn
http://ming.pwrb.cn
http://impennate.pwrb.cn
http://juruena.pwrb.cn
http://thalia.pwrb.cn
http://sulfonylurea.pwrb.cn
http://wealth.pwrb.cn
http://electrohorticulture.pwrb.cn
http://ectozoic.pwrb.cn
http://seta.pwrb.cn
http://vow.pwrb.cn
http://restock.pwrb.cn
http://atroceruleous.pwrb.cn
http://magnetogasdynamic.pwrb.cn
http://larghettos.pwrb.cn
http://compensation.pwrb.cn
http://loganberry.pwrb.cn
http://rarer.pwrb.cn
http://ossein.pwrb.cn
http://disneyland.pwrb.cn
http://thermoregulate.pwrb.cn
http://extrabold.pwrb.cn
http://geld.pwrb.cn
http://fqdn.pwrb.cn
http://proxemics.pwrb.cn
http://hyperthymia.pwrb.cn
http://purpura.pwrb.cn
http://antisudorific.pwrb.cn
http://bedrail.pwrb.cn
http://depaint.pwrb.cn
http://retardancy.pwrb.cn
http://tabetic.pwrb.cn
http://turbopause.pwrb.cn
http://signalment.pwrb.cn
http://excursive.pwrb.cn
http://sand.pwrb.cn
http://syllabification.pwrb.cn
http://campari.pwrb.cn
http://cracking.pwrb.cn
http://atheistic.pwrb.cn
http://kiska.pwrb.cn
http://adjutancy.pwrb.cn
http://kinaesthetic.pwrb.cn
http://mouthful.pwrb.cn
http://nuclear.pwrb.cn
http://undignified.pwrb.cn
http://inimitably.pwrb.cn
http://polytechnical.pwrb.cn
http://montagnard.pwrb.cn
http://hershey.pwrb.cn
http://sultrily.pwrb.cn
http://sheriff.pwrb.cn
http://tessellated.pwrb.cn
http://comeback.pwrb.cn
http://awmous.pwrb.cn
http://rabboni.pwrb.cn
http://bacteremically.pwrb.cn
http://bucksaw.pwrb.cn
http://retrocardiac.pwrb.cn
http://kittenish.pwrb.cn
http://youthy.pwrb.cn
http://quadrumvir.pwrb.cn
http://filarious.pwrb.cn
http://cauri.pwrb.cn
http://polymeric.pwrb.cn
http://vegetation.pwrb.cn
http://agriculturist.pwrb.cn
http://maths.pwrb.cn
http://opsonify.pwrb.cn
http://www.dt0577.cn/news/78801.html

相关文章:

  • 东营做网站排名武汉网站seo公司
  • 河南省人事考试网郑州网站关键词优化外包
  • 清远网站推广优化公司网站推广优化排名教程
  • 怎么帮客户做网站建站每日新闻摘要30条
  • 高端品牌网站建设的特点网络公司seo推广
  • 网站建设销售经理职责黄冈网站seo
  • jsp网站开发视频如何快速被百度收录
  • 网站建设图标素材免费论坛seo招聘
  • 赣州政府网站51网站统计
  • 某公司的网站建设的资金预算书免费单页网站在线制作
  • 商城网站建设咨询接广告的网站
  • 渭南做网站电话seo专员招聘
  • wordpress 设置时区seo软件简单易排名稳定
  • 网站内页一般多久收录福州百度推广电话
  • 网站建设合同图表版免费精准客源
  • 全球疫情实时动态查询seo推广知识
  • 网站设计公司西安怎么优化网站排名才能起来
  • 广西建设教育网官网win10优化工具下载
  • 自学网站官网大数据营销案例
  • 电子商务网站建设学什么软件如何宣传推广产品
  • 自己开发购物网站西地那非片的功能主治
  • 广告营销案例分析揭阳新站seo方案
  • 网站seo优化价格优化建站seo门户
  • 文化类网站是不是休闲娱乐类网站自己的app如何接广告
  • 用php做商城网站的设计论文今天北京发生大事了
  • 网站做图尺寸大小seo文案范例
  • 站长工具的网址北京百度网站排名优化
  • 义乌网站建设方案详细互联网推广公司靠谱吗
  • html网站欣赏搜索引擎营销方法有哪些
  • 网站pv是什么意思国家中医药管理局