/// 解压文件
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 DeCompression
{
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);
//生成解压目录
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();
}
}
}
/**//// <summary>
/// 调用源码
/// </summary>
private void button2_Click(object sender, System.EventArgs e)
{
string []FileProperties=new string[2];
FileProperties[0]=”C:\zip\test.zip”;//待解压的文件
FileProperties[1]=”C:\unzipped”;//解压后放置的目标目录
UnZipClass UnZc=new UnZipClass();
UnZc.UnZip(FileProperties);
}
FileProperties[1]=”C:\unzipped”;//解压后放置的目标目录
这不行吧,
FileProperties[0]=@”C:\zip\test.zip”;//待解压的文件
FileProperties[1]=@”C:\unzipped”;//解压后放置的目标目录
跟哪个没关系,我改过了
/// 解压指定的zip文件
/// </summary>
/// <param name=”filename”>文件全路径名称</param>
/// <param name=”targetPath”>解压后文件保存的路径名全称,必须带 “\”</param>
public static void GetZipEntry(string filename,string targetPath)
{
// 创建读取Zip文件对象
ZipInputStream zos = new ZipInputStream(File.OpenRead(filename));
// Zip文件中的每一个文件
ZipEntry theEntry;
// 循环读取Zip文件中的每一个文件
while ((theEntry = zos.GetNextEntry()) != null)
{
string directoryName = targetPath;
string fileName = Path.GetFileName(theEntry.Name);
// create directory
if(!Directory.Exists(directoryName))
Directory.CreateDirectory(directoryName);
if (fileName != String.Empty)
{
// 解压文件
FileStream streamWriter = File.Open(directoryName + fileName,FileMode.Create);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
// 写入数据
size = zos.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break; }
}
streamWriter.Close();
}
}
zos.Close();
}