[VB] UNZIP32 (ZIP32J+ZIP32) ・・・・・(ZIPの圧縮・解凍・アーカイバ処理)

  UNZIP.bas



  
' ******************************************************
' UNZIP32
' 圧縮には ZIP32J.DLL+ZIP32.DLL要
' ******************************************************
Type INDIVIDUALINFO
    dwOriginalSize As Long
    dwCompressedSize As Long
    dwCRC As Long
    uFlag As Integer
    uOSType As Integer
    wDate0 As Integer
    wTime0 As Integer
    wRatio As Integer
    wDate As Integer
    wTime As Integer
    szFileName As String * 513
    dummy1 As String * 3
    szAttribute As String * 8
    szMode As String * 8
    dummy2 As String * 512
End Type

' ------------------------------------------------------
' バージョン情報
' ------------------------------------------------------
Declare Function UnZipGetVersion Lib "unzip32.dll" () As Integer

' ------------------------------------------------------
' 書庫ファイルOPEN
' ------------------------------------------------------
Declare Function UnZipOpenArchive Lib "unzip32.dll" ( _
    ByVal hWnd As Long, _
    ByVal szFileName As String, _
    ByVal dwMode As Long _
) As Long

' ------------------------------------------------------
' 書庫ファイルCLOSE
' ------------------------------------------------------
Declare Function UnZipCloseArchive Lib "unzip32.dll" ( _
    ByVal harc As Long _
) As Integer

' ------------------------------------------------------
' 初回検索
' ------------------------------------------------------
Declare Function UnZipFindFirst Lib "unzip32.dll" ( _
    ByVal harc As Long, _
    ByVal szWildName As String, _
    lpSubInfo As INDIVIDUALINFO _
) As Integer

' ------------------------------------------------------
' 2回目以降の検索
' ------------------------------------------------------
Declare Function UnZipFindNext Lib "unzip32.dll" ( _
ByVal harc As Long, _
lpSubInfo As INDIVIDUALINFO _
) As Integer

' ------------------------------------------------------
' 解凍
' ------------------------------------------------------
Declare Function UnZip Lib "unzip32.dll" ( _
    ByVal hWnd As Long, _
    ByVal szCmdLine As String, _
    ByVal szOutput As String, _
    ByVal dwSize As Long _
) As Integer

' ------------------------------------------------------
' 圧縮
' ------------------------------------------------------
Declare Function Zip Lib "zip32j.dll" ( _
    ByVal hWnd As Long, _
    ByVal szCmdLine As String, _
    ByVal szOutput As String, _
    ByVal dwSize As Long _
) As Integer


Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type
Type SYSTEMTIME
        wYear As Integer
        wMonth As Integer
        wDayOfWeek As Integer
        wDay As Integer
        wHour As Integer
        wMinute As Integer
        wSecond As Integer
        wMilliseconds As Integer
End Type
Declare Function FileTimeToSystemTime Lib "kernel32" ( _
    lpFileTime As FILETIME, _
    lpSystemTime As SYSTEMTIME _
) As Long
Declare Function DosDateTimeToFileTime Lib "kernel32" ( _
    ByVal wFatDate As Long, _
    ByVal wFatTime As Long, _
    lpFileTime As FILETIME _
) As Long

Global harc As Variant

' ******************************************************
' アーカイブを開く
' ******************************************************
Public Function OpenZip(hWnd As Long, strPath As String)

    Dim ret As Long

    If TypeName(harc) <> "Empty" Then
        Call UnZipCloseArchive(harc)
        harc = Empty
    End If
    
    ret = UnZipOpenArchive(hWnd, strPath, 2)
    harc = ret

End Function

' ******************************************************
' アーカイブを閉じる
' ******************************************************
Public Function CloseZip()

    If TypeName(harc) <> "Empty" Then
        Call UnZipCloseArchive(harc)
        harc = Empty
    End If

End Function

' ******************************************************
' アーカイブを読む
' ******************************************************
Public Function FindZip(strTarget, ByRef UnzipData As INDIVIDUALINFO) As Long

    If strTarget <> "" Then
        FindZip = UnZipFindFirst(harc, strTarget, UnzipData)
    Else
        FindZip = UnZipFindNext(harc, UnzipData)
    End If

End Function

' ******************************************************
' ディレクトリ圧縮
' ******************************************************
Public Function ZipFreezeDir(hWnd As Long, strTargetDir, strTargetZip) As Long

    Dim strCommand As String
    Dim strOut As String * 512
    Dim nSize As Long
    
    strCommand = "-ur " & strTargetZip & " " & strTargetDir & "\*"
    nSize = 512
    ZipFreezeDir = Zip(hWnd, strCommand, strOut, nSize)

