The Zip, GZip, BZip2 and Tar Implementation For .NET より dll とサンプル をダウンロードして、サンプル内の 『CreateZipFile』と 『viewzipfile』 を実装してテストしました
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
namespace TryApp1
{
public class Program
{
[STAThreadAttribute]
static void Main(string[] args)
{
App app = new App();
// イベントの追加
app.ShowEnv += MyProc1;
app.GetDir += (string path) =>
{
Console.WriteLine(path + " が選択されました");
string[] filenames = null;
if (path != "") {
// 選択したディレクトリ内のファイル一覧
filenames = Directory.GetFiles(path);
try {
// 対象ファイルと同じディレクトリに test.zip を作成
using (ZipOutputStream s = new ZipOutputStream(File.Create(path + @"\test.zip"))) {
s.SetLevel(9); // 0 - store only to 9 - means best compression
byte[] buffer = new byte[4096];
foreach (string file in filenames) {
// 格納用のオブジェクトを作成
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
// 格納時のタイムスタンプ
entry.DateTime = DateTime.Now;
// (ZipOutputStream に)格納
s.PutNextEntry(entry);
// 実体の格納
using (FileStream fs = File.OpenRead(file)) {
int sourceBytes;
do {
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
// 後処理
s.Finish();
s.Close();
}
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
Console.WriteLine(path + @"\test.zip を作成しました" );
}
};
app.GetFile += (string path) =>
{
byte[] data = new byte[4096];
using (ZipInputStream s = new ZipInputStream(File.OpenRead(path))) {
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null) {
Console.WriteLine("Name : {0}", theEntry.Name);
Console.WriteLine("Date : {0}", theEntry.DateTime);
Console.WriteLine("Size : (-1, if the size information is in the footer)");
Console.WriteLine(" Uncompressed : {0}", theEntry.Size);
Console.WriteLine(" Compressed : {0}", theEntry.CompressedSize);
if (theEntry.IsFile) {
Console.Write("ファイルの内容を表示しますか (y/n) ?");
// y のみ表示
if (Console.ReadLine() == "y") {
int size = s.Read(data, 0, data.Length);
while (size > 0) {
Console.Write(Encoding.ASCII.GetString(data, 0, size));
size = s.Read(data, 0, data.Length);
}
}
Console.WriteLine();
}
}
// ZipInputStream を閉じる
s.Close();
}
};
// メソッドによる実装
app.Cmd += MyProc2;
// ラムダ式による実装
app.Cmd += (string cmd) =>
{
Console.WriteLine(cmd + " が実行されました");
};
// ******************************************
// 終了せずに、コマンドプロンプトを保持する。
// ( "q" で終了 )
// ******************************************
app.Loop();
}
// ******************************************
// 環境周りの情報
// ******************************************
static private void MyProc1(string[] data)
{
foreach (string value in data) {
Console.WriteLine(value);
}
}
// ******************************************
// 処理のテンプレート
// ******************************************
static private void MyProc2(string cmd)
{
Console.WriteLine(cmd);
}
}
}
関連する記事 アプリケーションテスト用コンソールアプリ(TryApp)テンプレート
|
|
【VS(C#)の最新記事】
- Replit : cs-list
- C# : Excel の新しいブックのデフォルトのシートのセルに直接値をセットして、オートフィルを Range オブジェクトから実行する
- C#( Form ) : ウインドウ枠の無い吹き出しの作成
- C# のタプル( Visual Studio 2017 でテスト )
- C# : インターネット上の JSON ファイルのフォーマットを クラスとして定義して1行でオブジェクト化して使用する
- C# の文法的文字列処理
- C# : System.Data.Odbc によるデータベースのテーブルからのデータ取得処理( サンプルの SQL は MySQL 用です )
- C# : Excel を データベースとして DataGridView に読み込む
- C# : dynamic 型 による Excel へのアクセス
- C# : フォームを表示せずに、通知領域にアイコンを表示させる常駐プログラム
- Microsoft Access に対してSQLを入力してその結果を DataGridView に表示する最も簡単なコード
- C# : System.Data.Odbc データ取得(SELECT)処理( MySQL ) : ※ using 無し( Dispose 実行 )
- C# : SQL 文を外部テキストにして、String.Format でデータ部分を置き換えて利用する
- C# コンソールアプリを AN HTTPD で実行
- C# : SQLServer( SQLExpress ) の SMO を使用してテーブルの CREATE TABLE 文 を取得する
- C# : DataGridView に TKMP.DLL の IMAP(POP3) で受信したメールを非同期に表示する( 添付ファイルも取得 )
- C# : TKMP.DLLを使った、メール送信テンプレート
- C# と VB.net : TKMP.DLL を使って IMAP でメール本文の一覧を取得する( コンソール )
- C# でDataTable と DataSource を使用して、DataGridView にデータを表示するテンプレート( 行をダブルクリックしてダイアログを表示して行データを処理 )
- (C#) / VS2010 または VS2012 : TKMP.DLL(3.1.2 または 3.1.8)を使った、『さくらインターネット』用メール送信テンプレート







