SQLの窓

2013年09月15日


TryAppテンプレートを使って『The Zip, GZip, BZip2 and Tar Implementation For .NET』のテスト



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#)の最新記事】
posted by lightbox at 2013-09-15 20:29 | VS(C#) | このブログの読者になる | 更新情報をチェックする
container 終わり



フリーフォントで簡単ロゴ作成
フリーフォントでボタン素材作成
フリーフォントで吹き出し画像作成
フリーフォントではんこ画像作成
ほぼ自由に利用できるフリーフォント
フリーフォントの書体見本とサンプル
画像を大きく見る為のウインドウを開くボタンの作成

CSS ドロップシャドウの参考デモ
イラストAC
ぱくたそ
写真素材 足成
フリーフォント一覧
utf8 文字ツール
右サイド 終わり
base 終わり