2018年1月26日金曜日

C# (net framework 4.5以降) zip fileを扱う

using System;
using System.IO;
using System.IO.Compression; //このLibを使う

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string startPath = @"c:\example\start";
            string zipPath = @"c:\example\result.zip";
            string extractPath = @"c:\example\extract";

            ZipFile.CreateFromDirectory(startPath, zipPath);

            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    }
}

ちなみに、「@」を利用するとエスケープを行わずに「/」を文字列に含められるので、パスを指定したい場合などに可読性が高く、便利に使える。

0 件のコメント:

コメントを投稿