检查zip文件是否在另一个目录中 [英] Check files of a zip file exists in another directory

查看:53
本文介绍了检查zip文件是否在另一个目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无内容检查

我有两个目录C:\Workspace\TestProject\ZipFileA.zip\作为目录' A '和C:\Workspace\Reports\Latest\作为目录' B '......内部目录 A ,其中有多个日志文件,并且在目录' B '中有多个日志文件...

我需要检查目录"B"中是否存在目录"A"的每个日志文件.如果目录"A"的任何日志文件不存在或在目录"B"中不存在,则我需要引发异常或返回false.基本上,A目录的文件应该位于'B'目录中.如果"B"包含不在"A"中的其他文件,则没有问题

我如何检查并返回bool/throw异常?

解决方案

使用Intersect方法返回两个列表中都存在的值

var dirAFiles = System.IO.Directory.GetFiles("c:\\dirA").Select(s => System.IO.Path.GetFileName(s));
var dirBFiles = System.IO.Directory.GetFiles("c:\\dirB").Select(s => System.IO.Path.GetFileName(s));
var filesInDirAAndDirB = dirAFiles.Intersect(dirBFiles );

别忘了选择文件名,因为GetFiles返回文件的完整路径

要引发异常,只需从A列表中排除A和B中存在的文件.如果存在任何文件,则错误

if (dirAFiles.Except(filesInDirAAndDirB).Any())
{
// error
}

因为没有必要将两个目录中的文件列表相交,所以删除的其他答案要好得多.一个简单的

dirAFiles.Except(dirBFiles).Any()

足以确定dirA中是否存在任何元素而dirB中不存在. 哦,好吧,没有像杀人这样的杀人了:

No Content check

I have two directories C:\Workspace\TestProject\ZipFileA.zip\ as Directory 'A' and C:\Workspace\Reports\Latest\ as Directory 'B' ......Inside directory A, multiple Log files are there and inside Directory 'B' multiple log files are there...

I need to check each log file of Directory 'A' is present in Directory 'B' or not. Incase If any log file of Directory 'A' is NOT exists or doesn't present in Directory 'B' then i need to throw an exception or return false. Basically, files of A directory should be present in 'B' directory. No Issues if 'B' contains additional files which are not in 'A'

How can i check and return bool/throw exception?

解决方案

Use the Intersect method to return values present in both lists

var dirAFiles = System.IO.Directory.GetFiles("c:\\dirA").Select(s => System.IO.Path.GetFileName(s));
var dirBFiles = System.IO.Directory.GetFiles("c:\\dirB").Select(s => System.IO.Path.GetFileName(s));
var filesInDirAAndDirB = dirAFiles.Intersect(dirBFiles );

Don't forget to select the filename only because GetFiles returns the full path of the files

To throw an exception just exclude the files existing in A and B from the list of A. If any file exists, you have an error

if (dirAFiles.Except(filesInDirAAndDirB).Any())
{
// error
}

EDIT: Other answers that were deleted since were much better since it is not necessary to intersect the file list from both directories. A simple

dirAFiles.Except(dirBFiles).Any()

is sufficient to find out if any element in dirA exists without being present in dirB. Oh well, there's not kill like overkill :p

这篇关于检查zip文件是否在另一个目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