在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#文件解析和处理 → ICSharpCode.SharpZipLib.dll 解压缩文件例子 并附完整ISharpZip项目源码

ICSharpCode.SharpZipLib.dll 解压缩文件例子 并附完整ISharpZip项目源码

C#文件解析和处理

下载此实例
  • 开发语言:C#
  • 实例大小:0.69M
  • 下载次数:37
  • 浏览次数:2209
  • 发布时间:2013-06-25
  • 实例类别:C#文件解析和处理
  • 发 布 人:crazycode
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 解压缩

实例介绍

【实例简介】
【实例截图】


【核心代码】

using System;
using System.IO;

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Core;

namespace Samples.FastZipDemo
{
    class MainClass
    {
        enum Operation
        {
            Unknown,
            Create,
            Extract,
            List,
            Error
        };

        static void ListZipFile(string fileName, string fileFilter, string directoryFilter)
        {
            using (ZipFile zipFile = new ZipFile(fileName)) {
                PathFilter localFileFilter = new PathFilter(fileFilter);
                PathFilter localDirFilter = new PathFilter(directoryFilter);
				
                if ( zipFile.Count == 0 ) {
                    Console.WriteLine("No entries to list");
                }
                else {
                    for ( int i = 0 ; i < zipFile.Count;   i)
                    {
                        ZipEntry e = zipFile[i];
                        if ( e.IsFile ) {
                            string path = Path.GetDirectoryName(e.Name);
                            if ( localDirFilter.IsMatch(path) ) {
                                if ( localFileFilter.IsMatch(Path.GetFileName(e.Name)) ) {
                                    Console.WriteLine(e.Name);
                                }
                            }
                        }
                        else if ( e.IsDirectory ) {
                            if ( localDirFilter.IsMatch(e.Name) ) {
                                Console.WriteLine(e.Name);
                            }
                        }
                        else {
                            Console.WriteLine(e.Name);
                        }
                    }
                }
            }
        }
		
        void ListFile(object sender, ScanEventArgs e)
        {
            Console.WriteLine("{0}", e.Name);
        }
		
        void ListDir(object sender, DirectoryEventArgs e)
        {
            if ( !e.HasMatchingFiles ) {
                Console.WriteLine("Dir:{0}", e.Name);
            }
        }

        void ListFileSystem(string directory, bool recurse, string fileFilter, string directoryFilter)
        {
            FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter);
            scanner.ProcessDirectory  = new ProcessDirectoryHandler(ListDir);
            scanner.ProcessFile  = new ProcessFileHandler(ListFile);
            scanner.Scan(directory, recurse);
        }

        void ShowProgress(object sender, ProgressEventArgs e)
        {
            // Very ugly but this is a sample!
            Console.WriteLine("{0}%", e.PercentComplete);
        }

        void ProcessFile(object sender, ScanEventArgs e)
        {
            Console.WriteLine(e.Name);
        }
		
        void ProcessDirectory(object sender, DirectoryEventArgs e)
        {
            if ( !e.HasMatchingFiles ) {
                Console.WriteLine(e.Name);
            }
        }

        bool ConfirmOverwrite(string file)
        {
            Console.WriteLine("Overwrite file {0} Y/N", file);
            string yesNo = Console.ReadLine();
            return (yesNo != null) && string.Compare(yesNo.Trim(), "y", true) == 0;
        }
		
