package ums.zxcomc.pm.maf.cbwa.pmpcommon.comm.pmcollect;
import java.io.*;
import ums.uep.api.util.DebugPrn;
import ums.zxcomc.pm.maf.cbwa.pmpcommon.comm.PmpMafConst;
/**
* <p>文件名称: CbwaFileCopyManager.java</p>
* <p>文件描述: 文件和文件夹的拷贝类,支持多层子目录嵌套</p>
* <p>版权所有: </p>
* <p>公 司: </p>
* <p>内容摘要: </p>
* <p>其他说明: </p>
* <p>完成日期:2010-6-4</p>
* <p>修改记录1: </p>
* <pre>
* 修改日期:
* 版 本 号:
* 修 改 人:
* 修改内容:
* </pre>
* <p>修改记录2:…</p>
* @version 1.0
* @author
*/
public class CbwaFileCopyManager {
private static DebugPrn debugPrn = new DebugPrn(CbwaPmCollectInfoExport.class.getName());
/**
* 拷贝文件或者文件夹
* @param srcFileName 源文件(夹)的限定名
* @param destFileName 目标文件(夹)的限定名
* @param overlay 是否允许覆盖文件
* @return boolean 拷贝是否成功
*/
public boolean copyFileOrFolder(String srcFileName, String destFileName, boolean overlay){
File srcFile = new File(srcFileName);
// 判断源文件(夹)是否存在
if (!srcFile.exists()) {
debugPrn.info("The source file/folder doesn't exist: " + srcFileName);
return false;
}
if(srcFile.isFile())
{
//拷贝文件
boolean isOK = copyFile(srcFileName, destFileName, overlay);
if(!isOK)
{
debugPrn.warn("Fail to copy file: " + srcFileName);
return false;
}
}
else if(srcFile.isDirectory())
{
//拷贝文件夹
boolean isOK = copyFolder(srcFileName, destFileName, overlay);
if(!isOK)
{
debugPrn.warn("Fail to copy folder: " + srcFileName);
return false;
}
}
else
{
debugPrn.info("The source file/folder isn't valid : " + srcFileName);
return false;
}
return true;
}
/**
* 拷贝文件
* @param srcFileName 源文件的限定名
* @param destFileName 目标文件的限定名
* @param overlay 是否允许覆盖文件
* @return boolean 拷贝是否成功
*/
private boolean copyFile(String srcFileName, String destFileName, boolean overlay){
// 判断目标文件是否存在
File destFile = new File(destFileName);
if (destFile.exists()) {
//如果目标文件存在
if (overlay) {
// 允许覆盖文件:则先删除目标文件
if(!destFile.delete())
{
//如果删除失败
debugPrn.warn("Fail to delete the File:" + destFileName);
return false;
}
}
else
{
//不允许覆盖,直接返回,不视为拷贝失败
debugPrn.info("Not allowed to overwrite the file: " + destFileName);
return true;
}
} else {
// 如果目标文件所在目录不存在,则先创建目录
if (!destFile.getParentFile().exists()) {
// 目标文件所在目录不存在
if (!destFile.getParentFile().mkdirs()) {
// 创建目标文件所在目录失败
debugPrn.warn("Fail to mkdirs by the path: " + destFileName);
return false;
}
}
}
//开始复制文件
File srcFile = new File(srcFileName);
int byteread = 0; //读取的字节数
InputStream in = null;
OutputStream out = null;
BufferedOutputStream bufferedOut = null;
BufferedInputStream bufferedIn = null;
try {
in = new FileInputStream(srcFile);
out = new FileOutputStream(destFile);
bufferedIn = new BufferedInputStream(in);
bufferedOut= new BufferedOutputStream(out);
byte[] buffer = new byte[1024];
while ((byteread = bufferedIn.read(buffer)) != -1) {
bufferedOut.write(buffer, 0, byteread);
}
bufferedOut.flush();
return true;
} catch (FileNotFoundException e) {
debugPrn.warn("FileNotFoundException in copyFile: ", e);
return false;
} catch (IOException e) {
debugPrn.warn("IOException in copyFile: ", e);
return false;
} finally{
if (bufferedOut != null)
{
try
{
bufferedOut.close();
}
catch(IOException e)
{
bufferedOut = null;
debugPrn.warn("IOException in copyFile: ", e);
}
}
if (bufferedIn != null)
{
try
{
bufferedIn.close();
}
catch(IOException e)
{
bufferedIn = null;
debugPrn.warn("IOException in copyFile: ", e);
}
}
}
}
/**
* 拷贝文件夹,支持多层嵌套的文件夹拷贝
* @param srcFolderName 源文件夹的限定名
* @param destFolderName 目标文件夹的限定名
* @param overlay 是否允许覆盖文件
* @return boolean 拷贝是否成功
*/
private boolean copyFolder(String srcFolderName, String destFolderName, boolean overlay){
File srcFolder = new File(srcFolderName);
File destFolder = new File(destFolderName);
if(!destFolder.exists())
{
if(!destFolder.mkdirs())
{
debugPrn.warn("Fail to mkdirs: " + destFolderName);
return false;
}
}
String[] subFileFolderList = srcFolder.list();
for(int i = 0; i < subFileFolderList.length; i++)
{
String srcSubFileName = srcFolderName + PmpMafConst.SEPARATOR +subFileFolderList[i]; //子文件(夹)的源路径
String deskSubFileName = destFolderName + PmpMafConst.SEPARATOR + subFileFolderList[i];
boolean isOK = copyFileOrFolder(srcSubFileName, deskSubFileName, overlay);
if(!isOK)
{
debugPrn.warn("Fail to copy: " + srcSubFileName);
return false;
}
}
return true;
}
}