我使用以下命令在nix(Linux、AIX、Sunos、HPUX)平台中获取目录列表
命令
ls -latr
Ouput
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校验和。
在这些nix(Linux、AIX、Sunos、HPUX)平台中,如何将CRC校验和附加在每个文件后面(也包括目录列表),如下图所示,并保持以下格式?
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/Govy将输出最终解析为给定的格式,该格式使用Groovy XmlSlurper形成xml(xml的生成大小约为5MB)
"permission","hardlink","owner","group","fsize","month","date","time","filename","checksum"
欢迎所有建议!:)
使用我的代码更新
但在这里,我计算的是md5hex,它给出的输出与linux中的md5sum
命令类似。所以它不再是cksum
,因为我不能使用某些许可问题的累积奖金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
filecheckrelease-1.0.zip, 3cc0f2b13778129c0cc41fb2fdc7a85f, 18/07/2012, 11786307b, rwx, 18/07/2012, 04:13:47 PM
fileDedicated.mp3, 238f793f0b80e7eacf5fac31d23c65d4, 04/05/2010, 4650908b, rwx, 04/05/2010, 10:45:32 AM
但我仍然需要一种方法来计算硬链接,所有者&组我在网上搜索了一下,它看起来像是java7具有这种能力&我被java6卡住了。有什么帮助吗?
看看http://www.jonelo.de/java/jacksum/index.html-据报道,它提供了与cksum兼容的CRC32校验和。
顺便说一句,我尝试使用java.util.zip.RC32来计算校验和,它给出的值与cksum不同,所以必须使用稍微不同的算法。
编辑:我尝试了jacksum,它有效,但你必须告诉它使用"cksum"算法——显然与crc32不同,crc32也支持jacksum。
好吧,您可以运行该命令,然后,对于每一行,运行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"
}
}
特别注意我的"必须有更好的方法来做到这一点"。