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

使用ICSharpCode.SharpZipLib对文件进行压缩或解压

2010-01-07 23:44 609 查看
简介: #ziplib(SharpZipLib,原名NZipLib)是zip,gzip,rar和bzip2压缩库
完全用C#语言编写的。NET平台。
这是作为一个程序集(在GAC中安装),因此可以很容易地被纳入其他项目(在任何。NET语言)。
在#ziplib这样说的创造者:“我将zip库移植到C#多,因为我需要
gzip压缩/ zip压缩,我不希望使用libzip.dll或这样的事情。我希望所有的纯C#。“



下载链接

用于。NET 1.1组件,。NET 2.0中的。NET参看1.0的。NET参看2.0: 下载
[228 KB]


源代码和示例 下载
[499 KB]


帮助文件 下载
[1157 KB]

压缩方法(一)

/// <summary>

/// 压缩zip文件

/// </summary>

/// <param name="args"></param>

void Official(string[] args)

{

// Perform some simple parameter checking. More could be done

// like checking the target file name is ok, disk space, and lots

// of other things, but for a demo this covers some obvious traps.

//if ( args.Length < 2 ) {

// Console.WriteLine("Usage: CreateZipFile Path ZipFile");

// return;

//}

//if ( !Directory.Exists(args[0]) ) {

// Console.WriteLine("Cannot find directory '{0}'", args[0]);

// return;

//}

try

{

// Depending on the directory this could be very large and would require more attention

// in a commercial package.

string[] filenames = Directory.GetFiles("c://zip");//args[0] 压缩文件目录

// 'using' statements gaurantee the stream is closed properly which is a big source

// of problems otherwise. Its exception safe as well which is great.

using (ZipOutputStream s = new ZipOutputStream(File.Create("c://bb.zip"))) //args[1] 压缩文件名称

{

s.SetLevel(9); // 0 - store only to 9 - means best compression

byte[] buffer = new byte[4096];

foreach (string file in filenames)

{

// Using GetFileName makes the result compatible with XP

// as the resulting path is not absolute.

ZipEntry entry = new ZipEntry(Path.GetFileName(file));

// Setup the entry data as required.

// Crc and size are handled by the library for seakable streams

// so no need to do them here.

// Could also use the last write time or similar for the file.

entry.DateTime = DateTime.Now;

s.PutNextEntry(entry);



using (FileStream fs = File.OpenRead(file))

{

// Using a fixed size buffer here makes no noticeable difference for output

// but keeps a lid on memory usage.

int sourceBytes;

do

{

sourceBytes = fs.Read(buffer, 0, buffer.Length);

s.Write(buffer, 0, sourceBytes);

} while (sourceBytes > 0);

}

}

// Finish/Close arent needed strictly as the using statement does this automatically

// Finish is important to ensure trailing information for a Zip file is appended. Without this

// the created file would be invalid.

s.Finish();

// Close is important to wrap things up and unlock the file.

s.Close();

}

}

catch (Exception ex)

{

Console.WriteLine("Exception during processing {0}", ex);

// No need to rethrow the exception as for our purposes its handled.

}

}

压缩方法(二)

//SrcFile 源文件路径,DstFile 压缩后目标文件的路径,BufferSize 缓冲期大小

public static bool ZipFiles(string[] SrcFile, string DstFile, int BufferSize)

{

try

{

//检测压缩文件是否已存在

if (File.Exists(DstFile))

{

File.Delete(DstFile);

Thread.Sleep(100);//

}

FileStream fileStreamIn = null;//源文件流

FileStream fileStreamOut = new FileStream(DstFile, FileMode.Create, FileAccess.Write);//创建压缩文件夹流

ZipOutputStream zipOutStream = new ZipOutputStream(fileStreamOut);//给压缩对象填充指定流

byte[] buffer = new byte[BufferSize];//缓冲大小

ZipEntry entry = null;

foreach (string file in SrcFile)

{

if (file != string.Empty)

try

{

fileStreamIn = new FileStream(file, FileMode.Open, FileAccess.Read);//创建压缩文件流对象

entry = new ZipEntry(Path.GetFileName(file));//获取压缩对象

zipOutStream.PutNextEntry(entry);//压缩

int size = 0;

do

{

size = fileStreamIn.Read(buffer, 0, buffer.Length);

zipOutStream.Write(buffer, 0, size);

}

while (size > 0);

}

finally

{

fileStreamIn.Close();

}

}

zipOutStream.Close();

fileStreamOut.Close();

return true;

}

catch (Exception ex)

{

return false;

throw ex;

}

}



解压方法

void unZip()

{

// Perform simple parameter checking.

//if ( args.Length < 1 ) {

// Console.WriteLine("Usage UnzipFile NameOfFile");

// return;

//}



//if ( !File.Exists(args[0]) ) {

// Console.WriteLine("Cannot find file '{0}'", args[0]);

// return;

//}

string ss="C://Documents and Settings//Administrator//桌面//SharpZipLib_0855_SourceSamples.zip";

using (ZipInputStream s = new ZipInputStream(File.OpenRead(ss))) {



ZipEntry theEntry;

while ((theEntry = s.GetNextEntry()) != null) {



Console.WriteLine(theEntry.Name);



string directoryName = Path.GetDirectoryName(theEntry.Name);

string fileName = Path.GetFileName(theEntry.Name);



// create directory

if ( directoryName.Length > 0 ) {

Directory.CreateDirectory(directoryName);

}



if (fileName != String.Empty) {

using (FileStream streamWriter = File.Create(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;

}

}

}

}

}

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