12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- 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)
- {
- size = s.Read(data, 0, data.Length);
- streamWriter.Write(data, 0, size);
- }
- }
- }
- }
- }
- }
- }
- }
|