最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于.NET 4.5 壓縮的使用

 更新時(shí)間:2013年04月19日 14:49:04   作者:  
本篇文章小編為大家介紹,基于.NET 4.5 壓縮的使用。需要的朋友參考下

在.NET 4.5中新加入的壓縮的命名空間和方法??梢話仐塈CSharpCode.SharpZipLib.dll 這個(gè)類庫了。性能上不相上下。但是能夠大大簡(jiǎn)化你的代碼。如果開始使用.NET FrameWork4.5 做壓縮不妨試試自帶的壓縮方法.

傳統(tǒng)使用ICSharpCode.SharpZipLib.dll 所寫的代碼。

復(fù)制代碼 代碼如下:

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("消耗時(shí)間:{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中自帶的壓縮。
復(fù)制代碼 代碼如下:

static void Main(string[] args)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            string path = @"E:\";
            Compress(path, @"F:\4.5.zip");
            watch.Stop();
            Console.WriteLine("消耗時(shí)間:{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);
        }

怎么樣代碼是不是簡(jiǎn)潔了很多呢?

相關(guān)文章

最新評(píng)論

宽城| 湟源县| 青州市| 保康县| 分宜县| 枝江市| 大悟县| 泊头市| 棋牌| 博野县| 天气| 武山县| 合江县| 灵宝市| 保康县| 高陵县| 贵州省| 电白县| 长海县| 同心县| 泰和县| 黄浦区| 石林| 靖边县| 桐乡市| 琼海市| 昌吉市| 武义县| 巴林右旗| 上高县| 丰都县| 阿城市| 都昌县| 抚松县| 开封县| 原阳县| 阿瓦提县| 鄂托克前旗| 隆化县| 曲松县| 普宁市|