C# 利用Rar压缩文件并FTP上传

1.利用Rar压缩文件

        /// 
        /// 使用Rar压缩文件
        /// 
        /// 待压缩文件路径
        /// 压缩后的文件路径
        /// 返回压缩结果
        public string RarFile(string fromFilePath, string rarFilePath)
        {
            try
            {
                //Rar程序安装目录
                var fileName = @"C:\Program Files\WinRAR\rar.exe";

                //命令行参数  
                var arguments = " a " + rarFilePath + " " + fromFilePath;
                var startInfo = new ProcessStartInfo();
                startInfo.FileName = fileName;
                startInfo.Arguments = arguments;
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;

                //打包文件存放目录  
                var dir = Path.GetDirectoryName(fromFilePath);
                if (dir != null)
                {
                    startInfo.WorkingDirectory = dir;
                    var process = new Process { StartInfo = startInfo };
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.CreateNoWindow = true;

                    process.Start();
                    process.WaitForExit();
                    var output = process.StandardOutput.ReadToEnd();
                    var errMsg = process.StandardError.ReadToEnd();
                    process.Close();

                    //rar可能是中文版或英文版
                    if (output.IndexOf("完成", StringComparison.Ordinal) > -1 || output.ToLower().IndexOf("done", StringComparison.Ordinal) > -1)
                    {
                        return "完成";
                    }
                }
                return "文件所在目录不存在";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
调用方法:

            //将文件压缩到同级目录
            var fromFile = "";
            var dir = Path.GetDirectoryName(fromFile);
            var name = Path.GetFileNameWithoutExtension(fromFile);
            var tofile = dir.Combine(name + ".zip");
            var result = RarFile(fromFile, tofile);


2.通过FTP上传文件

       /// 
        /// Ftp上传文件
        /// 
        /// 本地文件路径
        /// ftp服务器地址
        /// ftp用户名
        /// ftp用户密码
        /// 
        public string Upload(string localFile, string ftpServer, string ftpUserName, string ftpPassword)
        {
            if (File.Exists(localFile))
            {
                var fileInfo = new FileInfo(localFile);
                using (var fileStream = fileInfo.OpenRead())
                {
                    var length = fileStream.Length;
                    var request = (FtpWebRequest)WebRequest.Create("ftp://" + ftpServer + "/" + fileInfo.Name);
                    request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
                    request.Method = WebRequestMethods.Ftp.UploadFile;
                    request.UseBinary = true;
                    request.ContentLength = length;
                    request.Timeout = 10 * 1000;
                    try
                    {
                        var stream = request.GetRequestStream();
                        var BufferLength = 1024 * 1024; //1M   
                        var b = new byte[BufferLength];
                        int i;
                        while ((i = fileStream.Read(b, 0, BufferLength)) > 0)
                        {
                            stream.Write(b, 0, i);
                        }
                        stream.Close();
                        stream.Dispose();
                        return "上传完毕";
                    }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
                }
            }
            return "本地文件不存在!";
        }
通过以上方式,再做个定时任务,可以将服务器文件定期备份到指定的ftp上。

你可能感兴趣的:(C#,ASP.NET)