调用类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
namespace s
{
public class ssss
{
public int avg = 1024 * 1024 * 100;//100MB写一次
#region 压缩文件 和 文件夹
/// <summary>
/// 压缩文件 和 文件夹
/// </summary>
/// <param name="FileToZip">待压缩的文件或文件夹,全路径格式</param>
/// <param name="ZipedFile">压缩后生成的压缩文件名,全路径格式</param>
/// <returns>压缩是否成功</returns>
public bool Zip(string FileToZip, string ZipedFile)
{
return Zip(FileToZip, ZipedFile, "");
}
/// <summary>
/// 压缩文件 和 文件夹,不压缩顶级目录
/// </summary>
/// <param name="FolderToZip">待压缩的文件夹,全路径格式</param>
/// <param name="ZipedFile">压缩后生成的压缩文件名,全路径格式</param>
/// <returns>压缩是否成功</returns>
public bool ZipNo(string FolderToZip, string ZipedFile)
{
if (!Directory.Exists(FolderToZip))
return false;
if (ZipedFile == string.Empty)
{
//如果为空则文件名为待压缩的文件名加上.rar
ZipedFile = FolderToZip + ".zip";
}
ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));
s.SetLevel(6);
string[] filenames = Directory.GetFiles(FolderToZip);
ZipEntry entry = null;
FileStream fs = null;
Crc32 crc = new Crc32();
foreach (string file in filenames)
{
//压缩文件
fs = File.OpenRead(file);
byte[] buffer = new byte[avg];
entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
s.PutNextEntry(entry);
for (int i = 0; i < fs.Length; i += avg)
{
if (i + avg > fs.Length)
{
//不足100MB的部分写剩余部分
buffer = new byte[fs.Length - i];
}
fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, buffer.Length);
}
}
if (fs != null)
{
fs.Close();
fs = null;
}
if (entry != null)
entry = null;
GC.Collect();
GC.Collect(1);
//压缩目录
string[] folders = Directory.GetDirectories(FolderToZip);
foreach (string folder in folders)
{