/* ==============================================================================
* 功能描述:Compress
* 创 建 者:Garrett
* 创建日期:2019/2/14 15:04:11
* ==============================================================================*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ICSharpCode.SharpZipLib.Zip;
namespace PackageUploader.Compress
{
///
/// Compress
///
class CompressT
{
///
/// 压缩
///
/// 源目录
/// ZipOutputStream对象
public static void Compress(string basePath, string source, ZipOutputStream s, CompressArgs args)
{
string[] filenames = Directory.GetFileSystemEntries(source);
foreach (string file in filenames)
{
if (Directory.Exists(file))
{
Compress(basePath, file, s, args); //递归压缩子文件夹
}
else
{
using (FileStream fs = File.OpenRead(file))
{
byte[] buffer = new byte[4 * 1024];
ZipEntry entry = new ZipEntry(file.Replace(basePath, "")); //此处去掉盘符,如D:\123\1.txt 去掉D:
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
args.IncrementTransferred = sourceBytes;
} while (sourceBytes > 0);
}
}
}
}
}
}