Java 复制zip文件到指定目录

工具类:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileHelpUtils {

    /** The Constant LOG. */
    private static final Logger LOG = LoggerFactory.getLogger(FileHelpUtils.class);
    // 拷贝文件到指定目录
    public static boolean backupFile(final File srcFile, String copyPath )
    {

        if (srcFile == null)
        {
            LOG.error("backSuccess process, source is null!");
            return false;
        }

        File typeFolder = new File(copyPath);
        if (typeFolder.exists())
        {
            LOG.info("      check  folder exists! (" + copyPath + ")");
        }else{
            LOG.info("      create folder (" + copyPath + ")...");
            typeFolder.mkdir();
            LOG.info("      create folder (" + copyPath + ") end, check exists:" + typeFolder.exists());
        }

        //Folder: date
        String version = new SimpleDateFormat("yyyyMMdd").format(new Date());
        final String strDestDirectory = copyPath + File.separator +version;
        final File destFolder = new File(strDestDirectory);
        if (destFolder.exists())
        {
            LOG.info("      check  folder exists! (" + strDestDirectory + ") ");
        }else{
            LOG.info("      create folder (" + strDestDirectory + ")...");
            destFolder.mkdir();
            LOG.info("      create folder (" + strDestDirectory + ") end, check exists:" + destFolder.exists());
        }

        //file name
        final String fileName = srcFile.getName();
        //{destDirectory}/{fileName}
        final String destFilePath = strDestDirectory + File.separator + fileName;

        //    Move file
        final File destFile = new File(destFilePath);
        if (destFile.exists())
        {
            LOG.info(" Delete old file : " + destFilePath + "!");
            destFile.delete();
        }

        // NEW strategy to avoid move file failure
        return reNameFile(srcFile, destFile);
    }

    public static boolean reNameFile(File oldname, File newname){
        try{
            // copy file
            org.apache.commons.io.FileUtils.copyFile(oldname, newname);
            LOG.info(" [OK] Copy file from " + oldname.getPath() + " ----> " + newname + " !");
            // delete file
            if(oldname.delete()){
                LOG.info(" [OK] Delete source file:" + oldname.getPath() + " !");
            }
        }catch(IOException ioex){
            LOG.error(" [KO] Copy file path: " + oldname.getPath() + ". error:" + ioex.getMessage());
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        backupFile(new File("E:\\rollout_1608284713998.json.gz"),"E:\\home");
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值