unzip.cs

来自「北大青鸟-Cs学生用书-教师用书实例源代码.rar」· CS 代码 · 共 90 行

CS
90
字号
using System;
using System.Text;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data;

using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;

namespace Compression
{
	public class Unzip
	{   
		public void UnZipFile(string ZipFileName, string UnzipPath)
		{
			ZipInputStream s = new ZipInputStream(File.OpenRead(ZipFileName));
  
			ZipEntry theEntry;
			while ((theEntry = s.GetNextEntry()) != null) 
			{
   
				string directoryName = UnzipPath;
				string fileName      = ZipFileName;
   
				//生成解压目录
				
				Directory.CreateDirectory(directoryName);
   
				if (fileName != String.Empty) 
				{   
					//解压文件到指定的目录
					string entryPath = theEntry.Name;
					if(entryPath.Substring(1, 1) == ":")
					{
						entryPath = entryPath.Substring(2);
					}
					
					string path = GetFilePath(directoryName + entryPath.Replace(@"\\", @"\"));
					Directory.CreateDirectory(path);
					FileStream streamWriter = File.Create(directoryName + entryPath.Replace(@"\\", @"\"));
    
					int size = 2048;
					byte[] data = new byte[2048];
					while (true) 
					{
						size = s.Read(data, 0, data.Length);
						if (size > 0) 
						{
							streamWriter.Write(data, 0, size);
						} 
						else 
						{
							break;
						}
					}
    
					streamWriter.Close();
				}
			}
			s.Close();
		}
	
		private string  GetFilePath(string FileName)
		{
			string[] strs = FileName.Split('\\');
			string Path = "";
			for(int i = 0; i < strs.Length - 1; i++)
			{
				Path += strs[i] + "\\";
			}

			if(Path == "")
			{
				return @"c:\";
			}

			return Path;
			
		}

	}

 }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?