.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 });

        }

    }

}

 

posted @ 2013-02-28 10:04  While蹒跚学步...  阅读(1427)  评论(0编辑  收藏  举报