7-zip32.dll ラッパークラス ( アンマネージ DLL の処理 )

  7-zip32.dll



統合アーカイバプロジェクト のうちの一つですから、unlha32.dll も含めて他のアーカイバにも使えるはずです。
zip 書庫が扱えるという事で実装してみました( Framework 2.0 )

↓統合アーカイバプロジェクト
http://www.csdinc.co.jp/archiver/lib/7-zip32.html

↓7-Zip に関する wikipedia の説明
http://ja.wikipedia.org/wiki/7-Zip



  SevenZip クラス



全ての API を実装していませんが、必要ならば追加実装するだけです。

  
Imports System.Runtime.InteropServices
Imports System.Text

Public Class SevenZip

	' ********************************************************
	' * DLL 内 関数宣言
	' ********************************************************
	<DllImport("Kernel32.dll", CharSet:=CharSet.Ansi)> _
	 Private Shared Function LoadLibrary( _
	  ByVal lpDllName As String) As IntPtr
	End Function

	<DllImport("Kernel32.dll", CharSet:=CharSet.Auto)> _
	Private Shared Function FreeLibrary( _
	 ByVal hModule As IntPtr) As Integer
	End Function

	<DllImport("Kernel32.dll", CharSet:=CharSet.Ansi)> _
	 Private Shared Function GetProcAddress( _
	  ByVal hModule As IntPtr, _
	  ByVal lpProcName As String) As IntPtr
	End Function

	<DllImport("Kernel32.dll", CharSet:=CharSet.Ansi)> _
	 Private Shared Function DosDateTimeToFileTime( _
	  ByVal wFatDate As Short, _
	  ByVal wFatTime As Short, _
	  <[In](), Out()> ByVal lpFileTime As FILETIME) As Boolean
	End Function

	<DllImport("Kernel32.dll", CharSet:=CharSet.Ansi)> _
	 Private Shared Function FileTimeToSystemTime( _
	   <[In](), Out()> ByVal lpFileTime As FILETIME, _
	   <[In](), Out()> ByVal lpSystemTime As SYSTEMTIME) As Boolean
	End Function

	' ********************************************************
	' * 構造体定義
	' ********************************************************
	<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
	  Public Class INDIVIDUALINFO
		Public dwOriginalSize As Integer = 0
		Public dwCompressedSize As Integer = 0
		Public dwCRC As Integer = 0
		Public uFlag As Integer = 0
		Public uOSType As Integer = 0
		Public wRatio As Short = 0
		Public wDate As Short = 0
		Public wTime As Short = 0
		<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=513)> _
		Public szFileName As String = Nothing
		<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=3)> _
		  Public dummy1 As String = Nothing
		<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=8)> _
		 Public szAttribute As String = Nothing
		<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=8)> _
		 Public szMode As String = Nothing
	End Class

	<StructLayout(LayoutKind.Sequential)> _
	  Public Class FILETIME
		Public dwLowDateTime As Integer = 0
		Public dwHighDateTime As Integer = 0
	End Class

	<StructLayout(LayoutKind.Sequential)> _
	Public Class SYSTEMTIME
		Public wYear As Short = 0
		Public wMonth As Short = 0
		Public wDayOfWeek As Short = 0
		Public wDay As Short = 0
		Public wHour As Short = 0
		Public wMinute As Short = 0
		Public wSecond As Short = 0
		Public wMilliseconds As Short = 0
	End Class

	' ********************************************************
	'引数
	'    _hwnd       7-zip32.dll を呼び出すアプリのウィンドウハンドル。
	'    _szFileName 書庫ファイル名。
	'    _dwMode     現時点では無視。0 を指定。

	'戻り値
	'    指定の書庫ファイルに対応したハンドル。エラー時は NULL
	' ********************************************************
	Public Delegate Function SevenZipOpenArchive( _
	 ByVal hwnd As IntPtr, _
	 <[In](), MarshalAs(UnmanagedType.LPStr)> ByVal szFileName As String, _
	 ByVal dwMode As Integer) As IntPtr

	' ********************************************************
	'戻り値
	'    正常終了時に 0 、異常時には -1 が返ります。
	' ********************************************************
	Public Delegate Function SevenZipCloseArchive( _
	ByVal harc As IntPtr) As Integer

	' ********************************************************
	'引数
	'    _harc       SevenZipOpenArchive() で返されたハンドル。
	'    _szWildName 検索するファイル名の指定。
	'                ワイルドカード指定が可能。
	'                ファイル名はスペース区切りで複数指定が可能。
	'    _lpSubInfo  結果を返すための INDIVIDUALINFO 構造体へのポインタ。
	'                結果を必要としない場合は NULL を指定できます。

	'戻り値
	'    0           正常終了。
	'    -1          検索終了。
	'    0,-1 以外  エラー終了。エラーコードを返します。
	' ********************************************************
	Public Delegate Function SevenZipFindFirst( _
	  ByVal harc As IntPtr, _
	  <[In](), MarshalAs(UnmanagedType.LPStr)> ByVal szWildName As String, _
	  <[In](), Out()> ByVal lpSubInfo As INDIVIDUALINFO) As Integer

	' ********************************************************
	'引数
	'    _harc       SevenZipOpenArchive() で返されたハンドル。
	'    _lpSubInfo  結果を返すための INDIVIDUALINFO 構造体へのポインタ。
	'                結果を必要としない場合は NULL を指定できます。

	'戻り値
	'    0           正常終了。
	'    -1          検索終了。
	'    0,-1 以外  エラー終了。エラーコードを返します。
	' ********************************************************
	Public Delegate Function SevenZipFindNext( _
	   ByVal harc As IntPtr, _
	   <[In](), Out()> ByVal lpSubInfo As INDIVIDUALINFO) As Integer

	' ********************************************************
	'_hwnd       7-zip32.dll を呼び出すアプリのウィンドウハンドル。
	'            7-zip32.dll は実行時にこのウィンドウに対して
	'            EnableWindow() を実行しウィンドウの動作を抑制します。
	'            ウィンドウが存在しないコンソールアプリの場合や、
	'            指定する必要のない場合は NULL を渡します。
	'_szCmdLine  7-zip32.dll に渡すコマンド文字列。
	'_szOutput   7-zip32.dll が結果を返すためのバッファ。
	'            グローバルメモリー 等の場合はロックされている必要が
	'            あります。
	'_dwSize     バッファのサイズ。
	'            結果が指定サイズを越える場合は、このサイズに
	'            切り詰められます。
	'            サイズが 1 以上であれば、常に最後に NULL 文字が
	'            付加されます。
	' ********************************************************
	Public Delegate Function SevenZip( _
	  ByVal hwnd As IntPtr, _
	  <[In](), MarshalAs(UnmanagedType.LPStr)> ByVal szCmdLine As String, _
	  <[In](), Out(), MarshalAs(UnmanagedType.LPStr)> ByVal szOutput As StringBuilder, _
	  ByVal dwSize As Integer) As Integer

	' 実行中のチェック
	Public Delegate Function SevenZipGetRunning() As Boolean


	Private hModule As IntPtr
	Public OpenArchive As SevenZipOpenArchive
	Public CloseArchive As SevenZipCloseArchive
	Public FindFirst As SevenZipFindFirst
	Public FindNext As SevenZipFindNext
	Public GetRunning As SevenZipGetRunning
	Public DoSevenZip As SevenZip
	Public ft As New FILETIME()
	Public st As New SYSTEMTIME()

	' ********************************************************
	' 7-zip32.dll ロード
	' ********************************************************
	Public Function Load() As Boolean

		hModule = LoadLibrary("7-zip32.dll")
		If hModule = IntPtr.Zero Then
			Return False
		End If

		Dim ptr As IntPtr

		ptr = GetProcAddress(hModule, "SevenZipOpenArchive")
		If ptr <> IntPtr.Zero Then
			OpenArchive = _
			 Marshal.GetDelegateForFunctionPointer( _
			  ptr, _
			  GetType(SevenZipOpenArchive) _
			 )
		End If

		ptr = GetProcAddress(hModule, "SevenZipCloseArchive")
		If ptr <> IntPtr.Zero Then
			CloseArchive = _
			  Marshal.GetDelegateForFunctionPointer( _
			ptr, _
			GetType(SevenZipCloseArchive) _
			 )
		End If

		ptr = GetProcAddress(hModule, "SevenZipFindFirst")
		If ptr <> IntPtr.Zero Then
			FindFirst = _
			  Marshal.GetDelegateForFunctionPointer( _
			ptr, _
			GetType(SevenZipFindFirst) _
			 )
		End If

		ptr = GetProcAddress(hModule, "SevenZipFindNext")
		If ptr <> IntPtr.Zero Then
			FindNext = _
			  Marshal.GetDelegateForFunctionPointer( _
			ptr, _
			GetType(SevenZipFindNext) _
			 )
		End If

		ptr = GetProcAddress(hModule, "SevenZipGetRunning")
		If ptr <> IntPtr.Zero Then
			GetRunning = _
			Marshal.GetDelegateForFunctionPointer( _
			 ptr, _
			 GetType(SevenZipGetRunning) _
		   )
		End If

		ptr = GetProcAddress(hModule, "SevenZip")
		If ptr <> IntPtr.Zero Then
			DoSevenZip = _
			Marshal.GetDelegateForFunctionPointer( _
			 ptr, _
			 GetType(SevenZip) _
		   )
		End If

		Return True

	End Function

	' ********************************************************
	' 日付・時間変換
	' ********************************************************
	Public Sub ConvDateTime(ByVal target As INDIVIDUALINFO)

		If hModule <> IntPtr.Zero Then
			DosDateTimeToFileTime(target.wDate, target.wTime, Me.ft)
			FileTimeToSystemTime(Me.ft, Me.st)
		End If

	End Sub

	' ********************************************************
	' 廃棄
	' ********************************************************
	Public Sub Dispose()

		If hModule <> IntPtr.Zero Then
			FreeLibrary(hModule)
		End If

	End Sub

End Class
  



  処理サンプル

  
' インスタンス
Private zip7 As New SevenZip()

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

	' フォームのハンドル取得
	Dim hwnd As IntPtr = Me.Handle

	' 7-zip32.dll のロード
	If Not zip7.Load() Then
		MessageBox.Show("DLLをロードできません")
		Return
	End If

	' アーカイブハンドル
	Dim handle As IntPtr

	' データ取得用構造体
	Dim data As New SevenZip.INDIVIDUALINFO()

	' 開く
	If zip7.GetRunning() Then
		' 厳密には、API を実行する毎にチェックする
		MessageBox.Show("7-zip32.dll が使用中です")
		Return
	End If
	handle = zip7.OpenArchive(hwnd, "C:\TMP\日本語.zip", 0)

	' 最初の検索
	Dim ret As Integer = zip7.FindFirst(handle, "*", data)

	' 2回目以降の検索
	Dim out_file As String = " "
	Dim cnt As Integer = 0	' カウンタ
	Do While ret = 0
		zip7.ConvDateTime(data)
		Console.Write("{0:0000}/{1:00}/{2:00} {3:00}:{4:00}:{5:00} ", _
		 zip7.st.wYear _
		 , zip7.st.wMonth _
		 , zip7.st.wDay _
		 , zip7.st.wHour _
		 , zip7.st.wMinute _
		 , zip7.st.wSecond _
		 )
		Console.Write(data.szFileName + "  ")
		Console.Write(data.dwOriginalSize.ToString() + "  ")
		Console.Write(data.dwCompressedSize.ToString() + "  ")
		Console.Write((data.wRatio / 10).ToString() + "  ")
		Console.Write(data.szAttribute)
		Console.WriteLine("")
		cnt += 1
		' 解凍用文字列
		out_file += """" + data.szFileName + """ "

		' 次を読む
		ret = zip7.FindNext(handle, data)
	Loop

	' 閉じる
	zip7.CloseArchive(handle)

	Dim szOutput As New System.Text.StringBuilder(500)
	Dim szCommand As String
	' 解凍コマンド作成
	szCommand = String.Format("x C:\TMP\日本語.zip -oC:\TMP\解凍 {0} -r -aoa", out_file)
	Console.WriteLine(szCommand)

	' 解凍
	ret = zip7.DoSevenZip(Nothing, szCommand, szOutput, szOutput.Capacity)
	Console.WriteLine(ret.ToString())
	Console.WriteLine(szOutput.ToString())

	' ZIP 書庫作成コマンド
	szCommand = "u -tzip C:\TMP\ZIP圧縮\日本語.zip C:\TMP\解凍\*"
	Console.WriteLine(szCommand)

	' ZIP 書庫作成
	ret = zip7.DoSevenZip(Nothing, szCommand, szOutput, szOutput.Capacity)
	Console.WriteLine(ret.ToString())
	Console.WriteLine(szOutput.ToString())

	' 7Z 書庫作成コマンド
	szCommand = "u -t7z C:\TMP\7Z圧縮\日本語.7z C:\TMP\解凍\*"
	Console.WriteLine(szCommand)

	' 7Z 書庫作成
	ret = zip7.DoSevenZip(Nothing, szCommand, szOutput, szOutput.Capacity)
	Console.WriteLine(ret.ToString())
	Console.WriteLine(szOutput.ToString())

	' 廃棄
	zip7.Dispose()

End Sub
  

※ 以下マニュアルより
-----------------------------------------------------------------------------
スペースを含むファイル名は以下の様に""で全体を囲ってください。
例) "Dir\Program files\*"
-----------------------------------------------------------------------------










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