zoukankan      html  css  js  c++  java
  • C# 备份、还原、拷贝远程文件夹

    最近一直都很忙,非常抱歉好久没有写过博客了。最近遇到拷贝远程文件的一些工作,比如我们发布的web站点的时候,开发提供一个zip压缩包,我们需要上传到远程的服务器A,然后在部署(文件拷贝)到远程环境B和C,ABC都在一个局域网里面。文件压缩需要引用 System.IO.Compression和System.IO.Compression.FileSystem

    首先我们需要一个工具类来转换文件路径,本地地址与远程地址的转换 比如192.168.0.1上的D: est 转换 为\192.168.0.1D$ est,文件路径的拼接,

     public class PathUtil
        {
            public static string GetRemotePath(string ip, string localPath)
            {
                if (!localPath.Contains(":"))
                {
                    throw new Exception($"{localPath}路径必须是全路径且是本地路径");
                }
                localPath = localPath.Replace(":", "$");
                return $@"\{ip}{localPath}";
            }
    
            public static string GetLocaPath(string remotePath)
            {
                int index = remotePath.IndexOf("$");
                if (index < 1)
                {
                    throw new Exception($"{remotePath}路径必须包含磁盘信息");
                }
    
                string temp = remotePath.Substring(index - 1);
                temp = temp.Replace("$", ":");
                return temp;
            }
    
            public static string Combine(string path1, string path2)
            {
                path1 = path1.Trim();
                path2 = path2.Trim();
    
                if (path1.EndsWith("\") && path2.StartsWith("\"))
                {
                    string ret = (path1 + path2).Replace("\", "");
                    return ret;
                }
                else if (!path1.EndsWith("\") && !path2.StartsWith("\"))
                {
                    return path1 + "\" + path2;
                }
                // if ((path1.EndsWith("\") && !path2.StartsWith("\")) ||
                //(!path1.EndsWith("\") && path2.StartsWith("\"))) { }
                return path1 + path2;
            }
        }

    备份远程目录的文件夹 (首先备份远程A目录到本地临时文件zip->拷贝到远程B->删除本地临时文件zip

    还原远程文件(部署发布包)(远程文件解压到本地临时目录->拷贝到目标服务器->删除本地临时目录

    文件夹得拷贝就比较简单,递归调用文件复制就okay了,比如 \192.168.0.1D$ est 拷贝到  \192.168.0.2D$ est下 (建议先删除存在文件在拷贝)

    相关code如下:

      #region 文件操作部分
            /// <summary>
            /// 把一个目录下的文件拷贝的目标目录下
            /// </summary>
            /// <param name="sourceFolder">源目录</param>
            /// <param name="targerFolder">目标目录</param>
            /// <param name="removePrefix">移除文件名部分路径</param>
            public void CopyFiles(string sourceFolder, string targerFolder, string removePrefix = "")
            {
                if (string.IsNullOrEmpty(removePrefix))
                {
                    removePrefix = sourceFolder;
                }
                if (!Directory.Exists(targerFolder))
                {
                    Directory.CreateDirectory(targerFolder);
                }
                DirectoryInfo directory = new DirectoryInfo(sourceFolder);
                //获取目录下的文件
                FileInfo[] files = directory.GetFiles();
                foreach (FileInfo item in files)
                {
                    if (item.Name == "Thumbs.db")
                    {
                        continue;
                    }
                    string tempPath = item.FullName.Replace(removePrefix, string.Empty);
                    tempPath = targerFolder + tempPath;
                    FileInfo fileInfo = new FileInfo(tempPath);
                    if (!fileInfo.Directory.Exists)
                    {
                        fileInfo.Directory.Create();
                    }
                    File.Delete(tempPath);
                    item.CopyTo(tempPath, true);
                }
                //获取目录下的子目录
                DirectoryInfo[] directors = directory.GetDirectories();
                foreach (var item in directors)
                {
                    CopyFiles(item.FullName, targerFolder, removePrefix);
                }
            }
    
            /// <summary>
            /// 备份远程文件夹为zip文件
            /// </summary>
            /// <param name="sourceFolder">备份目录</param>
            /// <param name="targertFile">目标文件</param>
            /// <returns></returns>
            public bool BackZip(string sourceFolder, string targertFile)
            {
                string tempfileName = PathUtil.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".zip");
                bool ret = false;
                try
                {
                    ZipFile.CreateFromDirectory(sourceFolder, tempfileName, CompressionLevel.Optimal, false);
                    var parentDirect = (new FileInfo(targertFile)).Directory;
                    if (!parentDirect.Exists)
                    {
                        parentDirect.Create();
                    }
                    File.Copy(tempfileName, targertFile, true);
                    ret = true;
                }
                catch (Exception ex)
                {
                    throw new Exception($"备份目录{sourceFolder}出错{ex.Message}");
                }
                finally
                {
                    if (File.Exists(tempfileName))
                    {
                        File.Delete(tempfileName);
                    }
                }
                return ret;
            }
    
            /// <summary>
            /// 把远程文件还原为远程目录
            /// </summary>
            /// <param name="sourceFile">远程文件</param>
            /// <param name="targetFolder">远程目录</param>
            /// <returns></returns>
            public bool RestoreZip(string sourceFile, string targetFolder)
            {
                string tempFolderName = PathUtil.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                bool ret = false;
    
                try
                {
                    using (ZipArchive readZip = ZipFile.OpenRead(sourceFile))
                    {
                        readZip.ExtractToDirectory(tempFolderName);
                    }
                    string copyFolder = tempFolderName;
                    //if (!Directory.GetFiles(copyFolder).Any() && Directory.GetDirectories(copyFolder).Length == 1)
                    //{
                    //    copyFolder = Directory.GetDirectories(copyFolder)[0];
                    //}
                    CopyFiles(copyFolder, targetFolder, copyFolder);
                    ret = true;
                }
                catch (Exception ex)
                {
                    throw new Exception($"发布文件{sourceFile}到{targetFolder}出错{ex.Message}");
                }
                finally
                {
                    if (Directory.Exists(tempFolderName))
                    {
                        Directory.Delete(tempFolderName, true);
                    }
                }
                return ret;
            }
            #endregion
  • 相关阅读:
    MyBatis的入门案例
    MySQL数据库解决乱码 latin1 转 gbk
    13.MD5对用户密码进行加密
    Windows下永久解决数据库乱码 utf8 转 gbk
    SpringMVC 异常处理
    SpringMVC 实现返回一段数据 & 实现自动发送json格式数据
    SpringMVC中session的使用
    SpringMVC中的重定向和转发的实现
    SpringMVC 获取请求参数
    vue-router介绍
  • 原文地址:https://www.cnblogs.com/majiang/p/7533648.html
Copyright © 2011-2022 走看看