c# 压缩文件及解压Zip

本文是我自己在写功能的时候用到,在网上查看到别人的,所以转载过来,再添点我自己的理解备忘。原文链接:http://www.cnblogs.com/GoCircle/p/6544678.html。

这里利用了一个压缩帮助类,使用的是有要添加一个dll引用ICSharpCode.SharpZipLib.dll,下载地址:http://download.csdn.net/detail/cleopard/8304539,下载完成后将这个dll引用进来,然后再代码的开头要声明一下:

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;

其中ICSharpCode是引用文件夹中存放dll的路径。

压缩文件以及文件夹为zip代码如下:

		/// <summary>  
		///  ファイルをZipする  
		/// </summary>  
		/// <param name="filesName">ファイル名</param>  
		/// <param name="ZipedFileName">Zipファイル名</param>  
		/// <returns></returns>  
		public static bool Zip(string[] filesName, string ZipedFileName,string kbn)
		{
			//有効ファイルの再配列
			filesName = filesName.Where(f => File.Exists(f) || Directory.Exists(f)).ToArray();
			//有効ファイルがある場合、ジップを行う
			if (filesName.Length != 0)
			{
				ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFileName));
				s.SetLevel(6);
				if (kbn == "PC")
				{
					//ジップファイル作成
					ZipFileDictory(filesName, s, "/");
				}
				else
				{
					//ジップファイル作成
					ZipFileDictory(filesName, s, "/");
				}
				s.Finish();
				s.Close();

				return true;
			}
			else
			{
				MessageBox.Show(Msg.msgNoEffectiveFile);
				return false;
			}
		}

		private static void ZipFileDictory(string[] filesName, ZipOutputStream s, string folderName,string kbn)
		{
			ZipEntry entry = null;
			FileStream fs = null;
			Crc32 crc = new Crc32();
			try
			{
				int projNums = 1;
				int dirNums = 1;
				int boardNums = 1;
				for (int i = 0; i < filesName.Length; i++)
				{
					//ファイルのしょの所属フォルダ作成
					string file = filesName[i];
					entry = new ZipEntry(folderName);
					s.PutNextEntry(entry);
					s.Flush();
					//該当ファイル名がファイルかフォルダかを判断する
					if (Directory.Exists(file))
					{
						//フォルダの場合
						string tmpFilePath = file;
						//黒板及び黒板画像を取得する
						string[] tmpFileName = Directory.GetFileSystemEntries(tmpFilePath, "BD*");
						if (kbn == "PC")
						{
							if (!tmpFilePath[tmpFilePath.Length - 1].Equals("/"))
							{
								tmpFilePath = (tmpFilePath + "/").Replace("\\", "/".ToString());
							}
							tmpFilePath = tmpFilePath.Substring(Para.path.Replace("\\", "/".ToString()).Length + 1);
						}
						else
						{
							if (!tmpFilePath[tmpFilePath.Length - 1].Equals("/"))
							{
								tmpFilePath = tmpFilePath.Replace("\\", "/".ToString());
							}
							if (dirNums < 10)
							{
								tmpFilePath = "project" + dirNums.ToString("00") + "/";
								dirNums++;
							}
							else
							{
								tmpFilePath = "project" + dirNums.ToString() + "/";
								dirNums++;
							}

						}
						
						tmpFileName = GetCompleteBD(tmpFileName);
						tmpFileName = tmpFileName.Where(f => File.Exists(f)).ToArray();
						if (tmpFileName.Length > 0)
						{
							if (kbn == "PC")
							{
								//黒板がある場合、再帰を呼び出す
								ZipFileDictory(tmpFileName, s, tmpFilePath,"PC");
							}
							else
							{
								//黒板がある場合、再帰を呼び出す
								ZipFileDictory(tmpFileName, s, tmpFilePath, "IPAD");
							}
							
						}
						else
						{
							entry = new ZipEntry(tmpFilePath);
							s.PutNextEntry(entry);
							s.Flush();
						}
					}
					else
					{
						string tempFile = string.Empty;
						fs = File.OpenRead(file);
						byte[] buffer = new byte[fs.Length];
						fs.Read(buffer, 0, buffer.Length);
						tempFile = Path.GetFileName(file);
						if (kbn == "IPAD")
						{
							if (tempFile.Contains("project"))
							{
								if (projNums < 10)
								{
									tempFile = "project" + projNums.ToString("00") + ".xml";
								}
								else
								{
									tempFile = "project" + projNums.ToString() + ".xml";
								}
								projNums++;
							}
							else if (tempFile.Contains("BD"))
							{
								if (boardNums < 10000)
								{
									tempFile = "BD" + boardNums.ToString("0000") + ".xml";
								}
								else
								{
									tempFile = "BD" + boardNums.ToString() + ".xml";
								}
								boardNums++;
							}
						}
						if (!folderName.Equals("/"))
						{
							tempFile = folderName + tempFile;
						}
						entry = new ZipEntry(tempFile);
						entry.DateTime = DateTime.Now;
						entry.Size = fs.Length;
						fs.Close();
						crc.Reset();
						crc.Update(buffer);
						entry.Crc = crc.Value;
						s.PutNextEntry(entry);
						s.Write(buffer, 0, buffer.Length);
					}
				}
			}
			finally
			{
				if (fs != null)
				{
					fs.Close();
					fs = null;
				}
				if (entry != null)
					entry = null;
				GC.Collect();
			}
		}

解压zip文件的代码如下,解压文件fileToUnzip到zipFolder路径下:

public static bool UnZip(string fileToUnZip, string zipedFolder)
		{
			bool result = true;
			FileStream fs = null;
			ZipInputStream zipStream = null;
			ZipEntry ent = null;
			string fileName;

			if (!File.Exists(fileToUnZip))
				return false;

			if (!Directory.Exists(zipedFolder))
				Directory.CreateDirectory(zipedFolder);

			try
			{
				zipStream = new ZipInputStream(File.OpenRead(fileToUnZip));
				while ((ent = zipStream.GetNextEntry()) != null)
				{
					if (!string.IsNullOrEmpty(ent.Name))
					{
						fileName = Path.Combine(zipedFolder, ent.Name);
						fileName = fileName.Replace('/', '\\');

						if (fileName.EndsWith("\\"))
						{
							Directory.CreateDirectory(fileName);
							continue;
						}

						fs = File.Create(fileName);
						int size = 2048;
						byte[] data = new byte[size];
						while (true)
						{
							size = zipStream.Read(data, 0, data.Length);
							if (size > 0)
								fs.Write(data, 0, size);
							else
								break;
						}
						fs.Close();
					}
				}
			}
			catch
			{
				result = false;
			}
			finally
			{
				if (fs != null)
				{
					fs.Close();
					fs.Dispose();
				}
				if (zipStream != null)
				{
					zipStream.Close();
					zipStream.Dispose();
				}
				if (ent != null)
				{
					ent = null;
				}
				GC.Collect();
				GC.Collect(1);
			}
			return result;
		}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值