Option Strict On Imports System.Runtime.InteropServices Imports System.Text Imports System.IO Imports System Public Module Sample Public Sub Main() Try Dim archive As String = "C:\src.7z" Dim folder As String = "C:\test\" Extract7z("x {0} -o{1}", archive, folder) Catch ex As Exception Console.WriteLine(ex.Message) End Try End Sub Private Declare Ansi Function SevenZip Lib "7-zip32" ( _ ByVal hwnd As IntPtr, _ ByVal szCmdLine As String, _ ByVal szOutput As String, _ ByVal dwSize As Integer) As Integer Private Declare Auto Function SevenZipGetVersion Lib "7-zip32" () As Short Private Declare Auto Function SevenZipGetRunning Lib "7-zip32" () As Boolean Private Enum CHECKARCHIVE RAPID BASIC FULLCRC End Enum Private Declare Auto Function SevenZipCheckArchive Lib "7-zip32" ( _ ByVal szFileName As String, _ ByVal iMode As CHECKARCHIVE) As Boolean Public Sub Extract7z( _ ByVal command As String, _ ByVal archiveFile As String, _ ByVal extractDir As String) '指定されたファイルがあるか調べる If Not File.Exists(archiveFile) Then Throw New FileNotFoundException("ファイルが見つかりません。", archiveFile) End If Dim ver As Short = SevenZipGetVersion() Console.WriteLine("バージョン:{0}", ver) '動作中かチェック If SevenZipGetRunning() Then Throw New InvalidOperationException("DLLが動作中です。") End If '展開できるかチェック If SevenZipCheckArchive(archiveFile, CHECKARCHIVE.RAPID) Then Throw New ArgumentOutOfRangeException( _ "archiveFile", archiveFile, "対応書庫ではありません。") End If 'ファイル名とフォルダ名を修正する If archiveFile.IndexOf(" "c) > 0 Then archiveFile = """" & archiveFile & """" End If If Not extractDir.EndsWith("\") Then extractDir &= "\" End If If extractDir.IndexOf(" "c) > 0 Then extractDir = """" & extractDir & """" End If '展開する Dim ret As Integer = SevenZip( _ IntPtr.Zero, _ String.Format(command, archiveFile, extractDir), _ Nothing, _ 0) '結果 If ret <> 0 Then Throw New ApplicationException("書庫の展開に失敗しました。(" & CStr(ret) & ")") End If End Sub End Module