search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

C# MySQL/文件檔案 壓縮(ZIP) 備份 ~ DBBackup​ – jashliao部落格

C# MySQL/文件檔案 壓縮(ZIP) 備份 ~ DBBackup

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing;
using System.Diagnostics;
using System.IO.Compression;//C# 透過程式壓縮資料夾為zip檔[NET4.5 版本 內建的方法]

namespace SYWEB_V8_Workstation
{
    public class FileLib
    {
        public static bool DBBackup(String StrName="")
        {
            bool blnAns = false;
            
            if(StrName.Length==0)
            {
                StrName=DateTime.Now.ToString("yyyyMMddHHmmss")+".zip";
            }

            String StrSrcFullFilePath = String.Format("{0}\\{1}", path, "mysql\\data\\v8_workstation");
            string StrOutPath = String.Format("{0}\\{1}", path, "DBBackup\\");
            CreateDirectory(StrOutPath);
            CreateDirectory(StrOutPath + "v8_workstation\\");
            DirectoryCopy(StrSrcFullFilePath, StrOutPath+ "v8_workstation\\", true);
            ZipFile.CreateFromDirectory(StrOutPath+ "\\v8_workstation", StrOutPath + StrName);
            DeleteDirectory(StrOutPath + "v8_workstation\\", true);

            return blnAns;
        }

        //刪除目錄,recursive為True時,直接刪除目錄及其目錄下所有文件或子目錄;recursive為False時,需先將目錄下所有文件或子目錄刪除
        private static void DeleteDirectory(string path, bool recursive)
        {
            if (Directory.Exists(path))
            {
                if (recursive)
                {
                    Directory.Delete(path, true);
                }
            }
        }

        public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
        {
            // Get the subdirectories for the specified directory.
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException(
                    "Source directory does not exist or could not be found: "
                    + sourceDirName);
            }

            DirectoryInfo[] dirs = dir.GetDirectories();
            // If the destination directory doesn't exist, create it.
            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }

