博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PathFileHelper GZipFileHelper (Decompress)
阅读量:7016 次
发布时间:2019-06-28

本文共 12932 字,大约阅读时间需要 43 分钟。

#define NET45namespace 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 NET45namespace Microshaoft{
using System; using System.IO; using System.IO.Compression; public static class GZipFileHelper {
public static bool Decompress ( string originalFileFullPath , string targetDirectoryPath , Func
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; } }}#endifnamespace 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
( () => {
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; } }}

转载于:https://www.cnblogs.com/Microshaoft/archive/2012/08/12/2634195.html

你可能感兴趣的文章
AC日记——[SDOI2015]星际战争 洛谷 P3324
查看>>
Gcc编译Objective-C命令行 + UltraEdit(用ultraEdit打造自己的Objective-C IDE for Windows补充)...
查看>>
CSS(一)
查看>>
jquery树形菜单插件treeView
查看>>
autofs文件自动挂载系统
查看>>
bzoj千题计划224:bzoj1023: [SHOI2008]cactus仙人掌图
查看>>
java日期处理SimpleDateFormat等
查看>>
replace characters in a string
查看>>
旋转数组---简单
查看>>
第十六周进度表
查看>>
ASP.NET读取网络图片并在页面上显示
查看>>
python代码审查之flake8
查看>>
我们都曾经历过:生活教会我的8堂人生课
查看>>
Ubuntu中安装XAMPP出错的解决方法
查看>>
Bzoj2563 阿狸和桃子的游戏
查看>>
洛谷P1993 小 K 的农场
查看>>
合数据类型练习
查看>>
设计模式及Python实现
查看>>
Node和Jquery实现Jsonp
查看>>
Hibernate中使用Criteria查询
查看>>