教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 C# 实现文件的压缩与解压缩的代码

C# 实现文件的压缩与解压缩的代码

发布时间:2016-03-23   编辑:jiaochengji.com
本文介绍下C#实现文件压缩与文件解压缩的代码,有需要的朋友,可以参考下。

说明:
引用 using ICSharpCode.SharpZipLib.Zip;

代码如下:
 

复制代码 代码示例:

//调用:CreateZipFile("文件路径","压缩路径")
private static void CreateZipFile(string filesPath, string zipFilePath)
{
if (!Directory.Exists(filesPath))
{
MessageBox.Show("找不到 " + filesPath);
return;
}
try
{
string[] filenames = Directory.GetFiles(filesPath);
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
{
s.SetLevel(9); // 压缩级别 0-9
//s.Password = "123"; //Zip压缩文件密码
byte[] buffer = new byte[4096]; //缓冲区大小
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
s.Finish();
s.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}

//UnZipFile("压缩路径","文件路径")
private static void UnZipFile(string zipFilePath, string unZipFilePath)
{
if (!File.Exists(zipFilePath))
{
MessageBox.Show(zipFilePath + " 不存在!");
return;
}

using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{
ZipEntry theEntry;
string directoryName = Path.GetDirectoryName(unZipFilePath);
// create directory
if (directoryName.Length > 0)
{
Directory.CreateDirectory(directoryName);
}

while ((theEntry = s.GetNextEntry()) != null)
{
//string fileName = Path.GetFileName(theEntry.Name);
string fileName = Path.Combine(unZipFilePath,theEntry.Name);

if (fileName != String.Empty)
{
using (FileStream 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;
}
}

streamWriter.Close();
}
}
}

s.Close();
}
}

附,使用ICSharpCode SharpZipLib进行压缩与解压缩文件或文件夹的代码。

1、解压缩代码
 

复制代码 代码示例:

using System;
using System.Collections.Generic;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;

namespace DRMEncryption
{
/// ;summary;
/// UnZip 类用于解压缩一个 zip 文件。
/// ;/summary;
public class UnZipDir
{

//解压缩以后的文件名和路径,压缩前的路径
public static Boolean UNZipFile(string FileToZip, string ZipedFile)
{
try
{
FastZip fastZip = new FastZip();
fastZip.ExtractZip(FileToZip, ZipedFile, "");
return true;
}
catch {
return false;
}
}
}
}

2、压缩文件的代码
 

复制代码 代码示例:

using System;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
namespace DRMEncryption
{
public class ZipClass
{

public static Boolean ZipFile(string FileToZip, string ZipedFile)
{
try
{
FastZip fastZip = new FastZip();

bool recurse = true;

//压缩后的文件名,压缩目录 ,是否递归
fastZip.CreateZip(FileToZip, ZipedFile, recurse, "");
return true;
}
catch { return false; }
}
}
}
 

说明:
那个返回方法是用来另外一个页面判断解压是否成功用的,可以不要;
解压缩:UnZipDir.UNZipFile(文件名, 路径);
压缩:ZipClass.ZipFile(文件名, 路径);

就是这些了,希望有需要的朋友,可以从中受益,呵呵。

您可能感兴趣的文章:
PHP开启gzip压缩的二种方法
IIS压缩及性能优化
asp.net中如何用GZip压缩和解压
php压缩与解压缩类PclZip的例子
Linux 指令:备份压缩--lha
RAR参数详解
C# 实现文件的压缩与解压缩的代码
压缩文件并以日期格式命名的shell脚本
PHP压缩解压缩类PclZip用法教程
php在线压缩与解压缩SWF文件的代码(修正版)

[关闭]
~ ~