• 文件操作辅助类


    项目工程中使用的,可以保证可用性以及准确性。。。。。 处了文件压缩解压 没用过其他组件 文件压缩解压缩使用Ionic.Zip.dll自行下载就好

    public interface IFileOperation
        {
            /// <summary>
            ///  删除文件
            /// </summary>
            /// <param name="filePath">文件路径</param>
            /// <returns></returns>
            bool DelFile(string filePath);
    
            /// <summary>
            /// 删除文件目录
            /// </summary>
            /// <param name="dirPath">目录地址</param>
            /// <returns></returns>
            bool DelDirectory(string dirPath);
    
            /// <summary>
            /// 创建文件
            /// </summary>
            /// <param name="filePath">文件地址</param>
            /// <returns></returns>
            bool CreateFile(string filePath);
    
            /// <summary>
            /// 创建文件目录
            /// </summary>
            /// <param name="dirPath">目录地址</param>
            /// <returns></returns>
            bool CreateDirectory(string dirPath);
    
            /// <summary>
            /// 复制文件
            /// </summary>
            /// <param name="filePath">原文件地址</param>
            /// <param name="targetFilePath">目标文件地址</param>
            /// <param name="overwrite">是否允许覆盖相同名称文件</param>
            /// <returns></returns>
            bool CopyFile(string filePath, string targetFilePath, bool overwrite);
    
            /// <summary>
            /// 复制文件夹
            /// </summary>
            /// <param name="dirPath">待复制文件目录</param>
            /// <param name="targetDirPath">目标文件目录</param>
            /// <param name="DirFirst">是否保留文件第一级目录</param>
            /// <returns></returns>
            bool CopyDirectroy(string dirPath, string targetDirPath, bool dirFirst);
    
            /// <summary>
            /// 移动文件
            /// </summary>
            /// <param name="filePath">原文件地址</param>
            /// <param name="targetFilePath">目标文件地址</param>
            /// <returns></returns>
            bool MoveFile(string filePath, string targetFilePath);
    
            /// <summary>
            /// 移动制文件目录
            /// </summary>
            /// <param name="dirPath">原目录</param>
            /// <param name="targetDirPath">目标目录</param>
            /// <returns></returns>
            bool MoveDirectroy(string dirPath, string targetDirPath);
    
            /// <summary>
            /// 文件压缩
            /// </summary>
            /// <param name="strFileToCompress">待压缩文件</param>
            /// <param name="strCompressZipPath">压缩到目标文件</param>
            bool ExeCompression(string strFileToCompress, string strCompressZipPath);
         
            /// <summary>
            /// 解压缩文件
            /// </summary>
            /// <param name="strFileToCompress">待解压缩文件</param>
            /// <param name="strCompressZipPath">解压缩到目标文件</param>
            bool ExeDecompression(string strDecompressionZipPath, string strDecompressionPath);
    
            /// <summary>
            /// 下载文件
            /// </summary>
            /// <param name="strUrl">下载文件的地址</param>
            /// <param name="strPath">下载后保存的地址</param>
            void DownloadFile(string strUrl, string strPath);
        }

    上面是接口下面是实现类

     public class FileOperationImpl : IFileOperation
        {
            private string module = "FileManager.cs";
            private IWriteLog logWrite = new WriteLog();
    
            /// <summary>
            /// 删除文件
            /// </summary>
            /// <param name="filePath">待删除文件目录</param>
            /// <returns></returns>
            public bool DelFile(string filePath)
            {
                bool result = false;
                try
                {
                    // 检查文件是否存在
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    logWrite.Log(LogConstant.LogType.Exception, module,
                          "Error on DelFile modules: {0}.", ex.Message);
                }
                return result;
            }
    
            /// <summary>
            /// 删除目录
            /// </summary>
            /// <param name="dirPath">待删除目录</param>
            /// <returns></returns>
            public bool DelDirectory(string dirPath)
            {
                bool result = false;
                try
                {
                    // 检查目录是否存在
                    if (Directory.Exists(dirPath))
                    {
                        Directory.Delete(dirPath, true);
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    logWrite.Log(LogConstant.LogType.Exception, module,
                          "Error on DelDirectory modules: {0}.", ex.Message);
                }
                return result;
            }
    
            /// <summary>
            /// 创建文件
            /// </summary>
            /// <param name="filePath">待创建文件</param>
            /// <returns></returns>
            public bool CreateFile(string filePath)
            {
                bool result = false;
                try
                {
                    File.Create(filePath);
                    result = true;
                }
                catch (Exception ex)
                {
                    logWrite.Log(LogConstant.LogType.Exception, module,
                          "Error on CreateFile modules: {0}.", ex.Message);
                }
                return result;
            }
    
            /// <summary>
            /// 创建目录
            /// </summary>
            /// <param name="dirPath">待创建目录</param>
            /// <returns></returns>
            public bool CreateDirectory(string dirPath)
            {
                bool result = false;
                try
                {
                    if (!Directory.Exists(dirPath))
                    {
                        Directory.CreateDirectory(dirPath);
                    }
                    result = true;
                }
                catch (Exception ex)
                {
                    logWrite.Log(LogConstant.LogType.Exception, module,
                          "Error on CreateDirectory: {0}.", ex.Message);
                }
                return result;
            }
    
            /// <summary>
            /// 文件复制
            /// </summary>
            /// <param name="filePath">待复制文件</param>
            /// <param name="targetFilePath">复制到目标地址</param>
            /// <returns></returns>
            public bool CopyFile(string filePath, string targetFilePath, bool overwrite)
            {
                bool result = false;
                try
                {
                    if (File.Exists(filePath))
                    {
                        // 复制并允许覆盖
                        File.Copy(filePath, targetFilePath, overwrite);
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    logWrite.Log(LogConstant.LogType.Exception, module,
                          "Error on CopyFile: {0}.", ex.Message);
                }
                return result;
            }
    
            /// <summary>
            /// Get Directory Name
            /// </summary>
            /// <returns>int</returns>
            public int DirectoryName(string directoryPath)
            {
                // 获取文件夹名,截取“” 
                int j = 0;
                j = directoryPath.LastIndexOf("\");
                return j + 1;
            }
    
            /// <summary>
            /// 复制文件夹
            /// </summary>
            /// <param name="dirPath">待复制文件目录</param>
            /// <param name="targetDirPath">目标文件目录</param>
            /// <param name="dirFirst">是否保留文件第一级目录</param>
            /// <returns></returns>
            public bool CopyDirectroy(string dirPath, string targetDirPath, bool dirFirst)
            {
                bool result = false;
                try
                {
                    // 获取文件夹名
                    string s = dirPath.Substring(DirectoryName(dirPath));
    
                    DirectoryInfo directoryArray = new DirectoryInfo(dirPath);
    
                    // 获取该文件夹下的文件列表     
                    FileInfo[] files = directoryArray.GetFiles();
    
                    // 获取该文件夹下的文件夹列表 
                    DirectoryInfo[] directorys = directoryArray.GetDirectories();
    
                    if (!dirFirst)
                    {
                        if (Directory.Exists(targetDirPath))
                        {
                            // 若文件夹存在,不管目录是否为空,删除 
                            Directory.Delete(targetDirPath, true);
    
                            // 删除后,重新创建文件夹    
                            Directory.CreateDirectory(targetDirPath);
                        }
                        else
                        {
                            // 文件夹不存在,创建     
                            Directory.CreateDirectory(targetDirPath);
                        }
    
                        // 逐个复制文件     
                        foreach (FileInfo inf in files)
                        {
                            File.Copy(dirPath + "\" + inf.Name, targetDirPath + "\" + inf.Name);
                        }
    
                        // 逐个获取文件夹名称,并递归调用方法本身     
                        foreach (DirectoryInfo dir in directorys)
                        {
                            CopyDirectroy(dirPath + "\" + dir.Name, targetDirPath, true);
                        }
                    }
                    else
                    {
                        if (Directory.Exists(targetDirPath + "\" + s))
                        {
                            // 若文件夹存在,不管目录是否为空,删除 
                            Directory.Delete(targetDirPath + "\" + s, true);
    
                            // 删除后,重新创建文件夹 
                            Directory.CreateDirectory(targetDirPath + "\" + s);
                        }
                        else
                        {
                            // 文件夹不存在,创建     
                            Directory.CreateDirectory(targetDirPath + "\" + s);
                        }
    
                        // 逐个复制文件     
                        foreach (FileInfo inf in files)
                        {
                            File.Copy(dirPath + "\" + inf.Name, targetDirPath + "\" + s + "\" + inf.Name);
                        }
    
                        // 逐个获取文件夹名称,并递归调用方法本身     
                        foreach (DirectoryInfo dir in directorys)
                        {
                            CopyDirectroy(dirPath + "\" + dir.Name, targetDirPath + "\" + s, true);
                        }
                    }
                }
                catch (Exception ex)
                {
                    logWrite.Log(LogConstant.LogType.Exception, module,
                          "Error on CopyDirectroy: {0}.", ex.Message);
                }
    
                return result;
            }
    
            /// <summary>
            /// 文件移动
            /// </summary>
            /// <param name="filePath">待移动文件</param>
            /// <param name="targetFilePath">移动到目标地址</param>
            /// <returns></returns>
            public bool MoveFile(string filePath, string targetFilePath)
            {
                bool result = false;
                try
                {
                    if (File.Exists(filePath))
                    {
                        File.Move(filePath, targetFilePath);
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    logWrite.Log(LogConstant.LogType.Exception, module,
                          "Error on MoveFile: {0}.", ex.Message);
                }
                return result;
            }
    
            /// <summary>
            /// 目录移动
            /// </summary>
            /// <param name="dirPath">待移动目录</param>
            /// <param name="targetDirPath">移动到目标地址</param>
            /// <returns></returns>
            public bool MoveDirectroy(string dirPath, string targetDirPath)
            {
                bool result = false;
                try
                {
                    if (Directory.Exists(dirPath))
                    {
                        Directory.Move(dirPath, targetDirPath);
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    logWrite.Log(LogConstant.LogType.Exception, module,
                          "Error on MoveDirectroy: {0}.", ex.Message);
                }
                return result;
            }
    
            /// <summary>
            /// 文件压缩
            /// </summary>
            /// <param name="strFileToCompress">待压缩文件</param>
            /// <param name="strCompressZipPath">压缩到目标文件</param>
            public bool ExeCompression(string strFileToCompress, string strCompressZipPath)
            {
                bool result = false;
                try
                {
                    using (ZipFile zipOperation = new ZipFile(strCompressZipPath, System.Text.Encoding.Default))
                    {
                        if (File.Exists(strFileToCompress))
                            zipOperation.AddFile(strFileToCompress, string.Empty);
                        else
                            zipOperation.AddDirectory(strFileToCompress);
                        zipOperation.Save();
                        zipOperation.Dispose();
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    logWrite.Log(LogConstant.LogType.Exception, module,
                          "Error on ExeCompression: {0}.", ex.Message);
                }
    
                return result;
            }
    
            /// <summary>
            /// 解压缩文件
            /// </summary>
            /// <param name="strFileToCompress">待解压缩文件</param>
            /// <param name="strCompressZipPath">解压缩到目标文件</param>
            public bool ExeDecompression(string strDecompressionZipPath, string strDecompressionPath)
            {
                bool result = false;
                try
                {
                    using (ZipFile zipOperation = new ZipFile(strDecompressionZipPath, System.Text.Encoding.Default))
                    {
                        zipOperation.ExtractAll(strDecompressionPath);
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    logWrite.Log(LogConstant.LogType.Exception, module,
                          "Error on ExeDecompression: {0}.", ex.Message);
                }
                return result;
            }
    
            /// <summary>
            /// Download File
            /// </summary>
            /// <param name="strUrl">strUrl</param>
            /// <param name="strPath">strPath 例如C:/Document/</param>
            public void DownloadFile(string strUrl, string strPath)
            {
                try
                {
                    string SEPARATOR = "\";
    
                    string pathFlag = "/";
    
                    int index = strUrl.LastIndexOf("/");
    
                    String fileName = strUrl.Substring(index + 1);
    
                    if (strPath.EndsWith(SEPARATOR) || strPath.EndsWith(pathFlag))
                    {
                        strPath = strPath + fileName;
                    }
                    else
                    {
                        strPath = strPath + SEPARATOR + fileName;
                    }
    
                    FileStream fs = new FileStream(strPath, FileMode.OpenOrCreate);
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream stream = response.GetResponseStream();
    
                    int bufSize = 1048576; // 1Mb
    
                    byte[] buffer = new byte[bufSize];
    
                    int bytesRead = 0;
    
                    while ((bytesRead = stream.Read(buffer, 0, bufSize)) > 0)
                    {
                        fs.Write(buffer, 0, bytesRead);
                    }
    
                    stream.Close();
                    fs.Close();
                    response.Close();
    
                }
                catch (WebException WebExcp)
                {
                    logWrite.Log(LogConstant.LogType.Exception, module,
                             "DownloadFile. Message: {0}", WebExcp.Message);
                    throw WebExcp;
                }
            }
        }
  • 相关阅读:
    0723作业
    Kafka概述与设计原理
    Spring 装配Bean入门级
    logback节点配置详解
    Kafka问题排查(消费者自动关闭)
    .NET委托解析
    .NET可变性解析(协变和逆变)
    .NET泛型解析(下)
    .NET泛型解析(上)
    .NET动态调用WebService
  • 原文地址:https://www.cnblogs.com/HelloXZ/p/3803972.html
Copyright © 2020-2023  润新知