本文主要是介绍(转载)C#利用ICSharpCode.SharpZipLib.dll压缩文件和解压文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这两个类是从网上找来的,修改了一下,改为自己所需要的,测试通过.压缩后的文件为.zip
感觉还不错,所以转载一下与大家共享
/// <summary>
/// 压缩文件
/// </summary>
using System;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
namespace FtpResume.Utility
{
public class ZipClass
{
public string cutStr = "";
public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
{
//如果文件没有找到则报错。
if (!System.IO.File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
}
System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
ZipEntry ZipEntry = new ZipEntry("ZippedFile");
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(CompressionLevel);
byte[] buffer = new byte[BlockSize];
System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, size);
try
{
while (size < StreamToZip.Length)
{
int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, sizeRead);
size += sizeRead;
}
}
catch (System.Exception ex)