好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

.NET 4.5 压缩

.NET 4.5 压缩

.NET 4.5 压缩

 在.NET 4.5中新加入的压缩的命名空间和方法。可以抛弃ICSharpCode.SharpZipLib.dll 这个类库了。性能上不相上下。但是能够大大简化你的代码。如果开始使用.NET FrameWork4.5 做压缩不妨试试自带的压缩方法.

传统使用ICSharpCode.SharpZipLib.dll 所写的代码。

 static   void  Main( string  [] args)
        {
            Stopwatch watch  =  new   Stopwatch();
            watch.Start();
              string  path =  @"  E:\  "  ;        
            Compress(Directory.GetFiles(path),   @"  F:\4.0.zip  "  );
            watch.Stop();
            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)
        {
            Stopwatch watch  =  new   Stopwatch();
            watch.Start();
              string  path =  @"  E:\  "  ;
            Compress(path,   @"  F:\4.5.zip  "  );
            watch.Stop();
            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  );
        } 


怎么样代码是不是简洁了很多呢?

作者: risk
出处: http://www.cnblogs.com/risk
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

分类:  .NET

标签:  .NET 压缩 .net 4.5

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于.NET 4.5 压缩的详细内容...

  阅读:49次