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

app网站制作软件有哪些直通车官网

app网站制作软件有哪些,直通车官网,花都网络推广seo公司,济宁专业做优化的网站.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://www.yayakq.cn/news/792629/

相关文章:

  • 英国网站域名做网站需要数据库么
  • 平面设计师用的网站个人做搜索引擎网站违法吗
  • 阐述网站建设的步骤品牌设计公司宣传画册
  • 单位如何建设网站wordpress支持内网和外网
  • 做企业网站的尺寸是多少钱宝塔面安装wordpress
  • 做一个购物商城网站多少钱wordpress+支付宝+微信
  • 公司网站定制互联网创业有哪些项目
  • 网站开发与优化课程总结商城建站费用
  • 期末网站设计做什么网站比较好苏州网站建设推广咨询平台
  • 陕西省交通建设集团公司门户网站珠海中企网站建设公司
  • 网站开发设计比赛网站开发确认书
  • 徐州集团网站建设方案网络公司排名前十名有哪些
  • 网站做的拖管不行 怎么投诉女生学网络工程难吗
  • 电子商务如何设计网站建设wordpress建站赚钱
  • 青岛做网站哪个最好购物网站主要的功能模块
  • 美工做兼职在那个网站卡片式设计 网站
  • 成都搜索优化整站优化wordpress 环镜
  • 网站建设 启象科技电商网站零售客户
  • 深圳网站制作的公司如何做外贸soho做网站
  • 公司网站被百度收录水果网站源码
  • 局域网内服务器做网站哈尔滨seo优化排名免费咨询
  • 长治哪里能找到做网站的技术员群晖nas可以做网站吗
  • php注册网站源码带数据库如何自己做优惠卷网站
  • 做音乐分享的网站淘宝客建网站
  • 郑州做公司网站嘉兴网站快速排名优化
  • 网站迁移到别的服务器要怎么做织梦网站分享插件
  • 自己怎么做装修网站什么公司会招网站建设
  • 网站开发企业官网软件开发外包方案
  • dede手机网站跳转内蒙营销型网站建设
  • 健康咨询类网站模板cn域名后缀网站