0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C#でrar圧縮

Last updated at Posted at 2019-09-17

RARとは

・リカバリレコード付きの圧縮フォーマット.リード・ソロモン符号を付加することでファイルの破損(昨今のNAND flash memory:SSD,USBmemoryは多値化が進みデータが一年程度で揮発する)の修復を可能とする.
・シェアウエアである.
・ZIPと異なりC#にはrar圧縮する機能は組み込まれていない.そのためRar.exeを外部アプリとして呼び出す必要ががある.

Rar.exeの入手

Rar.exeをexeと同じ階層に配置する必要がある.以下からダウンロードし7zなどで展開してください
https://www.rarlab.com/download.htm
https://www.rarlab.com/rar/winrar-x64-58b2.exe
https://www.rarlab.com/rar/wrar58b2.exe

サンプルプログラムの簡易解説

CreateRAR(...)パスを指定してこの関数を呼べば圧縮される
string PathName内のすべてのファイルが圧縮される.
System.Environment.ProcessorCountはシステムの論理プロセッサの数を取得(4 cores 8 threadsなら8).並列で圧縮する.
ep:アーカイヴからフォルダ構造をなくしたい場合指定する.

CreateRAR.cs
private void CreateRAR(string PathName) {
    string Extension = ".rar";
    string FileName = "Rar.exe";
    string CompressLevel = " -m5 ";//compress level max 
    string Arguments = " a \"" + PathName + Extension + "\" -rr5 -mt" + System.Environment.ProcessorCount + CompressLevel + "-ep \"" + PathName + "\"";
                /*a             書庫にファイルを圧縮
                  rr[N]         リカバリレコードを付加.5 %が推奨らしい
                  m<0..5>       圧縮方式を指定 (0-無圧縮...5-標準...5-最高圧縮)
                  mt<threads>   スレッドの数をセット
                  ep            名前からパスを除外*/
    ExecuteAnotherApp(in FileName, in Arguments, false, true);
}
private void ExecuteAnotherApp(in string FileName, in string Arguments, bool UseShellExecute, bool CreateNoWindow) {//外部アプリ実行
    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo {
        FileName = FileName, Arguments = Arguments,
        UseShellExecute = UseShellExecute,
        CreateNoWindow = CreateNoWindow    // コンソール・ウィンドウを開かない
   }).WaitForExit();//終了を待つ
}
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?