主頁 > 知識庫 > 基于.NET 4.5 壓縮的使用

基于.NET 4.5 壓縮的使用

熱門標簽:Mysql連接數(shù)設置 服務器配置 阿里云 團購網(wǎng)站 銀行業(yè)務 科大訊飛語音識別系統(tǒng) Linux服務器 電子圍欄

在.NET 4.5中新加入的壓縮的命名空間和方法。可以拋棄ICSharpCode.SharpZipLib.dll 這個類庫了。性能上不相上下。但是能夠大大簡化你的代碼。如果開始使用.NET FrameWork4.5 做壓縮不妨試試自帶的壓縮方法.

傳統(tǒng)使用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);
        }

怎么樣代碼是不是簡潔了很多呢?

標簽:棗莊 萍鄉(xiāng) 衡水 衢州 廣元 江蘇 大理 蚌埠

巨人網(wǎng)絡通訊聲明:本文標題《基于.NET 4.5 壓縮的使用》,本文關鍵詞  ;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266