浏览代码

添加ZIP文件解压方法

章政 8 年之前
父节点
当前提交
16a88ba5ae
共有 2 个文件被更改,包括 61 次插入0 次删除
  1. 61 0
      UAS-MES/PublicMethod/ZipHelper.cs
  2. 二进制
      UAS-MES/Tool/ICSharpCode.SharpZipLib.dll

+ 61 - 0
UAS-MES/PublicMethod/ZipHelper.cs

@@ -0,0 +1,61 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using ICSharpCode.SharpZipLib.Zip;
+using System.IO;
+
+namespace UAS_MES.PublicMethod
+{
+    class ZipHelper
+    {
+        /// <summary>
+        /// 用于解压缩Zip文件
+        /// </summary>
+        /// <param name="ZipFilePath"></param>
+        /// <param name="UnZipPath"></param>
+        public static void UnZip(string ZipFilePath, string UnZipPath)
+        {
+            if (!File.Exists(ZipFilePath))
+            {
+                throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", ZipFilePath));
+            }
+
+            if (!Directory.Exists(UnZipPath))
+            {
+                Directory.CreateDirectory(UnZipPath);
+            }
+
+            using (var s = new ZipInputStream(File.OpenRead(ZipFilePath)))
+            {
+                ZipEntry theEntry;
+                while ((theEntry = s.GetNextEntry()) != null)
+                {
+                    if (theEntry.IsDirectory)
+                    {
+                        continue;
+                    }
+                    string directorName = Path.Combine(UnZipPath, Path.GetDirectoryName(theEntry.Name));
+                    string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));
+                    if (!Directory.Exists(directorName))
+                    {
+                        Directory.CreateDirectory(directorName);
+                    }
+                    if (!String.IsNullOrEmpty(fileName))
+                    {
+                        using (FileStream streamWriter = File.Create(fileName))
+                        {
+                            int size = 4096;
+                            byte[] data = new byte[size];
+                            while (size > 0)
+                            {
+                                streamWriter.Write(data, 0, size);
+                                size = s.Read(data, 0, data.Length);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+}

二进制
UAS-MES/Tool/ICSharpCode.SharpZipLib.dll