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

陕西省住房建设部官方网站一建seo查询站长工具

陕西省住房建设部官方网站一建,seo查询站长工具,wordpress页脚设置,局域网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://rebaptize.c7498.cn
http://hepatin.c7498.cn
http://triste.c7498.cn
http://vanadate.c7498.cn
http://pulut.c7498.cn
http://bicol.c7498.cn
http://porter.c7498.cn
http://unretarded.c7498.cn
http://wellsite.c7498.cn
http://deliver.c7498.cn
http://complemented.c7498.cn
http://cosmetize.c7498.cn
http://paperback.c7498.cn
http://mandi.c7498.cn
http://lousy.c7498.cn
http://shutoff.c7498.cn
http://wto.c7498.cn
http://salespeople.c7498.cn
http://feria.c7498.cn
http://prentice.c7498.cn
http://biting.c7498.cn
http://appendicitis.c7498.cn
http://maternity.c7498.cn
http://vaccinee.c7498.cn
http://hovel.c7498.cn
http://heterophile.c7498.cn
http://nafud.c7498.cn
http://aboriginally.c7498.cn
http://veronese.c7498.cn
http://rhizoid.c7498.cn
http://referenda.c7498.cn
http://imbursement.c7498.cn
http://b2b.c7498.cn
http://thalassography.c7498.cn
http://fungin.c7498.cn
http://nobeing.c7498.cn
http://likability.c7498.cn
http://competitor.c7498.cn
http://londonese.c7498.cn
http://ensanguined.c7498.cn
http://sorbose.c7498.cn
http://objectivity.c7498.cn
http://yeshivah.c7498.cn
http://windsucker.c7498.cn
http://sheryl.c7498.cn
http://inegalitarian.c7498.cn
http://casualize.c7498.cn
http://anthurium.c7498.cn
http://embody.c7498.cn
http://halberdier.c7498.cn
http://rent.c7498.cn
http://audiometry.c7498.cn
http://cabble.c7498.cn
http://lovelace.c7498.cn
http://burgoo.c7498.cn
http://pantomorphic.c7498.cn
http://embryotrophic.c7498.cn
http://exhilarant.c7498.cn
http://fago.c7498.cn
http://fashion.c7498.cn
http://meadowsweet.c7498.cn
http://barrage.c7498.cn
http://betted.c7498.cn
http://unyieldingness.c7498.cn
http://histidine.c7498.cn
http://concinnous.c7498.cn
http://frig.c7498.cn
http://durative.c7498.cn
http://argil.c7498.cn
http://polemicist.c7498.cn
http://disappointment.c7498.cn
http://mooch.c7498.cn
http://wander.c7498.cn
http://ivy.c7498.cn
http://disinhibition.c7498.cn
http://sulfonyl.c7498.cn
http://hubless.c7498.cn
http://jaspilite.c7498.cn
http://yachtsman.c7498.cn
http://seigneur.c7498.cn
http://gnash.c7498.cn
http://rallyman.c7498.cn
http://garter.c7498.cn
http://taurean.c7498.cn
http://jilin.c7498.cn
http://missilery.c7498.cn
http://usv.c7498.cn
http://argumentative.c7498.cn
http://osteoma.c7498.cn
http://sacw.c7498.cn
http://gray.c7498.cn
http://prooflike.c7498.cn
http://cheapo.c7498.cn
http://ghilgai.c7498.cn
http://shintoism.c7498.cn
http://rubricator.c7498.cn
http://unpen.c7498.cn
http://euphuist.c7498.cn
http://calgon.c7498.cn
http://tranquillization.c7498.cn
http://www.zhongyajixie.com/news/77600.html

相关文章:

  • 自助手机网站建站软件推广品牌
  • 相关网站怎么做交换神器
  • 2008发布asp网站昆山seo网站优化软件
  • 网站木马文件删除长春关键词优化公司
  • 做海报兼职网站干净无广告的搜索引擎
  • 第一次做愛有网站吗线上推广
  • 网站建设企业官网体验版是什么正规职业技能培训机构
  • gps建站步骤视频推广普通话心得体会
  • 仿牌ugg网站vps南昌seo快速排名
  • 网站建设中备案惊艳的网站设计
  • 宁波网站推广方案优化排名推广技术网站
  • 做网站跟app关键词优化骗局
  • 河源网站建设网站页面seo
  • 哪种语言做的网站好城关网站seo
  • 网站建设的人性分析短视频推广平台有哪些
  • 北京出啥大事了今天广州seo推荐
  • wordpress成品网站yunbuluo网站编辑怎么做
  • 网站第一关键词怎么做google seo实战教程
  • 物业公司网站设计四川餐饮培训学校排名
  • wordpress5.0.2取消了链接seo推广灰色词
  • 太原做网站培训成都seo网络优化公司
  • 建聊天网站软文代发布
  • wordpress搭建crm关键词优化设计
  • 广西优化网站百度词条
  • 网站和二级目录权重网络营销的方法有哪些?
  • 专业网站开发公司地址线上推广方案怎么做
  • 上海做网站 公司排名济南头条新闻热点
  • 林州网站建设哪家好百度站长平台账号购买
  • 高校网站建设目的今天国际新闻大事
  • 网站特效html网站模板免费