⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unzipclass.cs

📁 压缩和解压缩的源码,可以支持大多数文件的压缩和解压缩
💻 CS
字号:
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 SoundCondense
{
	/// <summary>
	/// UnZipClass 的摘要说明。
	/// </summary>
	public class UnZipClass
	{
		public void UnZip(string[] args)
		{
			ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));
  
			try
			{
				ZipEntry theEntry;
				while ((theEntry = s.GetNextEntry()) != null) 
				{   
					string directoryName = Path.GetDirectoryName(args[1]);
					string fileName  = Path.GetFileName(theEntry.Name);
   
					//生成解压目录
					Directory.CreateDirectory(directoryName);
   
					if (fileName != String.Empty) 
					{   
						//解压文件到指定的目录
						FileStream streamWriter = File.Create(args[1]+theEntry.Name);
    
						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();
			}
			catch(Exception ex)
			{
				throw ex;
			}
			finally
			{
				s.Close();
			}
		}

		public static bool UnZipFile(string file, string dir)
  	{
   		try
   		{
    		if (!Directory.Exists(dir))
     		Directory.CreateDirectory(dir);
     		string fileFullName = Path.Combine(dir,file);
     		ZipInputStream s = new ZipInputStream(File.OpenRead( fileFullName ));
 
     		ZipEntry theEntry;
     		while ((theEntry = s.GetNextEntry()) != null)
     		{
      		string directoryName = Path.GetDirectoryName(theEntry.Name);
      		string fileName = Path.GetFileName(theEntry.Name);
 
      		if (directoryName != String.Empty)
       		Directory.CreateDirectory( Path.Combine(dir, directoryName));
       		if (fileName != String.Empty)
       		{
        		FileStream streamWriter = File.Create( Path.Combine(dir,theEntry.Name) );
        		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();
      	return true;
     	}
     	catch (Exception ex)
     	{
      	throw ex;
    	}
    }

		/// <summary>
		/// 解压文件
		/// </summary>
		/// <param name="sourceFileName">被解压文件名称</param>
		/// <param name="destPath">解压后文件目录</param>
		/// <param name="fileType">文件类型</param>
		public static void unzipFile(string sourceFileName,string destPath,string fileType)
		{
			ZipInputStream s = new ZipInputStream(File.OpenRead(sourceFileName));
			ZipEntry theEntry;
			ArrayList al=new ArrayList();

			while ((theEntry = s.GetNextEntry()) != null) 
			{   
				string fileName=Path.GetFileName(theEntry.Name);
				if(fileName!="")
				{
					fileName=destPath+"\\"+fileName;
					if(!Directory.Exists(destPath))
					{
						Directory.CreateDirectory(destPath);
					}
					FileStream streamWriter = File.Create(fileName);     
					int size = 2048;
					byte[] data = new byte[2048];
					s.Password="";
					while (true) 
					{
						size = s.Read(data, 0, data.Length);
						if (size > 0) 
						{
							streamWriter.Write(data, 0, size);
						} 
						else 
						{
							break;
						}
					}
					streamWriter.Close();
				}
			}
			s.Close();   
		}


		/// <summary>
        /// 解压缩指定的Zip文件
        /// </summary>
        /// <param name="源文件">要解压缩的文件路径</param>
        /// <param name="目标文件">解压缩后保存的文件路径</param>
       /*  
	    public static void UnCompressFile(string oldFile, string newFile)
        {
            if (!File.Exists(oldFile)) throw new FileNotFoundException();
            using (FileStream sourceStream = new FileStream(oldFile, FileMode.Open))
            {
                byte[] quartetBuffer = new byte[4];
                int position = (int)sourceStream.Length - 4;
                sourceStream.Position = position;
                sourceStream.Read(quartetBuffer, 0, 4);
                sourceStream.Position = 0;
                int checkLength = BitConverter.ToInt32(quartetBuffer, 0);
                byte[] buffer = new byte[checkLength + 100];
                using (GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true))
                {
                    int total = 0;
                    for (int offset = 0; ; )
                    {
                        int bytesRead = decompressedStream.Read(buffer, offset, 100);
                        if (bytesRead == 0) break;
                        offset += bytesRead;
                        total += bytesRead;
                    }
                    using (FileStream destinationStream = new FileStream(newFile, FileMode.Create))
                    {
                        destinationStream.Write(buffer, 0, total);
                        destinationStream.Flush();
                    }
                }
            }
        }
		*/
	}
}

⌨️ 快捷键说明

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