• .net 解压 zip 包完全不用第三方 library (.net unzip zip file without third party library)


    用我下面贴出来的code 可直接使用其中的 public static void UnZip(string zipPath, string destFolder) 方法来解压zip包,没有用到任何第三方的library

    using System;

    using System.Collections.Generic;

    using System.IO;

    using System.Linq;

    using System.Reflection;

    using System.Text;

    namespace UnzipApp

    {

        public class MyZipPackage : IDisposable

        {

            private object zipPackage;

            private MyZipPackage(object zPackage)

            {

                zipPackage = zPackage;

            }

            public static MyZipPackage OpenOnFile(string path)

            {

                Type type = typeof(System.IO.Packaging.Package).Assembly.GetType("MS.Internal.IO.Zip.ZipArchive");

                var method = type.GetMethod("OpenOnFile", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

                return new MyZipPackage(method.Invoke(null, new object[] { path, FileMode.Open, FileAccess.Read, FileShare.Read, false }));

            }

            public IEnumerable<MyZipFile> Files

            {

                get

                {

                    var method = zipPackage.GetType().GetMethod("GetFiles", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

                    var zipFiles = method.Invoke(zipPackage, null) as System.Collections.IEnumerable;

                    foreach (var file in zipFiles)

                    {

                        yield return new MyZipFile(file);

                    }

                }

            }

            public static void UnZip(string zipPath, string destFolder)

            {

                if (!Directory.Exists(destFolder))

                {

                    Directory.CreateDirectory(destFolder);

                }

                using (var zip = MyZipPackage.OpenOnFile(zipPath))

                {

                    string rootFolder = destFolder;

                    foreach (var file in zip.Files)

                    {

                        if (file.Name.EndsWith("/"))

                        {

                            string[] folders = file.Name.Split('/');

                            rootFolder = destFolder;

                            for (int i = 0; i < folders.Length; i++)

                            {

                                if (!Directory.Exists(Path.Combine(rootFolder, folders[i])))

                                {

                                    Directory.CreateDirectory(Path.Combine(rootFolder, folders[i]));

                                }

                                rootFolder = Path.Combine(rootFolder, folders[i]);

                            }

                        }

                    }

                    foreach (var file in zip.Files)

                    {

                        if (!file.Name.EndsWith("/"))

                        {

                            string contentFilePath = Path.Combine(destFolder, file.Name);

                            try

                            {

                                using (System.IO.FileStream newFileStream = System.IO.File.Create(contentFilePath))

                                {

                                    newFileStream.Close();

                                    byte[] content = new byte[file.GetStream().Length];

                                    file.GetStream().Read(content, 0, content.Length);

                                    System.IO.File.WriteAllBytes(contentFilePath, content);

                                }

                            }

                            catch (DirectoryNotFoundException e)

                            {

                                string[] folders = file.Name.Split('/');

                                rootFolder = destFolder;

                                for (int i = 0; i < folders.Length - 1; i++)

                                {

                                    if (!Directory.Exists(Path.Combine(rootFolder, folders[i])))

                                    {

                                        Directory.CreateDirectory(Path.Combine(rootFolder, folders[i]));

                                    }

                                    rootFolder = Path.Combine(rootFolder, folders[i]);

                                }

                                using (System.IO.FileStream newFileStream = System.IO.File.Create(contentFilePath))

                                {

                                    newFileStream.Close();

                                    byte[] content = new byte[file.GetStream().Length];

                                    file.GetStream().Read(content, 0, content.Length);

                                    System.IO.File.WriteAllBytes(contentFilePath, content);

                                }

                            }

                        }

                    }

                }

            }

            public void Dispose()

            {

                ((IDisposable)zipPackage).Dispose();

            }

        }

        public class MyZipFile

        {

            internal object zipFile;

            public MyZipFile(object zfile)

            {

                zipFile = zfile;

            }

            private object GetProperty(string name)

            {

                return zipFile.GetType().GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).GetValue(zipFile, null);

            }

            public string Name

            {

                get { return (string)GetProperty("Name"); }

            }

            public Stream GetStream(FileMode mode = FileMode.Open, FileAccess access = FileAccess.Read)

            {

                var meth = zipFile.GetType().GetMethod("GetStream", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

                return (Stream)meth.Invoke(zipFile, new object[] { mode, access });

            }

        }

    }

  • 相关阅读:
    七. 多线程编程3.主线程
    七. 多线程编程1.线程的概念
    六. 异常处理12.断言
    liunx 安装 mysql 5.6
    idea Unable to open debugger port (127.0.0.1:58006) Address already in use: JVM_Bind 的解决办法
    liunx 安装redis 4.0
    liunx 安装jdk1.8
    idea 去除xml文件sql语句背景色
    改变数据库和表编码
    mybatis 按in 函数参数顺序排序
  • 原文地址:https://www.cnblogs.com/infixu/p/Unzip.html
Copyright © 2020-2023  润新知