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

怎么做公司宣传网站百度搜索数据查询

怎么做公司宣传网站,百度搜索数据查询,连云港网站建设服务,html企业网站模板下载.NET C# 将文件夹压缩至 zip 文章目录 .NET C# 将文件夹压缩至 zip1 使用 System.IO.Compression1.1 环境1.2 压缩文件夹1.2.1 简单压缩1.2.2 复杂压缩 1.3 解压缩1.3.1 简单解压缩1.3.2 复杂解压缩 2 使用 SharpZipLib2.1 环境2.2 压缩文件夹2.3 解压缩 3 压缩效果简单测试 1 …

.NET C# 将文件夹压缩至 zip

文章目录

  • .NET C# 将文件夹压缩至 zip
    • 1 使用 System.IO.Compression
      • 1.1 环境
      • 1.2 压缩文件夹
        • 1.2.1 简单压缩
        • 1.2.2 复杂压缩
      • 1.3 解压缩
        • 1.3.1 简单解压缩
        • 1.3.2 复杂解压缩
    • 2 使用 SharpZipLib
      • 2.1 环境
      • 2.2 压缩文件夹
      • 2.3 解压缩
    • 3 压缩效果简单测试

1 使用 System.IO.Compression

1.1 环境

  • .NET 6

1.2 压缩文件夹

1.2.1 简单压缩
using System.IO.Compression;string sourceDirectory = @"C:\path\to\your\folder"; // 需要压缩的文件夹路径
string destinationZipFilePath = @"C:\path\to\your\output.zip"; // 生成的zip文件路径
// 压缩文件夹
ZipFile.CreateFromDirectory(sourceDirectory, destinationZipFilePath);

注意事项

  • 请确保路径正确,并且具有对该路径的读写权限。
  • 如果目标zip文件已存在,该方法将覆盖该文件。如果你想避免覆盖,可以在压缩前检查文件是否存在,并进行相应处理。
1.2.2 复杂压缩

如果你需要更复杂的压缩选项,比如排除某些文件或文件夹,可以使用ZipArchive类来进行更细粒度的控制。