End Function

' ******************************************************
' レスポンスファイルによる圧縮
' ******************************************************
Public Function ZipFreeze(hWnd As Long, strTargetDir, strTargetList, strTargetZip) As Long

    Dim strCommand As String
    Dim strOut As String * 512
    Dim nSize As Long
    
    strCommand = "-u " & strTargetZip & " " & strTargetDir & "\ @" & strTargetList
    nSize = 512
    ZipFreeze = Zip(hWnd, strCommand, strOut, nSize)

End Function

' ******************************************************
' 書庫解凍
' ******************************************************
Public Function ZipMelt( _
    hWnd As Long, _
    strOption, _
    strTargetZip, _
    strTargetDir, _
    strFileSpec _
) As Long

    Dim strCommand As String
    Dim strOut As String * 512
    Dim nSize As Long
    
    strCommand = strOption & " """ & strTargetZip & """ " & strTargetDir & "\ " & strFileSpec
    nSize = 512
    ZipMelt = UnZip(hWnd, strCommand, strOut, nSize)

End Function

' ******************************************************
' バイナリの日付を文字列に変換
' ******************************************************
Public Function GetDosDateTime(wDate As Integer, wTime As Integer) As String
    
    Dim lpFileTime As FILETIME
    Dim lpSystemTime As SYSTEMTIME

    Call DosDateTimeToFileTime(wDate, wTime, lpFileTime)
    Call FileTimeToSystemTime(lpFileTime, lpSystemTime)
    
    GetDosDateTime = _
        Format(lpSystemTime.wYear, "0000") & "/" & _
        Format(lpSystemTime.wMonth, "00") & "/" & _
        Format(lpSystemTime.wDay, "00") & " " & _
        Format(lpSystemTime.wHour, "00") & ":" & _
        Format(lpSystemTime.wMinute, "00") & ":" & _
        Format(lpSystemTime.wSecond, "00")

End Function

  



  アーカイバ処理サンプル



  
Private Sub Command1_Click()

    Dim sts As Long
    Dim uData As INDIVIDUALINFO
    Dim bFirst As Boolean

    Grid.Clear
    Grid.Rows = 2

    Call OpenZip(Me.hWnd, "アーカイブファイルのパス")
    bFirst = True
    
    Do
    
        If bFirst Then
            bFirst = False
            sts = FindZip("*.*", uData)
            If sts <> 0 Then
                Exit Do
            End If
        Else
            sts = FindZip("", uData)
            If sts <> 0 Then
                Exit Do
            End If
            Grid.Rows = Grid.Rows + 1
        End If

        With Grid

            .TextMatrix(.Rows - 1, 0) = .Rows - 1
            .TextMatrix(.Rows - 1, 1) = uData.szFileName
            .TextMatrix(.Rows - 1, 2) = GetDosDateTime(uData.wDate, uData.wTime)
            .TextMatrix(.Rows - 1, 3) = Format(uData.dwOriginalSize, "#,##0")
            .TextMatrix(.Rows - 1, 4) = Format(uData.dwCompressedSize, "#,##0")
            If uData.dwOriginalSize = 0 Then
                .TextMatrix(.Rows - 1, 5) = 0
            Else
                .TextMatrix(.Rows - 1, 5) = _
                    Format((uData.dwCompressedSize / uData.dwOriginalSize) * 100, "#0")
            End If
            .TextMatrix(.Rows - 1, 6) = uData.szAttribute
            .TextMatrix(.Rows - 1, 7) = uData.szMode
            
        End With

    Loop

    Call CloseZip

End Sub

Private Sub Form_Load()
    
    With Grid
    
        .Cols = 8
        .ColWidth(0) = 400
        .ColWidth(1) = 3000
        .ColWidth(2) = 1700
        .ColWidth(5) = 500
        .ColWidth(6) = 600
        .ColWidth(7) = 600
    
    End With

End Sub

  



  圧縮・解凍サンプル

  
Private Sub Command2_Click()

    Call ZipFreezeDir(Me.hWnd, "d:\temp", "zipのパス")

End Sub
Private Sub Command3_Click()

    Call ZipMelt(Me.hWnd, "-x -s -C -o", "zipのパス", "D:\temp\zipmelt", "*.*")

End Sub
Private Sub Command4_Click()

    Call ZipFreeze(Me.hWnd, "d:\temp", "d:\temp\list.txt", "zipのパス")

End Sub

  










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




SQLの窓  天気  IT用語辞典
Yahoo!ニュース  マルチ辞書
PHP マニュアル  Google URL短縮 


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

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