欢迎光临

木子屋

标题: SharpZipLib解压单个文件和文件夹

作者: dnawo

日期: 2010-11-02 10:32 AM

分类: Web编程

内容: 复制内容到剪贴板 程序代码using System;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ExtractFile(@"E:\mzwu.zip", @"code/default.htm");
ExtractFile(@"E:\mzwu.zip", "jquery-1.3.2.js", @"F:\a\b\c\d.js");
ExtractDirectory(@"E:\mzwu.zip");
ExtractDirectory(@"E:\mzwu.zip", @"E:\a\b\c\");
Console.WriteLine("ok");
Console.Read();
}
/// <summary>
/// 解压单个文件
/// </summary>
/// <param name="zipfile"></param>
/// <param name="sourcename"></param>
static void ExtractFile(string zipfile, string sourcename)
{
ExtractFile(zipfile, sourcename, null);
}
/// <summary>
/// 解压单个文件
/// </summary>
/// <param name="zipfile"></param>
/// <param name="sourcename"></param>
/// <param name="destname"></param>
static void ExtractFile(string zipfile, string sourcename, string destname)
{
ZipEntry entry;
byte[] b = new byte[1024];
int length;
using (ZipInputStream s = new ZipInputStream(File.Open(zipfile, FileMode.Open)))
{
while ((entry = s.GetNextEntry()) != null)
{
if (entry.IsFile && entry.Name.ToLower().Equals(sourcename.ToLower(), StringComparison.CurrentCultureIgnoreCase))
{
if (destname == null)
destname = zipfile.Replace(Path.GetFileName(zipfile), Path.GetFileName(entry.Name));
if(!Directory.Exists(Path.GetDirectoryName(destname)))
Directory.CreateDirectory(Path.GetDirectoryName(destname));
using (FileStream fs = File.Create(destname))
{
while ((length = s.Read(b, 0, b.Length)) > 0)
{
fs.Write(b, 0, length);
}
}
return;
}
}
}
}
/// <summary>
/// 解压文件夹
/// </summary>
/// <param name="zipfile"></param>
static void ExtractDirectory(string zipfile)
{
ExtractDirectory(zipfile, null);
}
/// <summary>
/// 解压文件夹
/// </summary>
/// <param name="zipfile"></param>
/// <param name="destpath"></param>
static void ExtractDirectory(string zipfile, string destpath)
{
ZipEntry entry;
string str;
byte[] b = new byte[1024];
int length;
if (destpath == null)
destpath = zipfile.Replace(Path.GetExtension(zipfile), "");
using (ZipInputStream s = new ZipInputStream(File.Open(zipfile, FileMode.Open)))
{
while ((entry = s.GetNextEntry()) != null)
{
str = Path.Combine(destpath, entry.Name);
if (entry.IsDirectory)
{
Directory.CreateDirectory(str);
}
else if (entry.IsFile)
{
Directory.CreateDirectory(Path.GetDirectoryName(str));
using (FileStream fs = File.Create(str))
{
while ((length = s.Read(b, 0, b.Length)) > 0)
{
fs.Write(b, 0, length);
}
}
}
}
}
}
}
}

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.133 ms

返回

标题: SharpZipLib解压单个文件和文件夹

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.133 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.168 ms

返回