您的位置:首页 > 编程语言 > C#

(C#)文件操作--解压文件夹和文件

2011-01-10 15:35 295 查看
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;

namespace DataReportedFeedBack
{
public class UnZipClass
{

public void UnZip(string[] args)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));

ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(args[1]);
string fileName = Path.GetFileName(theEntry.Name);

//生成解压目录
// create directory
if (directoryName != "")
{
Directory.CreateDirectory(directoryName);
}
if (fileName != String.Empty)
{
//if (theEntry.Name.IndexOf(".ini") < 0)
//{
string fullPath = directoryName + "//" + theEntry.Name;
fullPath = fullPath.Replace("// ", "//");
string fullDirPath = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
FileStream streamWriter = File.Create(fullPath);
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 void button3_Click(object sender, EventArgs e)
{

string[] FileProperties = new string[2];

待解压的文件

FileProperties[0] = strToBak1Directory + "//" + f.Name;

解压后放置的目标目录
FileProperties[1] = strTempDiretory+"//";

UnZipClass UnZc = new UnZipClass();
UnZc.UnZip(FileProperties);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: