濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 基于.NET 4.5 壓縮的使用

基于.NET 4.5 壓縮的使用

熱門(mén)標(biāo)簽:怎樣在地圖標(biāo)注消火栓圖形 廈門(mén)四川外呼系統(tǒng) 山東防封電銷(xiāo)卡辦理套餐 內(nèi)蒙古智能電銷(xiāo)機(jī)器人哪家強(qiáng) 百度地圖標(biāo)注點(diǎn)擊事件 杭州智能電話機(jī)器人 地圖標(biāo)注位置多的錢(qián) 泰州手機(jī)外呼系統(tǒng)軟件 濟(jì)源人工智能電話機(jī)器人價(jià)格

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

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

復(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)潔了很多呢?

標(biāo)簽:喀什 朔州 洛陽(yáng) 百色 臺(tái)州 新鄉(xiāng) 周口 朝陽(yáng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于.NET 4.5 壓縮的使用》,本文關(guān)鍵詞  基于,.NET,4.5,壓縮,的,使用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《基于.NET 4.5 壓縮的使用》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于基于.NET 4.5 壓縮的使用的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    邹平县| 洛隆县| 贺州市| 灵川县| 浦东新区| 湛江市| 江口县| 滕州市| 林芝县| 巢湖市| 上饶市| 玉林市| 眉山市| 吴桥县| 常德市| 长乐市| 台前县| 濮阳市| 邛崃市| 腾冲县| 嵊州市| 武隆县| 西藏| 临朐县| 太保市| 平原县| 灯塔市| 建水县| 文山县| 开鲁县| 岳池县| 福海县| 资源县| 开平市| 大同市| 治县。| 红桥区| 佛教| 台北县| 天峻县| 泉州市|