📄 zip.cs
字号:
using System;
using System.IO;
namespace Compression
{
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
public class Zip
{
private int _compressionLevel = 1;
private int _blockSize = 2048;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="CompressionLevel">压缩级别</param>
/// <param name="BlockSize">块大小</param>
public Zip(int CompressionLevel, int BlockSize)
{
_compressionLevel = CompressionLevel;
_blockSize = BlockSize;
}
public void ZipDirectory(string SourcePath, string TargetFile)
{
string[] filenames = Directory.GetFiles(SourcePath);
Crc32 crc = new Crc32();
ZipOutputStream s = new ZipOutputStream(File.Create(TargetFile));
s.SetLevel(6); // 0 - store only to 9 - means best compression
foreach (string file in filenames)
{
//打开压缩文件
FileStream fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(file);
entry.DateTime = DateTime.Now;
// set Size and the crc, because the information
// about the size and crc should be stored in the header
// if it is not set it is automatically written in the footer.
// (in this case size == crc == -1 in the header)
// Some ZIP programs have problems with zip files that don't store
// the size and crc in the header.
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
s.Finish();
s.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -