Zipファイルを簡単に扱う
J#ライブラリをインストールすると
簡単に使えるようになる
using java.io;
using java.util.zip;
using System;///
/// Zip圧縮を行うライブラリ
///
///
/// 参照に追加するライブラリ
/// vjslib.dll
///
public class Zip {
///
/// バッファサイズ
///
public const int BUFFUER_SIZE = 8192;
///
/// コンストラクタ
///
public Zip( ) {
this.sbuf = new sbyte[Zip.BUFFUER_SIZE];
}
///
/// コンストラクタ
///
/// ZIPパス
public Zip( string path )
: this( ) {
this.path = path;
}
///
/// ZIPパス
///
protected string path = string.Empty;
///
/// ZIPパス
///
public string Path {
get {
return this.path;
}
set {
if( string.IsNullOrEmpty( value ) ) {
this.path = string.Empty;
} else {
this.path = value;
}
}
}
///
/// File用Stream
///
protected FileOutputStream fos = null;
///
/// Zip用Stream
///
protected ZipOutputStream zos = null;
///
/// バッファ
///
protected sbyte[] sbuf;
///
/// ZIPファイルをOpenする
///
/// ZIPパス
public void Open( string path ) {
this.Path = path;this.Open( );
}
///
/// ZIPファイルをOpenする
///
public void Open( ) {
try {
//各StreamをOpen
this.fos = new FileOutputStream( this.path );
this.zos = new ZipOutputStream( fos );
} catch( Exception ) {
this.Close( );
throw;
}
}
///
/// ZIPファイルをCloseする
///
public void Close( ) {
try {
// ZIP用Streamのクローズ
if( this.zos != null ) {
this.zos.close( );
}
} finally {
this.zos = null;
try {
// File用Streamのクローズ
if( this.fos != null ) {
this.fos.close( );
}
} finally {
this.fos = null;
}
}
}
///
/// ファイルのPut
///
/// Zipエントリー
/// FileStream
public void Put( ZipEntry ze, FileInputStream fis ) {
int length;// EntryのPut
this.zos.putNextEntry( ze );// File出力
while( ( length = fis.read( this.sbuf, 0, this.sbuf.Length ) ) > 0 ) {
zos.write( this.sbuf, 0, length );
}
//閉じる
zos.closeEntry( );
}
///
/// ファイルのPut
///
/// Zipのファイル名
/// ファイルへのパス
public void Put( string name, string file ) {
FileInputStream fis = null;// ZipEntryの作成
ZipEntry ze = new ZipEntry( name.Replace( '\\', '/' ) );
ze.setMethod( ZipEntry.DEFLATED );try {
// ファイル入力ストリームの作成
fis = new FileInputStream( file );// ファイルのPut
this.Put( ze, fis );
} finally {
// ファイル入力ストリームのクローズ
if( fis != null ) {
try {
fis.close( );
} finally {
fis = null;
}
}
}
}
///
/// ファイルのPut
///
/// ファイルへのパス
///
/// ファイルのパスを無効化し、ZipのRootにファイルを追加します
///
public void Put( string file ) {
string name = System.IO.Path.GetFileName( file );this.Put( name, file );
}
///
/// ファイルのPut
///
/// ファイルへのパス
///
/// ドライブのルートからのディレクトリ構成を保ちながらZipへ格納します
///
public void PutRootFile( string file ) {
// ディレクトリを保持する時は次のようにする
string name = file.Remove( 0, System.IO.Path.GetPathRoot( file ).Length );this.Put( name, file );
}
///
/// ディレクトリのPut
///
/// ディレクトリへのパス
public void PutDir( string dir ) {
this.PutDir( dir, dir );
}
///
/// ディレクトリのPut
///
/// ルートディレクトリ
/// ディレクトリへのパス
protected void PutDir( string root, string dir ) {
string[] files = System.IO.Directory.GetFiles( dir );
string[] dirs = System.IO.Directory.GetDirectories( dir );// ファイルのPut
for( int cnt = 0; cnt < files.Length; cnt++ ) {
this.Put( files[cnt].Remove( 0, root.Length ), files[cnt] );
}// サブディレクトリのPut
for( int cnt = 0; cnt < dirs.Length; cnt++ ) {
// 再帰呼び出し
this.PutDir( root, dirs[cnt] );
}
}
}