如何收集目录列表以及每个文件的CRC校验和? [英] How to collect directory listing along with each file CRC checksum?
问题描述
我使用以下命令获取nix(Linux,AIX,Sunos,HPUX)平台上的dir列表
$ b 命令
ls -latr
输出
drwxr-xr-x 2 ricky支持4096 8月29 11:59 lib
-rwxrwxrwx 1 ricky支持924 8月29日12: 00 initservice.sh
cksum
命令被使用CRC校验和如何在每个文件之后添加如下所示的内容(包括目录列表),在这些nix中维护以下格式(Linux, AIX,Sunos,HPUX)平台?
drwxr-xr-x 2 ricky支持4096 Aug 29 11:59 lib
-rwxrwxrwx 1 ricky支持924 8月29日12:00 initservice.sh 4287252281
更新备注:没有第三方应用程序,我使用java / Groovy将输出最终解析为给定格式,并使用groovy形成xml XmlSlurper(XML生成的大小约为5MB)
权限,硬链接,所有者,组, fsize,month,date,time,filename,checksum
欢迎所有的建议! :)
用我的代码更新
但是我在计算md5hex从linux发出的命令类似于 md5sum
命令。所以它不再是 cksum
,因为我不能使用某些授权问题的jacksum bcz:(b / b
$ b pre $ 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
格式formatter = null
def list = []
if(dir.exists()){
dir.eachFileRecurse(FileType.FILES){fname - >
list<< fname
$ {
md5sum = getMD5CheckSum(fileob.toString())
filesize = fileob.length()+b
lastmodified = new Date(fileob.lastModified())
lastmodifiedDate = lastmodified.format('dd / MM / yyyy')
formatter = new Simp leDateFormat(hh:mm:ss a)
lastmodifiedTime = formatter.format(lastmodified)
permission = getReadPermissions(fileob)+ getWritePermissions(fileob)+ getExecutePermissions(fileob)
filename = getRelativePath (E:\\\ \\\\\\\\\\,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
}
}
}
}
$ b $ public def getReadPermissions(def file){
String temp = -
if(file.canRead())temp =r
return temp
}
(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上午
但我仍然需要一种计算硬链接,所有者和放大器的方法。组。我在网上搜索它看起来像 java7 有这种能力&我坚持使用java6。任何帮助?
请看 http://www.jonelo.de/java/jacksum/index.html - 据报道它提供与cksum兼容的CRC32校验和。
顺便说一句,我尝试使用java.util.zip.CRC32来计算校验和,它给出了与cksum不同的值,所以必须使用稍微不同的算法。 / p>
编辑:我试过jacksum,它可以工作,但是你必须告诉它使用'cksum'算法 - 显然 不同于crc32,其中jacksum也支持。
I use the following command to get dir listing in nix(Linux, AIX, Sunos, HPUX) platforms
Command
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
command is used for getting CRC checksum.
How can the CRC Checksum be appended after each file something (including directory listing too) like below, maintaining the below format in these nix(Linux, AIX, Sunos, HPUX) platforms?
drwxr-xr-x 2 ricky support 4096 Aug 29 11:59 lib
-rwxrwxrwx 1 ricky support 924 Aug 29 12:00 initservice.sh 4287252281
Update Note : No third party application, I am using java/Groovy to parse the output ultimately into a given format which forms a xml using groovy XmlSlurper (XML's get generated around 5MB sized)
"permission","hardlink","owner","group","fsize","month","date","time","filename","checksum"
All Suggestions are welcome! :)
Update with my code
But here I am calculating md5hex which gives a similar output as md5sum
command from linux. So it's no longer cksum
as I cannot use jacksum bcz of some licensing issue :(
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")
}
}
Output
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
but still I need a way to calculate hardlink, owner & group. I searched on the net it looks like java7 has this capability & I am stuck with java6. Any help?
Take a look at http://www.jonelo.de/java/jacksum/index.html - it is reported to provide cksum - compatible CRC32 checksums.
BTW, I tried using java.util.zip.CRC32 to calculate checksums, and it gives a different value than cksum does, so must use a slightly different algorithm.
EDIT: I tried jacksum, and it works, but you have to tell it to use the 'cksum' algorithm - apparently that is different from crc32, which jacksum also supports.
这篇关于如何收集目录列表以及每个文件的CRC校验和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!