            // Get the files in the directory and copy them to the new location.
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                string temppath = Path.Combine(destDirName, file.Name);
                file.CopyTo(temppath, false);
            }

            // If copying subdirectories, copy them and their contents to new location.
            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(destDirName, subdir.Name);
                    DirectoryCopy(subdir.FullName, temppath, copySubDirs);
                }
            }
        }

        //建立目錄
        private static void CreateDirectory(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        public static string path = System.Windows.Forms.Application.StartupPath;
        public static bool IsUTF8File(String StrName)
        {
            bool blnAns = false;

            Stream reader = File.Open(StrName, FileMode.Open, FileAccess.Read);
            Encoding encoder = null;
            byte[] header = new byte[4];
            // 讀取前四個Byte
            reader.Read(header, 0, 4);
            if (header[0] == 0xFF && header[1] == 0xFE)
            {
                // UniCode File
                reader.Position = 2;
                encoder = Encoding.Unicode;
            }
            else if (header[0] == 0xEF && header[1] == 0xBB && header[2] == 0xBF)
            {
                // UTF-8 File
                reader.Position = 3;
                encoder = Encoding.UTF8;
                blnAns = true;
            }
            else
            {
                // Default Encoding File
                reader.Position = 0;
                encoder = Encoding.Default;
            }

            reader.Close();
            // .......... 接下來的程式

            return blnAns;
        }
        public static String ImageFile2Base64String(String StrDestFilePath)
        {
            String StrAns = "";
            try
            {
                //開啟檔案
                FileStream file = File.Open(StrDestFilePath, FileMode.Open, FileAccess.ReadWrite);
                    //引用myReader類別
                    BinaryReader read = new BinaryReader(file);
                        int len = System.Convert.ToInt32(file.Length);
                        //讀取位元陣列
                        byte []data = read.ReadBytes(len);
                        StrAns = Convert.ToBase64String(data);
                        //讀取資料
                        //釋放資源
                        read.Close();
                    read.Dispose();
                file.Close();
                //file.Flush();
                
            }
            catch
            {
            }
            return StrAns;
        }
        public static void CreateFile(String StrDestFilePath, byte[] byteSource)
        {
            FileLib.DeleteFile("temp.png");
            try
            {
                //開啟建立檔案
                FileStream file = File.Open(StrDestFilePath, FileMode.Create, FileAccess.ReadWrite);
                    BinaryWriter write = new BinaryWriter(file);
                        write.Write(byteSource);
                        write.Close();
                        write.Dispose();
                    write.Flush();

                    file.Close();
                    file.Flush();
                file.Dispose();
            }
            catch
            {

            }
        }
        public static void CopyFile(String StrSourceFilePath, String StrDestFilePath)
        {
            System.IO.File.Copy(StrSourceFilePath, StrDestFilePath, true);
        }
        public static void DeleteFile(String StrFileName,bool blnAtRoot=true)
        {
            String FilePath="";
            if (blnAtRoot == true)
            {
                FilePath = path + "\\" + StrFileName;
            }
            else
            {
                FilePath = StrFileName;
            }
            if (System.IO.File.Exists(FilePath))
            {
                try
                {
                    System.IO.File.Delete(FilePath);
                }
                catch
                {
                }
            }
        }
        public static void TxtFile(String StrFileName, String StrData)
        {
            StreamWriter sw = new StreamWriter(StrFileName);
            sw.WriteLine(StrData);// 寫入文字
            sw.Close();// 關閉串流
        }
        public static void logFile(String StrFileName, String StrData)
        {
            FileStream fs = new FileStream(StrFileName, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(DateTime.Now.ToString("HH:mm:ss - ")+StrData);// 寫入文字
            sw.Close();// 關閉串流
        }

        //---
        //除錯(DEBUG)專用 log函式庫
        public static bool g_blnDebug = false;
        public static StreamWriter g_sw;
        public static void createLogFile()
        {
            if (g_blnDebug == true)
            {
                g_sw = new StreamWriter(Application.StartupPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd-HHmm") + ".log");
                writeLogFile("createLogFile");
            }
        }
        public static void writeLogFile(String msg)
        {
            if (g_blnDebug == true)
            {
                g_sw.WriteLine(msg + ",\t" + DateTime.Now.ToString("yyyy/MM/dd/HH:mm:ss"));
            }
        }
        public static void closeLogFile()
        {
            if (g_blnDebug == true)
            {
                writeLogFile("closeLogFile");
                g_sw.Close();// 關閉串流
            }
        }
        //---除錯(DEBUG)專用 log函式庫

        //---
        //縮圖
        public static bool ImageResize(String strImageSrcPath, String strImageDesPath, int intWidth = 0, int intHeight = 0)
        {
            /*
            ImageFormat IF_Png = ImageFormat.Png; 
            bool blnAns = true;
            Image objImage = Image.FromFile(strImageSrcPath);
            if (intWidth > objImage.Width)
            {
                intWidth = objImage.Width;
            }
            if (intHeight > objImage.Height)
            {
                intHeight = objImage.Height;
            }
            if ((intWidth == 0) && (intHeight == 0))
            {
                intWidth = objImage.Width;
                intHeight = objImage.Height;
            }
            else if ((intHeight == 0) && (intWidth != 0))
            {
                intHeight = (int)(objImage.Height * intWidth / objImage.Width);
            }
            else if ((intWidth == 0) && (intHeight != 0))
            {
                intWidth = (int)(objImage.Width * intHeight / objImage.Height);
            }
            Bitmap imgOutput = new Bitmap(objImage, intWidth, intHeight);
            imgOutput.Save(strImageDesPath, IF_Png);//imgOutput.Save(strImageDesPath, objImage.RawFormat);
            objImage.Dispose();
            objImage = null;
            imgOutput.Dispose();
            imgOutput = null;
            return blnAns;
            */
            Process m_pro;
            String StrVar = String.Format("\"{0}\" \"{1}\" {2}", strImageSrcPath, strImageDesPath, intWidth);
            ProcessStartInfo startInfo = new ProcessStartInfo("CS_cmd_ImageResize.exe", StrVar);
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;

            try
            {
                m_pro = Process.Start(startInfo);
            }
            catch
            {
                return false;//找不到執行檔的防呆 at 2017/06/16
            }
            if (m_pro != null)
            {
                m_pro.WaitForExit();//下載SERVER資料
                m_pro = null;
            }

            return true;
        }

        //---縮圖
    }
}


熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