如何收集目录中的每个文件的CRC校验一起上市?(How to collect directory l

2019-10-17 04:17发布

我用下面的命令来获取目录中上市尼克斯(Linux操作系统,AIX,SunOS中,HP-UX)平台

命令

ls -latr 

输出继电器

drwxr-xr-x  2 ricky support   4096 Aug 29 11:59 lib 
-rwxrwxrwx  1 ricky support    924 Aug 29 12:00 initservice.sh

cksum命令用于获取CRC校验。

如何CRC校验每个文件的东西(包括目录清单也行)下面一样被追加后,保持以下格式在这些nix中(Linux操作系统,AIX,SunOS中,HP-UX)平台?

drwxr-xr-x  2 ricky support   4096 Aug 29 11:59 lib 
-rwxrwxrwx  1 ricky support    924 Aug 29 12:00 initservice.sh 4287252281

更新注意 :任何第三方应用程序,我使用的Java / Groovy的最终解析输出到使用Groovy的XmlSlurper形成一个xml给定的格式(我们得到5MB左右生成的XML大小)

"permission","hardlink","owner","group","fsize","month","date","time","filename","checksum"

所有建议都欢迎! :)

我的代码更新

但是在这里我计算md5hex这给类似的输出md5sum从Linux命令。 所以它不再cksum ,因为我无法使用某些许可证问题的jacksum BCZ :(

class CheckSumCRC32 {

public def getFileListing(String file){
    def dir = new File(file)
    def filename = null
    def md5sum = null
    def filesize = null
    def lastmodified = null
    def lastmodifiedDate = null
    def lastmodifiedTime = null
    def permission = null
    Format formatter = null
    def list=[]
    if(dir.exists()){
        dir.eachFileRecurse (FileType.FILES) { fname ->
            list << fname
          }
        list.each{fileob->
            try{
                md5sum=getMD5CheckSum(fileob.toString())
                filesize=fileob.length()+"b"
                lastmodified=new Date(fileob.lastModified())
                lastmodifiedDate=lastmodified.format('dd/MM/yyyy')
                formatter=new SimpleDateFormat("hh:mm:ss a")
                lastmodifiedTime=formatter.format(lastmodified)
                permission=getReadPermissions(fileob)+getWritePermissions(fileob)+getExecutePermissions(fileob)
                filename=getRelativePath("E:\\\\temp\\\\recurssive\\\\",fileob.toString())
                println "$filename, $md5sum, $lastmodifiedDate, $filesize, $permission, $lastmodifiedDate, $lastmodifiedTime "
            }
            catch(IOException io){
                println io
            }
            catch(FileNotFoundException fne){
                println fne
            }   
            catch(Exception e){
                println e
            }           
        }
    }       
}

public def getReadPermissions(def file){
    String temp="-"
    if(file.canRead())temp="r"
    return temp
}
public def getWritePermissions(def file){
    String temp="-"
    if(file.canWrite())temp="w"
    return temp
}
public def getExecutePermissions(def file){
    String temp="-"
    if(file.canExecute())temp="x"
    return temp
}
public def getRelativePath(def main, def file){""
    return file.toString().replaceAll(main, "")
}


public static void main(String[] args) {
    CheckSumCRC32 crc = new CheckSumCRC32();
    crc.getFileListing("E:\\temp\\recurssive")
}
}

产量

release.zip, 25f995583144bebff729086ae6ec0eb2, 04/06/2012, 6301510b, rwx, 04/06/2012, 02:46:32 PM 
file\check\release-1.0.zip, 3cc0f2b13778129c0cc41fb2fdc7a85f, 18/07/2012, 11786307b, rwx, 18/07/2012, 04:13:47 PM 
file\Dedicated.mp3, 238f793f0b80e7eacf5fac31d23c65d4, 04/05/2010, 4650908b, rwx, 04/05/2010, 10:45:32 AM 

但我仍需要一种方法来计算硬链接,所有者组。 我搜索了它看起来像净java7有这个能力与我坚持的Java6。 任何帮助吗?

Answer 1:

看看http://www.jonelo.de/java/jacksum/index.html -据报道提供校验和-兼容CRC32校验。

顺便说一句,我尝试使用java.util.zip.CRC32计算校验和校验和相比呢,所以必须使用略有不同的算法,它给出了一个不同的值。

编辑:我试过jacksum,和它的作品,但你必须告诉它使用的“校验和”算法-显然这 CRC32,这jacksum也支持不同。



Answer 2:

嗯,你可以运行命令,那么,对于每一行,运行cksum ,并将其附加到该行。

我做了以下内容:

dir = "/home/will"

"ls -latr $dir".execute().in.eachLine { line ->

  // let's omit the first line, which starts with "total"
  if (line =~ /^total/) return

  // for directories, we just print the line
  if (line =~ /^d/) 
  {
    println line
  } 
  else 
  {
    // for files, we split the line by one or more spaces and join 
    // the last pieces to form the filename (there must be a better 
    // way to do this)
    def fileName = line.split(/ {1,}/)[8..-1].join("")

    // now we get the first part of the cksum
    def cksum = "cksum $dir/$fileName".execute().in.text.split(/ {1,}/)[0]

    // concat the result to the original line and print it
    println "$line $cksum"
  }
}

特别注意我的“必须有一个更好的方式来做到这一点。”



文章来源: How to collect directory listing along with each file CRC checksum?