c# - 批量压缩 C# 中的文件
问题描述
我正在使用 Zip Archive 方法来压缩文件夹中的文件。但是,需要在特定时间从文件夹中读取最多 10 个文件并将其压缩。如果有大量文件,比如 100 个,则需要使用 C# 创建 10 个 zip 文件夹。
我如何实现这一目标?我已经在 Windows 表单中尝试过这个 -
private void btnZip_Click(object sender, EventArgs e) {
string FolderPathToZip = txtFolderPath.Text.Trim();
//To create unique file name with date and time with nanoseconds.
string ZipFileName = "D:\\backup\\bak-" + DateTime.Now.ToString("ddMMyyyy-HHmmssfffff") + ".zip";
try {
//To check whether D:\Backup folder exists or not.
//If not exists this will create a BACKUP folder.
if (Directory.Exists("D:\\backup")) {} else {
Directory.CreateDirectory("D:\\backup");
}
//TO create a zip file.
ZipFile.CreateFromDirectory(FolderPathToZip, ZipFileName);
} catch (Exception) {
//If system throw any exception message box will display "SOME ERROR"
MessageBox.Show("Some Error");
}
//Display successfully created message to the user.
MessageBox.Show("Zip Filename : " + ZipFileName + " Created Successfully");
}
解决方案
伪代码:
- 为文件所在的路径创建一个 DirectoryInfo 对象
- 使用 DirectoryInfo.GetFiles() 获取所有文件的列表 (*current)
在循环中 (i=0; 10*i<fileList.Count; i++) 执行以下操作
- 子集 = fileList.Skip(10 * i).Take(10);
- 循环这个子集并创建 Zip 存档
要开心 :)
推荐阅读
- javascript - 如何突出显示任何网页的“标题”?
- bash - 通过 Gitlab API 访问项目变量值
- c# - 使用 c# 将嵌套模型对象复制到 DTO 对象
- python - 使用 beautifulsoup 和 urllib 库查找 li 标签
- java - java中的随机唯一字母数字字符串
- javascript - 如何在MongoDB中执行updateMany时使用外部值和字段内的值执行乘法运算
- javascript - 删除帖子时出现“posts.map 不是函数”错误
- ios - swift - 将 JSON 数据保存到本地文件
- c# - 在 Xamarin Forms 中更改背景颜色的有限时间
- html - 与其他浏览器不同,为什么 Firefox 以隐藏另一个页脚的方式显示底部粘性页脚?