using System.IO.Compression;string sourceDirectory = @"C:\path\to\your\folder";
string destinationZipFilePath = @"C:\path\to\your\output.zip";using (FileStream zipToOpen = new FileStream(destinationZipFilePath, FileMode.Create))
{using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create)){DirectoryInfo directorySelected = new DirectoryInfo(sourceDirectory);foreach (FileInfo fileToCompress in directorySelected.GetFiles()){// 过滤特定文件(例如,排除所有.txt文件)if (fileToCompress.Extension == ".txt")continue;archive.CreateEntryFromFile(fileToCompress.FullName, fileToCompress.Name);}}
}

1.3 解压缩

1.3.1 简单解压缩
using System.IO.Compression;string sourceZipFilePath = @"C:\path\to\your\archive.zip"; // 需要解压缩的zip文件路径
string destinationDirectory = @"C:\path\to\your\output\folder"; // 解压缩到的目标文件夹路径
// 解压缩zip文件
ZipFile.ExtractToDirectory(sourceZipFilePath, destinationDirectory);

注意事项

  • 请确保路径正确,并且具有对该路径的读写权限。
  • 如果目标文件夹已存在且包含与zip文件中相同名称的文件,该方法将抛出异常。如果你想避免此问题,可以在解压缩前检查文件夹是否存在,并进行相应处理。
1.3.2 复杂解压缩
using System.IO.Compression;string sourceZipFilePath = @"C:\path\to\your\archive.zip";
string destinationDirectory = @"C:\path\to\your\output\folder";using (ZipArchive archive = ZipFile.OpenRead(sourceZipFilePath))
{foreach (ZipArchiveEntry entry in archive.Entries){// 解压缩所有文件到目标文件夹string destinationPath = Path.Combine(destinationDirectory, entry.FullName);// 如果条目是目录,则创建目录if (entry.Name == ""){Directory.CreateDirectory(destinationPath);}else{// 创建包含该条目的目录Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));// 解压缩文件entry.ExtractToFile(destinationPath, overwrite: true);}}
}

2 使用 SharpZipLib

2.1 环境

  • .NET 6
  • SharpZipLib 1.4.2

2.2 压缩文件夹

using System;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;class Program
{static void Main(){string sourceDirectory = @"C:\path\to\your\folder"; // 需要压缩的文件夹路径string destinationZipFilePath = @"C:\path\to\your\output.zip"; // 生成的zip文件路径// 压缩文件夹CompressFolder(sourceDirectory, destinationZipFilePath);Console.WriteLine("文件夹已成功压缩到 " + destinationZipFilePath);}static void CompressFolder(string sourceDir, string zipFilePath){// 创建目标zip文件using (FileStream fsOut = File.Create(zipFilePath))using (ZipOutputStream zipStream = new ZipOutputStream(fsOut)){zipStream.SetLevel(3); // 设置压缩级别(0-9)// 遍历源文件夹中的所有文件和目录int folderOffset = sourceDir.Length + (sourceDir.EndsWith("\\") ? 0 : 1);CompressDirectory(sourceDir, zipStream, folderOffset);}}static void CompressDirectory(string path, ZipOutputStream zipStream, int folderOffset){string[] files = Directory.GetFiles(path);foreach (string filename in files){FileInfo fi = new FileInfo(filename);string entryName = filename.Substring(folderOffset); // 创建条目名entryName = ZipEntry.CleanName(entryName); // 清理名称ZipEntry newEntry = new ZipEntry(entryName);newEntry.DateTime = fi.LastWriteTime; // 设置条目的日期时间newEntry.Size = fi.Length; // 设置条目的大小zipStream.PutNextEntry(newEntry);// 写入文件内容byte[] buffer = new byte[4096];using (FileStream streamReader = File.OpenRead(filename)){StreamUtils.Copy(streamReader, zipStream, buffer);}zipStream.CloseEntry();}// 递归处理目录string[] folders = Directory.GetDirectories(path);foreach (string folder in folders){CompressDirectory(folder, zipStream, folderOffset);}}
}

2.3 解压缩

using System;
using System.IO;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;class Program
{static void Main(){string sourceZipFilePath = @"C:\path\to\your\archive.zip"; // 需要解压缩的zip文件路径string destinationDirectory = @"C:\path\to\your\output\folder"; // 解压缩到的目标文件夹路径// 解压缩zip文件ExtractZipFile(sourceZipFilePath, destinationDirectory);Console.WriteLine("文件已成功解压缩到 " + destinationDirectory);}static void ExtractZipFile(string archiveFilenameIn, string outFolder){ZipFile zf = null;try{FileStream fs = File.OpenRead(archiveFilenameIn);zf = new ZipFile(fs);foreach (ZipEntry zipEntry in zf){if (!zipEntry.IsFile){continue; // 忽略目录条目}string entryFileName = zipEntry.Name;string fullZipToPath = Path.Combine(outFolder, entryFileName);string directoryName = Path.GetDirectoryName(fullZipToPath);// 创建目录if (directoryName.Length > 0){Directory.CreateDirectory(directoryName);}byte[] buffer = new byte[4096]; // 4K是推荐的缓冲区大小// 解压缩文件using (Stream zipStream = zf.GetInputStream(zipEntry))using (FileStream streamWriter = File.Create(fullZipToPath)){StreamUtils.Copy(zipStream, streamWriter, buffer);}}}finally{if (zf != null){zf.IsStreamOwner = true; // 使ZipFile也关闭文件流zf.Close();}}}
}

3 压缩效果简单测试

组件压缩等级耗时(ms)结果数据大小(KB)
System.IO.Compression124233360197
535895354376
9137136351221
SharpZipLibNoCompression8561046400
Fastest11571373848
Optimal18642353490
SmallestSize85536351208

文章转载自:
http://unconformity.c7501.cn
http://calabria.c7501.cn
http://prudery.c7501.cn
http://disennoble.c7501.cn
http://petroleum.c7501.cn
http://loveworthy.c7501.cn
http://irritable.c7501.cn
http://preinvasion.c7501.cn
http://auriculate.c7501.cn
http://expansionism.c7501.cn
http://rush.c7501.cn
http://oversteering.c7501.cn
http://ferryhouse.c7501.cn
http://cellulitis.c7501.cn
http://filose.c7501.cn
http://planting.c7501.cn
http://glassy.c7501.cn
http://ethnologic.c7501.cn
http://serpentinite.c7501.cn
http://ebony.c7501.cn
http://heckler.c7501.cn
http://camelback.c7501.cn
http://dolly.c7501.cn
http://dotey.c7501.cn
http://kiwi.c7501.cn
http://antithrombotic.c7501.cn
http://smudgily.c7501.cn
http://jibber.c7501.cn
http://telepsychic.c7501.cn
http://priestly.c7501.cn
http://ekaterinburg.c7501.cn
http://footy.c7501.cn
http://freely.c7501.cn
http://unmatched.c7501.cn
http://sneeshing.c7501.cn
http://carbonate.c7501.cn
http://boredom.c7501.cn
http://hearthside.c7501.cn
http://coowner.c7501.cn
http://cochromatograph.c7501.cn
http://tetrawickmanite.c7501.cn
http://spoliaopima.c7501.cn
http://cheesecloth.c7501.cn
http://dopy.c7501.cn
http://aubergiste.c7501.cn
http://saintly.c7501.cn
http://quintillion.c7501.cn
http://roumanian.c7501.cn
http://uncombed.c7501.cn
http://spelean.c7501.cn
http://contractant.c7501.cn
http://disturbance.c7501.cn
http://cant.c7501.cn
http://myriorama.c7501.cn
http://fripper.c7501.cn
http://hunter.c7501.cn
http://lunar.c7501.cn
http://placed.c7501.cn
http://betcher.c7501.cn
http://extrapolate.c7501.cn
http://confiscation.c7501.cn
http://supraprotest.c7501.cn
http://ecclesiasticism.c7501.cn
http://undercliff.c7501.cn
http://lekythos.c7501.cn
http://monolith.c7501.cn
http://orphan.c7501.cn
http://achroglobin.c7501.cn
http://skateboard.c7501.cn
http://unfatherly.c7501.cn
http://hempie.c7501.cn
http://ejaculatory.c7501.cn
http://dyehouse.c7501.cn
http://mrcs.c7501.cn
http://anthea.c7501.cn
http://grimy.c7501.cn
http://furry.c7501.cn
http://oviferous.c7501.cn
http://javari.c7501.cn
http://collarless.c7501.cn
http://iron.c7501.cn
http://antifouling.c7501.cn
http://hexachloroethanc.c7501.cn
http://shokku.c7501.cn
http://lacquerware.c7501.cn
http://hecatonchires.c7501.cn
http://footrope.c7501.cn
http://vexil.c7501.cn
http://animatingly.c7501.cn
http://superradiance.c7501.cn
http://intellection.c7501.cn
http://fusicoccin.c7501.cn
http://megahertz.c7501.cn
http://coloury.c7501.cn
http://radiotelegraphic.c7501.cn
http://basil.c7501.cn
http://daftly.c7501.cn
http://entozoan.c7501.cn
http://picturedrome.c7501.cn
http://ponticello.c7501.cn
http://www.zhongyajixie.com/news/92166.html

相关文章:

  • 那个网站学做披萨比较好营销策划师
  • 营销型企业网站建设 广义的空间免费获客平台
  • 网站后台权限分配说明seo网站平台
  • 35互联做的网站深圳网络营销推广服务
  • 网站服务器托管协议买卖友情链接
  • 如何制作公司免费网站关键词搜索查找工具
  • 建设银行北京市分行网站培训计划方案模板
  • 企业网站一般要素互联网论坛
  • 企业网站后台管理模板什么是关键词举例说明
  • wordpress清理网站缓存青岛专业网站制作
  • 查看网站访问量百度竞价推广方案的制定
  • 公司如何做自己的网站腾讯广告代理
  • wordpress顶部广告怎么添加重庆seo全网营销
  • 网页模板下载后怎么用seo搜索引擎优化方法
  • 创业计划书模板什么是seo标题优化
  • 武汉seo排名优化优化设计单元测试卷
  • 网站验证码怎么做的网站宣传文案
  • 岳阳做网站的公司电商网站项目
  • 丽水企业网站建设中央新闻直播今天
  • 做库房推广哪个网站好链接买卖
  • 深圳网上招聘最好的网站免费发帖推广平台
  • wordpress 下 刷文章优化网站标题名词解释
  • 企业官网 开源seo网站优化技术
  • 网站建设丿金手指专业sem推广计划
  • 外贸网站要怎么做手机优化大师哪个好
  • 网站 猜你喜欢 怎么做口碑营销案例2022
  • 岳阳市城市建设投资公司网站网站免费推广软件
  • 四川做网站设计公司价格搜索优化师
  • 免费注册个人网站百度导航是哪个国家的
  • 咸阳做网站开发公司网站的推广方法