.net操作压缩文件

附件:SharpZipLib.zip


public class UnZipClass//解压
{
/// <summary>
/// 解压功能(解压压缩文件到指定目录)
/// </summary>
/// <param name="FileToUpZip">待解压的文件</param>
/// <param name="ZipedFolder">指定解压目标目录</param>
public static void UnZip(string FileToUpZip, string ZipedFolder, string Password)
{
if (!File.Exists(FileToUpZip))
{
MessageBox.Show("不存在压缩包");
return;
}

if (!Directory.Exists(ZipedFolder))
{
Directory.CreateDirectory(ZipedFolder);
}

ZipInputStream s = null;
ZipEntry theEntry = null;

string fileName;
FileStream streamWriter = null;
try
{
s = new ZipInputStream(File.OpenRead(FileToUpZip));
s.Password = Password;
while ((theEntry = s.GetNextEntry()) != null)
{
if (theEntry.Name != String.Empty)
{
fileName = Path.Combine(ZipedFolder, theEntry.Name);
///判断文件路径是否是文件夹
if (fileName.EndsWith("/") || fileName.EndsWith("\\"))
{
Directory.CreateDirectory(fileName);
continue;
}

streamWriter = File.Create(fileName);
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;
}
}
}
}
}
catch
{
MessageBox.Show("不能读取压缩包,请尝试输入口令", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
finally
{
if (streamWriter != null)
{
streamWriter.Close();
streamWriter = null;
}
if (theEntry != null)
{
theEntry = null;
}
if (s != null)
{
s.Close();
s = null;
}
GC.Collect();
GC.Collect(1);
}
}
}

/// <summary>
/// 压缩文件
/// </summary>
public class ZipHelper
{
#region 压缩

/// <summary>
/// 递归压缩文件夹的内部方法
/// </summary>
/// <param name="folderToZip">要压缩的文件夹路径</param>
/// <param name="zipStream">压缩输出流</param>
/// <param name="parentFolderName">此文件夹的上级文件夹</param>
/// <returns></returns>
private static bool ZipDirectory(string folderToZip, ZipOutputStream zipStream, string parentFolderName)
{
bool result = true;
string[] folders, files;
ZipEntry ent = null;
FileStream fs = null;
Crc32 crc = new Crc32();

try
{
ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/"));
zipStream.PutNextEntry(ent);
zipStream.Flush();

files = Directory.GetFiles(folderToZip);
foreach (string file in files)
{
fs = File.OpenRead(file);

byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/" + Path.GetFileName(file)));
ent.DateTime = DateTime.Now;
ent.Size = fs.Length;

fs.Close();

crc.Reset();
crc.Update(buffer);

ent.Crc = crc.Value;
zipStream.PutNextEntry(ent);
zipStream.Write(buffer, 0, buffer.Length);
}

}
catch
{
result = false;
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
if (ent != null)
{
ent = null;
}
GC.Collect();
GC.Collect(1);
}

folders = Directory.GetDirectories(folderToZip);
foreach (string folder in folders)
if (!ZipDirectory(folder, zipStream, folderToZip))
return false;

return result;
}

/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="folderToZip">要压缩的文件夹路径</param>
/// <param name="zipedFile">压缩文件完整路径</param>
/// <param name="password">密码</param>
/// <returns>是否压缩成功</returns>
public static bool ZipDirectory(string folderToZip, string zipedFile, string password)
{
bool result = false;
if (!Directory.Exists(folderToZip))
return result;

ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile));
zipStream.SetLevel(6);
if (!string.IsNullOrEmpty(password)) zipStream.Password = password;

result = ZipDirectory(folderToZip, zipStream, "");

zipStream.Finish();
zipStream.Close();

return result;
}

/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="folderToZip">要压缩的文件夹路径</param>
/// <param name="zipedFile">压缩文件完整路径</param>
/// <returns>是否压缩成功</returns>
public static bool ZipDirectory(string folderToZip, string zipedFile)
{
bool result = ZipDirectory(folderToZip, zipedFile, null);
return result;
}

/// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileToZip">要压缩的文件全名</param>
/// <param name="zipedFile">压缩后的文件名</param>
/// <param name="password">密码</param>
/// <returns>压缩结果</returns>
public static bool ZipFile(string fileToZip, string zipedFile, string password)
{
bool result = true;
ZipOutputStream zipStream = null;
FileStream fs = null;
ZipEntry ent = null;

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

try
{
fs = File.OpenRead(fileToZip);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();

fs = File.Create(zipedFile);
zipStream = new ZipOutputStream(fs);
if (!string.IsNullOrEmpty(password)) zipStream.Password = password;
ent = new ZipEntry(Path.GetFileName(fileToZip));
zipStream.PutNextEntry(ent);
zipStream.SetLevel(6);

zipStream.Write(buffer, 0, buffer.Length);

}
catch
{
result = false;
}
finally
{
if (zipStream != null)
{
zipStream.Finish();
zipStream.Close();
}
if (ent != null)
{
ent = null;
}
if (fs != null)
{
fs.Close();
fs.Dispose();
}
}
GC.Collect();
GC.Collect(1);

return result;
}

/// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileToZip">要压缩的文件全名</param>
/// <param name="zipedFile">压缩后的文件名</param>
/// <returns>压缩结果</returns>
public static bool ZipFile(string fileToZip, string zipedFile)
{
bool result = ZipFile(fileToZip, zipedFile, null);
return result;
}

