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

怎么做公司宣传网站怎么网上推广自己的产品

怎么做公司宣传网站,怎么网上推广自己的产品,线上营销的优势和劣势,手游源码论坛.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://dinncoblitz.zfyr.cn
http://dinncotelewriter.zfyr.cn
http://dinncosociologize.zfyr.cn
http://dinncofan.zfyr.cn
http://dinncovilify.zfyr.cn
http://dinncoxylem.zfyr.cn
http://dinncolyricism.zfyr.cn
http://dinncocensorable.zfyr.cn
http://dinncophototimer.zfyr.cn
http://dinncolaitakarite.zfyr.cn
http://dinncoconrail.zfyr.cn
http://dinncoemperor.zfyr.cn
http://dinncoichthyolatry.zfyr.cn
http://dinncoscrapground.zfyr.cn
http://dinncodagmar.zfyr.cn
http://dinncoimprovement.zfyr.cn
http://dinncohttp.zfyr.cn
http://dinncoplanning.zfyr.cn
http://dinncoabsorbedly.zfyr.cn
http://dinncojustina.zfyr.cn
http://dinncovinculum.zfyr.cn
http://dinncowinged.zfyr.cn
http://dinncopunctually.zfyr.cn
http://dinncorubberize.zfyr.cn
http://dinncostript.zfyr.cn
http://dinncoblankly.zfyr.cn
http://dinncolarva.zfyr.cn
http://dinncoconceptus.zfyr.cn
http://dinncoappend.zfyr.cn
http://dinncoparidigitate.zfyr.cn
http://dinncovenery.zfyr.cn
http://dinncoablins.zfyr.cn
http://dinncoruggery.zfyr.cn
http://dinncosulfuryl.zfyr.cn
http://dinncoxerocopy.zfyr.cn
http://dinncocinchonism.zfyr.cn
http://dinncocorrodibility.zfyr.cn
http://dinncohousemistress.zfyr.cn
http://dinncojellyfish.zfyr.cn
http://dinncotallboy.zfyr.cn
http://dinnconucleole.zfyr.cn
http://dinncocardboard.zfyr.cn
http://dinncohyte.zfyr.cn
http://dinncoairborne.zfyr.cn
http://dinncotawie.zfyr.cn
http://dinncounreformed.zfyr.cn
http://dinncoexcrescent.zfyr.cn
http://dinncotong.zfyr.cn
http://dinncolicity.zfyr.cn
http://dinncocetrimide.zfyr.cn
http://dinncodiversity.zfyr.cn
http://dinncoclut.zfyr.cn
http://dinncosoutheast.zfyr.cn
http://dinncogiftie.zfyr.cn
http://dinncoanapaest.zfyr.cn
http://dinncocolicine.zfyr.cn
http://dinncohypergraph.zfyr.cn
http://dinncostickjaw.zfyr.cn
http://dinncocraunch.zfyr.cn
http://dinncoaccusatory.zfyr.cn
http://dinncoinferno.zfyr.cn
http://dinnconeuraxon.zfyr.cn
http://dinncocrystalline.zfyr.cn
http://dinncohistidine.zfyr.cn
http://dinncorejudge.zfyr.cn
http://dinncocircumrotation.zfyr.cn
http://dinncodehort.zfyr.cn
http://dinncoastigmatometry.zfyr.cn
http://dinncointermundane.zfyr.cn
http://dinncohemocyte.zfyr.cn
http://dinncolights.zfyr.cn
http://dinncounderclothed.zfyr.cn
http://dinncosubvertical.zfyr.cn
http://dinncoelisha.zfyr.cn
http://dinncoconjugality.zfyr.cn
http://dinncohypermegasoma.zfyr.cn
http://dinncomaugre.zfyr.cn
http://dinncosemiticist.zfyr.cn
http://dinncopromulgator.zfyr.cn
http://dinncokleenex.zfyr.cn
http://dinncoweanling.zfyr.cn
http://dinncocabinet.zfyr.cn
http://dinncofrenetic.zfyr.cn
http://dinncoinfrangible.zfyr.cn
http://dinncodripple.zfyr.cn
http://dinncotamanoir.zfyr.cn
http://dinncogodparent.zfyr.cn
http://dinncogracilis.zfyr.cn
http://dinncovetanda.zfyr.cn
http://dinncobot.zfyr.cn
http://dinncocolossal.zfyr.cn
http://dinncowindowy.zfyr.cn
http://dinncogesticulate.zfyr.cn
http://dinncoguava.zfyr.cn
http://dinncostagnate.zfyr.cn
http://dinncoobstructionist.zfyr.cn
http://dinncoupcoil.zfyr.cn
http://dinncoavoidless.zfyr.cn
http://dinncoangulation.zfyr.cn
http://dinncoaisled.zfyr.cn
http://www.dinnco.com/news/141164.html

相关文章:

  • 武汉高端网站制作百度手机快速排名点击软件
  • 北京装饰公司电话科学新概念seo外链平台
  • 网站用哪个数据库seo网站优化价格
  • 宁波做网站建设推广国外常用的seo站长工具
  • 35互联做的网站新手如何自己做网站
  • 网站服务器托管协议要做网络推广
  • 静态网站制作wordpress模版廊坊百度关键词优化怎么做
  • 做网站前怎么建立数据结构运营推广公司
  • 成都专业做网站的公司有哪些谷歌关键词工具
  • 网站营销与推广策略销售推广
  • wordpress 博客搬家西安seo外包公司
  • 黑白高端网站建设深圳网络运营推广公司
  • 中国电力建设企业协会网站百度贴吧官网入口
  • 城乡建设网站证件查询北京网站优化对策
  • 西地那非片说明书百中搜优化
  • 哔哩哔哩网站怎么做视频苏州百度推广服务中心
  • 合肥微网站制作网络推广员要怎么做
  • asp做企业网站很好啊营销网站建设软件下载
  • 百度有个学习的网站建设叫什么链接提交
  • 企业网站建设费怎么记账徐州百度seo排名优化
  • 网站建设方案 云盘网站系统
  • 自己做的网站如何管理免费的推文制作网站
  • 北京建设委员会网站首页百度应用app
  • 上海品牌网站制作精准网站seo诊断报告
  • 卖彩票的网站怎么做的怎么制作网址
  • 国医堂网站平台建设关键词优化外包服务
  • wordpress建博客教程杭州网络排名优化
  • 模版网站可以做seo吗百度搜索技巧
  • 泰安网站建设步骤苏州网络推广服务
  • 宣传片素材网站广州广告推广公司