62,634
社区成员




<?xml version="1.0" encoding="iso-8859-1"?>
<Dog>
<Cat>
...
无数行
</Cat>
</Dog>
<?xml version="1.0" encoding="iso-8859-1"?><Dog>
<Cat>
...
无数行
</Cat>
</Dog>
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class DirSyncCompare {
/**
* @param srcPath
* @param destPath
* @return "ok": 表示完全一样;如果没有',':文件类型不匹配;有',',不同的文件列表
*/
public static final String dirCompare(String srcPath,String destPath){
File srcFile = new File(srcPath);
File tagFile = new File(destPath);
String result = comparate(srcFile, tagFile);
if("".equals(result))
return "ok";
else if(result.lastIndexOf("\r\n")>-1)
return result.substring(0,result.lastIndexOf("\r\n"));
else
return result;
}
/**
* 比较两个文件是否相等,并包含文件目录夹下子目录下面的文件
* @param srcFile 源文件地址
* @param tagFile 目标文件地址
* @return 一样返回为""
*/
public static final String comparate(File srcFile,File tagFile)
{
boolean equals =
(srcFile.isDirectory() && tagFile.isDirectory()) || ( srcFile.isFile() && tagFile.isFile());
if(!equals){
return srcFile.getAbsolutePath() + "与" + tagFile.getAbsolutePath() + "类型不一致\r\n";
}
if(srcFile.isFile()){
try {
equals = equals(srcFile,tagFile);
if(equals){
return "";
}
else return srcFile.getName()+"\r\n";
}
catch (IOException e) {
return "比较文件" + srcFile.getAbsolutePath() + "与" + tagFile.getAbsolutePath() + "IO ERROR\r\n";
}
}
else {
File[] srcFiles = srcFile.listFiles();
File[] tagFiles = tagFile.listFiles();
Map<String,File> maps = new HashMap<String,File>();
StringBuffer buffer = new StringBuffer();
for(int i = 0 ; tagFiles != null && i < tagFiles.length; i++){
maps.put(tagFiles[i].getName().toLowerCase(),tagFiles[i]);
}
for(int i = 0 ; srcFiles != null && i < srcFiles.length; i++){
String key = srcFiles[i].getName().toLowerCase();
File srcChildFile = srcFiles[i];
File tagChildFile = maps.get(key);
/*String result = comparate(srcChildFile,tagChildFile);
if(tagChildFile != null&&result!=null)
buffer.append(comparate(srcChildFile,tagChildFile));*/
if(tagChildFile != null)
buffer.append(comparate(srcChildFile,tagChildFile));
else {
buffer.append(srcChildFile.getName() + " miss\r\n!");
}
}
/*String result = buffer.toString().equals("")? "ok":buffer.toString();
if(result.lastIndexOf(",")>-1)
result = result.substring(0,result.lastIndexOf(","));
return result;*/
return buffer.toString();
}
}
public static final boolean equals(File file1,File file2) throws IOException{
long size1 = file1.length();
long size2 = file2.length();
//如果文件大小不一致,不相等退出
if(size1 != size2)
return false;
//如果文件名不一致,表示不相等
if(!file1.getName().equals(file2.getName())){
return false;
}
if(size1 == 0) return true;
BufferedInputStream buf1 = null;
BufferedInputStream buf2 = null;
try
{
buf1 = new BufferedInputStream(new FileInputStream(file1));
buf2 = new BufferedInputStream(new FileInputStream(file2));
long bufferSize = size1 > 4096 ? 4096 : size1;
byte[] bytes1 = new byte[(int) bufferSize];
byte[] bytes2 = new byte[(int) bufferSize];
int end = 0;
while((end = buf1.read(bytes1)) != -1 && (end = buf2.read(bytes2)) != -1){
for(int i=0; i<end; i++){
if(bytes1[i] != bytes2[i])
{
return false;
}
}
}
return true;
}
finally{
if(buf1 != null) {
try{
buf1.close();
}
catch(IOException ex){}
}
if(buf2 != null){
try{
buf2.close();
}
catch(IOException ex){
buf2.close();
}
}
}
}
}