基于.NET 4.5 压缩的使用
在.NET 4.5中新加入的压缩的命名空间和方法。可以抛弃ICSharpCode.SharpZipLib.dll 这个类库了。性能上不相上下。能够大大简化你的代码。如果开始使用.NET FrameWork4.5 做压缩不妨试试自带的压缩方法.
传统使用ICSharpCode.SharpZipLib.dll 所写的代码。
static void Main(string[] args)
{
Swatch watch = new Swatch();
watch.Start();
string path = @"E:\";
Compress(Directory.GetFiles(path), @"F:\4.0.zip");
watch.S();
Console.WriteLine("消耗时间:{0}", watch.ElapsedMilliseconds);
FileInfo f = new FileInfo(@"F:\4.0.zip");
Console.WriteLine("文件大小{0}", f.Length);
}
static void Compress(string[] filePaths, string zipFilePath)
{
byte[] _buffer = new byte[4096];
if (!Directory.Exists(zipFilePath))
Directory.CreateDirectory(Path.GetDirectoryName(zipFilePath));
using (ZipOutputStream zip = new ZipOutputStream(File.Create(zipFilePath)))
{
foreach (var item in filePaths)
{
if (!File.Exists(item))
{
Console.WriteLine("the file {0} not exist!", item);
}
else
{
ZipEntry entry = new ZipEntry(Path.GetFileName(item));
entry.DateTime = DateTime.Now;
zip.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(item))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(_buffer, 0, _buffer.Length);
zip.Write(_buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
}
zip.Finish();
zip.Close();
}
}
使用.NET FrameWork 4.5中自带的压缩。
static void Main(string[] args)
{
Swatch watch = new Swatch();
watch.Start();
string path = @"E:\";
Compress(path, @"F:\4.5.zip");
watch.S();
Console.WriteLine("消耗时间:{0}", watch.ElapsedMilliseconds);
FileInfo f = new FileInfo(@"F:\4.5.zip");
Console.WriteLine("文件大小{0}", f.Length);
}
static void Compress(string filePath, string zipFilePath)
{
ZipFile.CreateFromDirectory(filePath, zipFilePath, CompressionLevel.Fastest, false);
}
怎么样代码是不是简洁了很多呢?
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指