zoukankan      html  css  js  c++  java
  • 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);
            }
    复制代码
  • 相关阅读:
    刷新dbgrid 而不失去当前行位置
    Delphi Code Editor 之 快捷菜单
    Delphi Code Editor 之 几个特性(转)
    Delphi DBGrid双击事件、单元格操作
    在DBGrid中可选中行而又可进入编辑状态
    如何在DBGrid中能支持多项记录的选择
    在Delphi中如何获得SQL中存储过程的返回值?
    Delphi:ADOConnection连接SQLServer自动断网问题解决
    ADO.NET基础必备之SqlCommand.Execute三方法
    Delphi中exit、break、continue等跳出操作的区别
  • 原文地址:https://www.cnblogs.com/KingsLiu/p/6479156.html
Copyright © 2011-2022 走看看