C#使用SharpZipLib压缩和解压文件2/3解压类

using System;

using System.IO;

using ICSharpCode.SharpZipLib.Zip;

namespace DeCompression

{

/// <summary>

/// 解压文件

/// </summary>

public class UnZipClass

{

/// <summary>

/// 解压文件

/// </summary>

/// <param name="fileToUnZip">要解压的压缩文件</param>

/// <param name="unZipDestination">解压目标路径</param>

public static void UnZip(string fileToUnZip, string unZipDestination)

{

ZipInputStream inputStream = new ZipInputStream(File.OpenRead(fileToUnZip));

ZipEntry theEntry;

while ((theEntry = inputStream.GetNextEntry()) != null)

{

unZipDestination += "/";

string fileName = Path.GetFileName(theEntry.Name);

string path = Path.GetDirectoryName(unZipDestination + theEntry.Name) + "/";

Directory.CreateDirectory(path);//生成解压目录

if (fileName != String.Empty)

{

FileStream streamWriter = File.Create(path + fileName);//解压文件到指定的目录

int size = 2048;

byte[] data = new byte[2048];

while (true)

{

size = inputStream.Read(data, 0, data.Length);

if (size > 0)

{

streamWriter.Write(data, 0, size);

}

else

{

break;

}

}

streamWriter.Close();

}

}

inputStream.Close();

}

}

}

阅读更多

没有更多推荐了,返回首页