FrontPage

これは.NET4.5以上だから、Visual Studio2010Expressではそのままでは動かないわ。2013以上ならいけるかな、ここ要注意ね。

書き方

using System.IO.Compression;
:
:
:
ZipFile.CreateFromDirectory(directory名, datafile名.zip);
:
:

用例

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace _2013ftp1
{
   class Program
   {
       /// <summary>
       /// loadfile
       /// </summary>
       /// <param name="fn"></param>
       /// <returns></returns>
       private static string loadfile(string fn)
       {
           string text = "";
           try
           {
               StreamReader sr = new StreamReader(fn, Encoding.GetEncoding("Shift_JIS"));
               text = sr.ReadToEnd();
               sr.Close();
           }
           catch (Exception e) // 例外
           {
               text = e.Message;
           }
           return (text);
       }

       /// <summary>
       /// ディレクトリ定義を置き換える。
       /// </summary>
       /// <param name="dir"></param>
       /// <returns></returns>
       private static string dirfilter(string dir)
       {
           string r = dir;
           // Desktop をユーザーデスクトップに置き換える。
           r = Regex.Replace(r, @"Desktop", System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), RegexOptions.IgnoreCase);
           // MyDocuments をユーザーのマイドキュメントに置き換える。
           r = Regex.Replace(r, @"MyDocuments", System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), RegexOptions.IgnoreCase);
           return (r);
       }

       static void Main(string[] args)
       {
           string username = "";
           string password = "";
           string servername = "";
           string localdir = "";
           string remotedir = "";
           string datafile = "webdata.zip";

           if (args.Length == 1)
           {
               string fn = args[0];
               if (File.Exists(fn) == true)
               {
                   try
                   {
                       string[] s = Regex.Replace(loadfile(fn), @"\r", "", RegexOptions.IgnoreCase).Split('\n');
                       username = s[0];
                       password = s[1];
                       servername = s[2];
                       localdir = dirfilter(s[3]);
                       remotedir = s[4];
                   }
                   catch (Exception ex)
                   {
                   }

                   // 必要なデータが揃ったならば、処理を開始する。
                   if (username != "" && password != "" && servername != "" && localdir != "" && remotedir != "")
                   { // 必要なデータがそろっている。
                       // ローカルディレクトリを圧縮する。
                       Directory.SetCurrentDirectory(localdir);
                       Directory.SetCurrentDirectory("..");
                       string[] dir = localdir.Split('/');
                       string dx = dir[dir.Length - 1];
                       try
                       {
                           ZipFile.CreateFromDirectory(dx, datafile);
                       }
                       catch (Exception ex)
                       {
                       }
                   }
               }
           }
       }
   }
}

トップ   編集 凍結解除 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2016-01-28 (木) 18:10:43