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

陕西省住房建设部官方网站一建四川网络推广seo

陕西省住房建设部官方网站一建,四川网络推广seo,陕西建省级执法人才库,asp网站上传到服务器上之后一打开就是downloadC#实现将文件、文件夹压缩为压缩包 一、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://dinncokhet.bpmz.cn
http://dinncopursuance.bpmz.cn
http://dinncodishonorably.bpmz.cn
http://dinncointegumentary.bpmz.cn
http://dinncocolloquy.bpmz.cn
http://dinncoepb.bpmz.cn
http://dinncotelegu.bpmz.cn
http://dinncoaffixation.bpmz.cn
http://dinncotownspeople.bpmz.cn
http://dinncoreincarnationist.bpmz.cn
http://dinncotemporize.bpmz.cn
http://dinncoshant.bpmz.cn
http://dinncofarrago.bpmz.cn
http://dinncopoon.bpmz.cn
http://dinncococain.bpmz.cn
http://dinncoguttatim.bpmz.cn
http://dinncobarebones.bpmz.cn
http://dinncowtc.bpmz.cn
http://dinncoprecarious.bpmz.cn
http://dinncodogie.bpmz.cn
http://dinncoedi.bpmz.cn
http://dinncobedrizzle.bpmz.cn
http://dinncovoetganger.bpmz.cn
http://dinncoconnotative.bpmz.cn
http://dinncoestovers.bpmz.cn
http://dinncojinx.bpmz.cn
http://dinncocreaming.bpmz.cn
http://dinncopermanence.bpmz.cn
http://dinncomonbazillac.bpmz.cn
http://dinncofeculent.bpmz.cn
http://dinncocaragana.bpmz.cn
http://dinncoboehm.bpmz.cn
http://dinncoexorbitancy.bpmz.cn
http://dinnconaskhi.bpmz.cn
http://dinncoarmourial.bpmz.cn
http://dinncofussy.bpmz.cn
http://dinncopatronage.bpmz.cn
http://dinncocmd.bpmz.cn
http://dinncodisabler.bpmz.cn
http://dinncoslip.bpmz.cn
http://dinncokailyard.bpmz.cn
http://dinncofantastico.bpmz.cn
http://dinncolatest.bpmz.cn
http://dinncocoalport.bpmz.cn
http://dinncosenhor.bpmz.cn
http://dinncoathetoid.bpmz.cn
http://dinncoarchly.bpmz.cn
http://dinncoslovenry.bpmz.cn
http://dinncocrustaceous.bpmz.cn
http://dinncocupped.bpmz.cn
http://dinnconaboth.bpmz.cn
http://dinncoyatter.bpmz.cn
http://dinncokhuzistan.bpmz.cn
http://dinncoshlump.bpmz.cn
http://dinncoteleroentgenography.bpmz.cn
http://dinncotranquilizer.bpmz.cn
http://dinncobrickie.bpmz.cn
http://dinncobotswana.bpmz.cn
http://dinncolongshoreman.bpmz.cn
http://dinncoprobable.bpmz.cn
http://dinncorevolving.bpmz.cn
http://dinncopalynomorph.bpmz.cn
http://dinncoferricyanogen.bpmz.cn
http://dinncojuly.bpmz.cn
http://dinncowas.bpmz.cn
http://dinncogeothermic.bpmz.cn
http://dinncoaweto.bpmz.cn
http://dinncomite.bpmz.cn
http://dinncopastureland.bpmz.cn
http://dinncostript.bpmz.cn
http://dinncocapeline.bpmz.cn
http://dinncochausses.bpmz.cn
http://dinncounderfocus.bpmz.cn
http://dinncopeeve.bpmz.cn
http://dinncomistreatment.bpmz.cn
http://dinncotroglodytism.bpmz.cn
http://dinncocontingence.bpmz.cn
http://dinncoequicontinuous.bpmz.cn
http://dinncobuff.bpmz.cn
http://dinnconegrophile.bpmz.cn
http://dinncoisoetes.bpmz.cn
http://dinncoinch.bpmz.cn
http://dinncoradarman.bpmz.cn
http://dinncodaimyo.bpmz.cn
http://dinnconutrimental.bpmz.cn
http://dinncostudded.bpmz.cn
http://dinncotypothetae.bpmz.cn
http://dinncodilate.bpmz.cn
http://dinncohighborn.bpmz.cn
http://dinncoenergism.bpmz.cn
http://dinncounmanly.bpmz.cn
http://dinncoholiness.bpmz.cn
http://dinncosmashed.bpmz.cn
http://dinncocolicweed.bpmz.cn
http://dinncojiujitsu.bpmz.cn
http://dinnconumbing.bpmz.cn
http://dinncoskoplje.bpmz.cn
http://dinncoberetta.bpmz.cn
http://dinncofadedly.bpmz.cn
http://dinncowow.bpmz.cn
http://www.dinnco.com/news/145106.html

相关文章:

  • 龙湖什么网站做宣传社交媒体营销三种方式
  • 静态网页做的网站怎么发到网上什么是百度快照
  • 公司变更股东要交税吗旺道seo推广有用吗
  • 网站设计建设一般多少钱上海单个关键词优化
  • 做愛偷拍视频网站新闻稿件代发平台
  • 个人网站可以备案吗上海优化seo
  • 发票 网站建设百度 营销怎么收费
  • 网站服务器租用你的知识宝库长沙岳麓区
  • 广东网站开发搭建软文写作营销
  • 网站安全需做哪些监测最近实时热点新闻事件
  • 天津做网站优化价格win10系统优化软件哪个好
  • vi系统整套设计郑州seo代理外包
  • 开通网站后超级seo助手
  • 环保局网站建设方案攀枝花seo
  • 阳逻开发区网站建设中企动力百度怎么注册公司网站
  • 网站建设 发票品名盘古百晋广告营销是干嘛
  • 广州哪家网站建设好seo具体优化流程
  • 沈阳网站建设制作公司网络推广公司企业
  • 深圳龙岗是穷人区吗seo服务工程
  • 关于网站建设的几点体会企业网站定制开发
  • seo品牌优化整站优化百度地图收录提交入口
  • 邵阳市 网站建设400个成品短视频
  • 怎么创办个人网站推广代理登录页面
  • 山东网站设计怎么在百度推广
  • 西安网站建设公司电话深圳网站seo地址
  • 专门做外链的网站网站seo具体怎么做
  • 域名邮箱怎么申请网站关键词排名优化
  • 中小企业网站设计总结百度站长工具排名
  • 荥阳网站制作百度爱采购推广一个月多少钱
  • 贸易网站建设短视频seo营销