        void Run(string[] args)
        {
            bool recurse = false;
            string arg1 = null;
            string arg2 = null;
            string fileFilter = null;
            string dirFilter = null;
            bool verbose = false;
            bool restoreDates = false;
            bool restoreAttributes = false;
            bool progress = false;
            TimeSpan interval = TimeSpan.FromSeconds(1);

            bool createEmptyDirs = false;
            FastZip.Overwrite overwrite = FastZip.Overwrite.Always;
            FastZip.ConfirmOverwriteDelegate confirmOverwrite = null;
			
            Operation op = Operation.Unknown;
            int argCount = 0;
			
            for ( int i = 0; i < args.Length;   i ) {
                if ( args[i][0] == '-' ) {
                    string option = args[i].Substring(1).ToLower();
                    string optArg = "";
	
                    int parameterIndex = option.IndexOf('=');
	
                    if (parameterIndex >= 0)
                    {
                        if (parameterIndex < option.Length - 1) {
                            optArg = option.Substring(parameterIndex   1);
                        }
                        option = option.Substring(0, parameterIndex);
                    }
					
                    switch ( option ) {
                        case "e":
                        case "empty":
                            createEmptyDirs = true;
                            break;
							
                        case "x":
                        case "extract":
                            if ( op == Operation.Unknown ) {
                                op = Operation.Extract;
                            }
                            else {
                                Console.WriteLine("Only one operation at a time is permitted");
                                op = Operation.Error;
                            }
                            break;
							
                        case "c":
                        case "create":
                            if ( op == Operation.Unknown ) {
                                op = Operation.Create;
                            }
                            else {
                                Console.WriteLine("Only one operation at a time is permitted");
                                op = Operation.Error;
                            }
                            break;

                        case "l":
                        case "list":
                            if ( op == Operation.Unknown ) {
                                op = Operation.List;
                            }
                            else {
                                Console.WriteLine("Only one operation at a time is permitted");
                                op = Operation.Error;
                            }
                            break;

							
                        case "p":
                        case "progress":
                            progress = true;
                            verbose = true;
                            break;

                        case "r":
                        case "recurse":
                            recurse = true;
                            break;
							
                        case "v":
                        case "verbose":
                            verbose = true;
                            break;

                        case "i":
                            if (optArg.Length > 0) {
                                interval = TimeSpan.FromSeconds(int.Parse(optArg));
                            }
                            break;

                        case "file":
                            if ( NameFilter.IsValidFilterExpression(optArg) ) {
                                fileFilter = optArg;
                            }
                            else {
                                Console.WriteLine("File filter expression contains an invalid regular expression");
                                op = Operation.Error;
                            }
                            break;
							
                        case "dir":
                            if ( NameFilter.IsValidFilterExpression(optArg) ) {
                                dirFilter = optArg;
                            }
                            else {
                                Console.WriteLine("Path filter expression contains an invalid regular expression");
                                op = Operation.Error;
                            }
                            break;
							
                        case "o":
                        case "overwrite":
                            switch ( optArg )
                            {
                                case "always":
                                    overwrite = FastZip.Overwrite.Always;
                                    confirmOverwrite = null;
                                    break;
									
                                case "never":
                                    overwrite = FastZip.Overwrite.Never;
                                    confirmOverwrite = null;
                                    break;
									
                                case "prompt":
                                    overwrite = FastZip.Overwrite.Prompt;
                                    confirmOverwrite = new FastZip.ConfirmOverwriteDelegate(ConfirmOverwrite);
                                    break;
									
                                default:
                                    Console.WriteLine("Invalid overwrite option");
                                    op = Operation.Error;
                                    break;
                            }
                            break;
							
                        case "oa":
                            restoreAttributes = true;
                            break;
							
                        case "od":
                            restoreDates = true;
                            break;
							
                        default:
                            Console.WriteLine("Unknown option {0}", args[i]);
                            op = Operation.Error;
                            break;
                    }
                }
                else if ( arg1 == null ) {
                    arg1 = args[i];
                      argCount;
                }
                else if ( arg2 == null ) {
                    arg2 = args[i];
                      argCount;
                }
            }

            FastZipEvents events = null;
			
            if ( verbose ) {
                events = new FastZipEvents();
                events.ProcessDirectory = new ProcessDirectoryHandler(ProcessDirectory);
                events.ProcessFile = new ProcessFileHandler(ProcessFile);

                if (progress)
                {
                    events.Progress = new ProgressHandler(ShowProgress);
                    events.ProgressInterval = interval;
                }
            }
			
            FastZip fastZip = new FastZip(events);
            fastZip.CreateEmptyDirectories = createEmptyDirs;
            fastZip.RestoreAttributesOnExtract = restoreAttributes;
            fastZip.RestoreDateTimeOnExtract = restoreDates;
			
            switch ( op ) {
                case Operation.Create:
                    if ( argCount == 2 ) {
                        Console.WriteLine("Creating Zip");

                        fastZip.CreateZip(arg1, arg2, recurse, fileFilter, dirFilter);
                    }
                    else
                        Console.WriteLine("Invalid arguments");
                    break;
					
                case Operation.Extract:
                    if ( argCount == 2 ) {
                        Console.WriteLine("Extracting Zip");
                        fastZip.ExtractZip(arg1, arg2, overwrite, confirmOverwrite, fileFilter, dirFilter, restoreDates);
                    }
                    else
                        Console.WriteLine("zipfile and target directory not specified");
                    break;
					
                case Operation.List:
                    if ( File.Exists(arg1) ) {
                        ListZipFile(arg1, fileFilter, dirFilter);
                    }
                    else if ( Directory.Exists(arg1) ) {
                        ListFileSystem(arg1, recurse, fileFilter, dirFilter);
                    }
                    else {
                        Console.WriteLine("No valid list file or directory");
                    }
                    break;
					
                case Operation.Unknown:
                    Console.WriteLine(
                        "FastZip v0.5\n"
                           "  Usage: FastZip {options} operation args\n"
                           "Operation Options: (only one permitted)\n"
                           "  -x zipfile targetdir : Extract files from Zip\n"
                           "  -c zipfile sourcedir : Create zip file\n"
                           "  -l zipfile|dir       : List elements\n"
                           "\n"
                           "Behavioural options:\n"
                           "  -dir={dirFilter}\n"
                           "  -file={fileFilter}\n"
                           "  -e Process empty directories\n"
                           "  -i Progress interval in seconds\n"
                           "  -p Show file progress\n"
                           "  -r Recurse directories\n"
                           "  -v Verbose output\n"
                           "  -oa Restore file attributes on extract\n"
                           "  -ot Restore file date time on extract\n"
                           "  -overwrite=prompt|always|never   : Overwrite on extract handling\n"
                        );
                    break;
				
                case Operation.Error:
                    // Do nothing for now...
                    break;
            }
        }
		
        /// <summary>
        /// Main entry point for FastZip sample.
        /// </summary>
        /// <param name="args">The arguments provided to this process.</param>
        public static void Main(string[] args)
        {
            MainClass main = new MainClass();
            main.Run(args);
        }
    }
}

标签: 解压缩

实例下载地址

ICSharpCode.SharpZipLib.dll 解压缩文件例子 并附完整ISharpZip项目源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 20zhang46 发表于: 2018-03-27 16:45 08
2222

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有1条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警