C# - 基于年月自动压缩文件夹中的文件

发布于 2024-08-04 02:18:32 字数 276 浏览 2 评论 0原文

IT 部门的任务是降低文件服务器的使用率,因此我想尽自己的一份力量来压缩旧文件(即 Excel/Access/Txt)。

我们有一些包含数千个文件的文件夹,因此我不想将整个目录压缩成一个大文件 - 最好有许多较小的文件,以便用户更容易找到数据他们正在寻找“桶”。

有没有一种方法使用 C# 读取目录并将文件压缩到年月组中(年月中的所有文件放在一个 zip 中)?

或者使用像 AutoIT 这样的脚本会更好吗?

或者是否已经存在可以执行此操作的程序,因此我无需编写任何代码?

IT has been tasked with reducing the file-server usage rate so I'd like to do my part my compressing old files(ie Excel/Access/Txt).

We have some folders that contain thousands of files so I don't want to just zip the whole directory into one large file - it would be preferrable to have a number of smaller files so that it would be easier for a user to find the data 'bucket' they are looking for.

Is there a way using C# to read through a directory and zip the files into year-month groups (all files from year-month placed together in one zip)?

Or would it be better to use a script like AutoIT?

Or are there programs already existing to do this so I don't have to code anything?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

狼性发作 2024-08-11 02:18:32

我不确定您的问题是否与压缩、从特定年份/月份或两者中选择文件有关。

关于压缩 Peter 已经提到了 7-zip 和 SharpZipLib。我个人只有后者的经验,但它都是积极的,易于合作。

关于对文件进行分组,可以通过迭代文件夹中的所有文件并按创建日期或上次修改日期对它们进行分组来完成。

伪:

var files = new Dictionary<DateTime, IList<string>>();
foreach (var file in Directory.GetFiles(...)) {
   var fi = new FileInfo(file);
   var date = fi.CreatedDate();
   var groupDate = new DateTime(date.Year, date.Month);

   if (!files.ContainsKey(groupDate)) files.Add(groupDate, new Collection<string>());

   files[groupDate].Add(file);
}

现在您应该有一个包含不同年/月键的字典,并且 foreach 键包含属于该组的文件列表。所以对于压缩

伪:

foreach (var entry in files) {
   var date = entry.Key;
   var list = entry.Value;

   // create zip-file named date.ToString();
   foreach (var file in list) {
      // add file to zip
   } 
}

Im not sure if your question is about zipping, selecting files from particular year/month or both.

About zipping Peter already mentioned 7-zip and SharpZipLib. I have personally only experience with the latter but its all positive, easy to work with.

About grouping your files it could be done by iterating all the files in the folder and group them by either there created date or last modified date.

pseudo:

var files = new Dictionary<DateTime, IList<string>>();
foreach (var file in Directory.GetFiles(...)) {
   var fi = new FileInfo(file);
   var date = fi.CreatedDate();
   var groupDate = new DateTime(date.Year, date.Month);

   if (!files.ContainsKey(groupDate)) files.Add(groupDate, new Collection<string>());

   files[groupDate].Add(file);
}

now your should have a dictionary containing distinct year/month keys and foreach key a list of files belonging to that group. So for zipping

pseudo:

foreach (var entry in files) {
   var date = entry.Key;
   var list = entry.Value;

   // create zip-file named date.ToString();
   foreach (var file in list) {
      // add file to zip
   } 
}
一笑百媚生 2024-08-11 02:18:32

当然,您可以使用一些 C# 和库(如 7-zip 或 < a href="http://www.sharpdevelop.net/OpenSource/SharpZipLib/Default.aspx" rel="nofollow noreferrer">SharpZipLib。

Surely you can do this with a bit of C# and libraries like 7-zip or SharpZipLib.

染墨丶若流云 2024-08-11 02:18:32

您可以使用 System.IO.Directory.GetFiles() 循环遍历每个目录上的文件,按文件名解析并将它们添加到 7-zip 或 SharpZipLib 对象中。如果有数千个文件,最好将其放入服务或某种计划任务中以过夜运行,以免对文件共享造成负担。

祝你好运!

编辑: 作为附录,如果您需要按创建日期或其他文件属性进行解析,则可以为每个文件使用 System.IO.FileInfo 对象。

You could use System.IO.Directory.GetFiles() to loop through the files on each directory, parsing out by file name and adding them to a 7-zip or SharpZipLib object. If it's thousands of files it might be best to throw it in a service or some kind of scheduled task to run overnight so as not to tax the fileshare.

Good luck to you !

EDIT: As an addendum you could use a System.IO.FileInfo object for each file if you need to parse by created date or other file attirbutes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文