1. 程式人生 > >C# 利用Rar壓縮檔案並FTP上傳

C# 利用Rar壓縮檔案並FTP上傳

1.利用Rar壓縮檔案

        /// <summary>
        /// 使用Rar壓縮檔案
        /// </summary>
        /// <param name="fromFilePath">待壓縮檔案路徑</param>
        /// <param name="rarFilePath">壓縮後的檔案路徑</param>
        /// <returns>返回壓縮結果</returns>
        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上傳檔案

       /// <summary>
        /// Ftp上傳檔案
        /// </summary>
        /// <param name="localFile">本地檔案路徑</param>
        /// <param name="ftpServer">ftp伺服器地址</param>
        /// <param name="ftpUserName">ftp使用者名稱</param>
        /// <param name="ftpPassword">ftp使用者密碼</param>
        /// <returns></returns>
        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上。