/// <summary>
/// 压缩文件或文件夹
/// </summary>
/// <param name="fileToZip">要压缩的路径</param>
/// <param name="zipedFile">压缩后的文件名</param>
/// <param name="password">密码</param>
/// <returns>压缩结果</returns>
public static bool Zip(string fileToZip, string zipedFile, string password)
{
bool result = false;
if (Directory.Exists(fileToZip))
result = ZipDirectory(fileToZip, zipedFile, password);
else if (File.Exists(fileToZip))
result = ZipFile(fileToZip, zipedFile, password);

return result;
}

/// <summary>
/// 压缩文件或文件夹
/// </summary>
/// <param name="fileToZip">要压缩的路径</param>
/// <param name="zipedFile">压缩后的文件名</param>
/// <returns>压缩结果</returns>
public static bool Zip(string fileToZip, string zipedFile)
{
bool result = Zip(fileToZip, zipedFile, null);
return result;

}

}

时间: 2024-08-07 09:06:16

.net操作压缩文件的相关文章

利用System.IO.Compression操作压缩文件

引用: using System.IO.Compression; using (FileStream zipToOpen = new FileStream(@"D:\json.zip", FileMode.Open)) { using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update)) { ZipArchiveEntry readmeEntry = archive.Entries[0]; usi

C# 创建压缩文件

出处:http://www.cnblogs.com/sparkdev/ 在程序中对文件进行压缩解压缩是很重要的功能,不仅能减小文件的体积,还能对文件起到保护作用.如果是生成用户可以下载的文件,还可以极大的减少网络流量并提升下载速度.最近在一个 C# 项目中用到了创建压缩文件的功能,在此和同学们分享一下使用心得. SharpZipLib 库 既然是很重要的用能,那么如果每个人在使用的时候都去用基本的 API 去实现一遍显然不符合效率至上的生产要求.作为比较有经验的开发人员相信您一定会在第一时间去搜

dedecms中提取的zip压缩文件操作类zip.class.php

从织梦DeDeCMS中提取的zip压缩文件操作类,包含zip文件压缩.解压缩.添加文件到压缩包中等多个实用的函数,注释详细方便使用. 下载:dedecms中提取的zip压缩文件操作类zip.class.php 包含的函数和简单的使用方法: 1.函数get_List($zip_name) ,函数作用:获取zip文件中的文件列表.函数参数 $zip_name  zip文件名.返回值 文件列表数组. 2.函数Add($files,$compact),函数作用:增加文件到压缩文件.函数参数 $files

使用commons-compress操作zip文件(压缩和解压缩)

http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可以操作ar, cpio, Unix dump, tar, zip, gzip, XZ, Pack200 and bzip2格式的文件,功能比较强大. 在这里写两个用Commons Compress把文件压缩成zip和从zip解压缩的方法. 直接贴上工具类代码: /** * Zip文件工具类 * @a

c#操作Zip压缩文件

SharpZipLib 文件/文件夹压缩 一.ZipFile ZipFile类用于选择文件或文件夹进行压缩生成压缩包. 常用属性: 属性 说明 Count 文件数目(注意是在ComitUpdat之后才有) Password 压缩包密码 Size 压缩包占用空间大小 Name 压缩包名称,默认输出是文件路径 ZipEntry 压缩包里的文件,通过索引[]访问 其常用方法如下: 方法 说明 Add 添加要进行压缩的文件 AddDirectory 添加文件夹(不会压缩文件夹里的文件) Delete 删

C#利用SharpZipLib解压或压缩文件夹实例操作

最近要做一个项目涉及到C#中压缩与解压缩的问题的解决方法,大家分享. 这里主要解决文件夹包含文件夹的解压缩问题. )下载SharpZipLib.dll,在http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx中有最新免费版本,“Assemblies for .NET 1.1, .NET 2.0, .NET CF 1.0, .NET CF 2.0: Download [297 KB] ”点击Download可以下载,解压后里边

Linux操作系统基础操作之文件压缩

压缩格式(扩展名).Z:compress程序压缩的文件.tar: tar程序打包数据,并未压缩.gz :gzip程序压缩的文件.tar.gz:tar程序打包文件,并且经过gzip的压缩.bz2:bzip2程序压缩的文件.tar.bz2:bzip2压缩,并使用tar打包的文件根据后缀名称获取压缩格式,从而知道如何解压缩  compress命令 compress [-dfr] filename 解压或压缩后缀.Z的文件,多用于UNIX -d:解压缩参数 -f:强制压缩文件 -r:针对目录,将目录下的

nodejs用archiver模块压缩文件

router.get('/download/qiantu',function(req,res){ var dirname = (new Date()).getTime() var zippath = './down/'+dirname+'.zip' var output = fs.createWriteStream(zippath) var ziparchiver = archiver('zip',{ store:true }) ziparchiver.pipe(output) async.ma

Linux下的压缩文件剖析 (tar/gzip的区别)

Linux下的压缩文件剖析 对于刚刚接触Linux的人来说,一定会给Linux下一大堆各式各样的文件名给搞晕.别个不说,单单就压缩文件为例,我们知道在Windows下最常见的压缩文件就只有两种,一是,zip,另一个是.rap.可是Linux就不同了,它有.gz..tar.gz.tgz.bz2..Z..tar等众多的压缩文件名,此外windows下的.zip和.rar也可以在Linux下使用,不过在Linux使用.zip和.rar的人就太少了.本文就来对这些常见的压缩文件进行一番小结,希望你下次遇