zoukankan      html  css  js  c++  java
  • PathFileHelper GZipFileHelper (Decompress)

    
    #define NET45
    namespace Test
    {
        using Microshaoft;
        using System;
        using System.Diagnostics;
        using System.IO;
        public class Program
        {
            public static void Main()
            {
    #if NET45
                var files = Directory.EnumerateFiles(@"d:\", "*.gz*", SearchOption.TopDirectoryOnly);
                foreach (var file in files)
                {
                    var r = false;
                    string path = string.Empty;
                    try
                    {
                        if
                            (
                                GZipFileHelper
                                    .Decompress
                                        (
                                            file
                                            , @"d:\temp"
                                            , (x) =>
                                            {
                                                var s = ".Gz";
                                                int p = x.ToLower().LastIndexOf(s.ToLower());
                                                if (p > 0)
                                                {
                                                    x = x.Remove(p, s.Length);
                                                }
                                                return x;
                                            }
                                            , out path
                                        )
                            )
                        {
                            r = PathFileHelper
                                    .MoveFileTo
                                        (
                                            path
                                            , @"d:\Temp"
                                            , @"d:\Temp2"
                                            , true
                                        );
                        }
                    }
                    catch (Exception e)
                    {
                        string log = string
                                        .Format
                                            (
                                                "Process file: [{1}] caught exception:{0}{2}"
                                                , "\r\n"
                                                , file
                                                , e.ToString()
                                            );
                        Console.Error.WriteLine(log);
                        EventLogHelper
                            .WriteEventLogEntry
                                (
                                    ""
                                    , log
                                    , EventLogEntryType.Error
                                    , 1001
                                );
                        r = false;
                    }
                    if (r)
                    {
                        PathFileHelper
                                .MoveFileTo
                                    (
                                        file
                                        , @"d:\"
                                        , @"d:\Temp3"
                                        , true
                                    );
                        Console.WriteLine("ok: {0}", file);
                    }
                    else
                    {
                        PathFileHelper
                                    .MoveFileTo
                                        (
                                            file
                                            , @"d:\Temp"
                                            , @"d:\Temp4"
                                            , true
                                        );
                    }
                }
                Console.ReadLine();
    #endif
            }
        }
    }
    #if NET45
    namespace Microshaoft
    {
        using System;
        using System.IO;
        using System.IO.Compression;
        public static class GZipFileHelper
        {
            public static bool Decompress
                                (
                                    string originalFileFullPath
                                    , string targetDirectoryPath
                                    , Func<string, string> onNamingDecompressedFileProcessFunc
                                    , out string decompressedFileFullPath
                                )
            {
                var r = false;
                using (FileStream originalFileStream = File.OpenRead(originalFileFullPath))
                {
                    var originalFileExtensionName = Path.GetExtension(originalFileFullPath);
                    var originalDirectoryPath = Path.GetDirectoryName(originalFileFullPath);
                    decompressedFileFullPath = PathFileHelper.GetNewPath(originalDirectoryPath, targetDirectoryPath, originalFileFullPath);
                    string fileName = Path.GetFileName(decompressedFileFullPath);
                    string directory = Path.GetDirectoryName(decompressedFileFullPath);
                    if (onNamingDecompressedFileProcessFunc != null)
                    {
                        fileName = onNamingDecompressedFileProcessFunc(fileName);
                    }
                    decompressedFileFullPath = Path.Combine(directory, fileName);
                    using (FileStream decompressedFileStream = File.Create(decompressedFileFullPath))
                    {
                        using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
                        {
                            decompressionStream.CopyTo(decompressedFileStream);
                            r = true;
                        }
                    }
                }
                return r;
            }
        }
    }
    #endif
    namespace Microshaoft
    {
        using System;
        using System.IO;
        using System.Linq;
        public static class PathFileHelper
        {
            public static bool MoveFileTo
                                (
                                    string sourceFullPathFileName
                                    , string sourceDirectoryPath
                                    , string destDirectorytPath
                                    , bool deleteExistsDestFile = false
                                )
            {
                var r = false;
                var destFullPathFileName = GetNewPath(sourceDirectoryPath, destDirectorytPath, sourceFullPathFileName);
                var directory = Path.GetDirectoryName(destFullPathFileName);
                if (File.Exists(directory))
                {
                    File.Delete(directory);
                }
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
                if (deleteExistsDestFile)
                {
                    if (File.Exists(destFullPathFileName))
                    {
                        File.Delete(destFullPathFileName);
                    }
                }
                File.Move(sourceFullPathFileName, destFullPathFileName);
                r = true;
                return r;
            }
            public static string GetValidPathOrFileName(string path, string replacement)
            {
                string s = string.Empty;
                var chars = Path.GetInvalidPathChars();
                chars = chars.Union(Path.GetInvalidFileNameChars()).ToArray();
                Array
                    .ForEach
                        (
                            chars
                            , (x) =>
                            {
                                s = s.Replace(x.ToString(), replacement);
                            }
                        );
                return s;
            }
            public static string GetNewPath(string oldDirectoryPath, string newDirectoryPath, string originalFileFullPath)
            {
                string newPath = newDirectoryPath;
                originalFileFullPath = Path.GetFullPath(originalFileFullPath);
                var directorySeparator = Path.DirectorySeparatorChar.ToString();
                oldDirectoryPath = Path.GetFullPath(oldDirectoryPath);
                newDirectoryPath = Path.GetFullPath(newDirectoryPath);
                if (!oldDirectoryPath.EndsWith(directorySeparator))
                {
                    oldDirectoryPath += directorySeparator;
                }
                if (!newDirectoryPath.EndsWith(directorySeparator))
                {
                    newDirectoryPath += directorySeparator;
                }
                string relativeDirectoryPath = string.Empty;
                int p = originalFileFullPath
                            .ToLower()
                            .IndexOf(oldDirectoryPath.ToLower());
                if (p >= 0)
                {
                    p += oldDirectoryPath.Length;
                    relativeDirectoryPath = originalFileFullPath.Substring(p);
                    newPath = Path.Combine(newPath, relativeDirectoryPath);
                }
                newPath = Path.GetFullPath(newPath);
                return newPath;
            }
        }
    }
    namespace Microshaoft
    {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Diagnostics;
        public static class EventLogHelper
        {
            private static string _processNameID = new Func<string>
                                                    (
                                                        () =>
                                                        {
                                                            var process = Process.GetCurrentProcess();
                                                            return
                                                                string.Format
                                                                        (
                                                                            "{1}{0}({2})"
                                                                            , ""
                                                                            , process.ProcessName
                                                                            , process.Id
                                                                        );
                                                        }
                                                    )();
            public static EventLog[] GetEventLogs()
            {
                var r = EventLog.GetEventLogs();
                return r;
            }
            public static void WriteEventLogEntry
                                            (
                                                //string logName,
                                                string sourceName,
                                                string logMessage,
                                                EventLogEntryType logEntryType
                                                , int eventID
                                            )
            {
                EventLog eventLog = new EventLog();
                eventLog.Source = sourceName;
                //eventLog.Log = logName;
                logMessage = string.Format
                                (
                                    "{1}{0}Process [{3}] @ {4}{0}{5}{0}{2}"
                                    , "\r\n"
                                    , "begin ==========================================="
                                    , "end ============================================="
                                    , _processNameID
                                    , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffff")
                                    , logMessage
                                );
                eventLog.WriteEntry
                    (
                        logMessage
                        , logEntryType
                        , eventID
                    );
            }
            public static bool TryCreateEventSourceLog
                                            (
                                                string logName,
                                                string sourceName,
                                                bool allowRecreate = false
                                            )
            {
                bool r = false;
                if (EventLog.SourceExists(sourceName))
                {
                    if (allowRecreate)
                    {
                        try
                        {
                            var s = EventLog.LogNameFromSourceName(sourceName, ".");
                            if (string.Compare(s, logName, true) != 0)
                            {
                                EventLog.DeleteEventSource(sourceName);
                                EventLog.Delete(logName);
                                EventLog.CreateEventSource(sourceName, logName);
                                r = true;
                            }
                        }
                        catch// (Exception e)
                        {
                            r = false;
                        }
                    }
                    else
                    {
                        r = true;
                    }
                }
                else
                {
                    EventLog.CreateEventSource(sourceName, logName);
                    r = true;
                }
                return r;
            }
        }
    }
    
    
  • 相关阅读:
    Java中的CopyOnWrite
    Collection和Collections的区别
    java中值类型与引用类型的关系
    Xml的用途
    js弹框的3种方法
    文件夹重定向失败解决方案
    网络管理人员经常遇到的十个问题(转载)
    QTP之下拉列表单选框…
    Windows脚本宿主对象模型
    QTP报错“缺少对象WScript”
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/2634195.html
Copyright © 2011-2022 走看看