トップ  >  サンプルソース  >  ファイルZIP圧縮(VB.NET)

ファイルZIP圧縮(VB.NET)

2010年01月04日

ファイルをZIP圧縮するサンプル(ソース/コード)です。
フォルダをZIP圧縮する場合は改造が必要です。

' ----------------------------------------------------------
' ファイルをZIP圧縮するサンプル(VB.NET/VS2005)
' フォルダをZIP圧縮する場合は改造が必要です
' ZIPフォルダの設定が有効で無い場合は一時的に有効にします
' XP以上?(Windows2000では動作確認出来ませんでした)
' 起動引数:EXE名 test.zip "D:\Temp\test.txt"
Shared Sub Main()
    ' ZIPフォルダーチェック
    Dim blnZipFld As Boolean = False
    Try
        Dim objZipFld As Object = _
            CreateObject("CompressedFolder")
        If objZipFld Is Nothing = False Then _
            Marshal.ReleaseComObject(objZipFld)
        objZipFld = Nothing
        blnZipFld = True
    Catch ex As Exception
    End Try

    Try
        ' 一時的にZIPフォルダを有効にする
        Try
            If blnZipFld = False Then
                Shell("regsvr32 /s ZIPFLDR.DLL", _
                    AppWinStyle.Hide)
                Dim objZipFld As Object = _
                    CreateObject("CompressedFolder")
                If objZipFld Is Nothing = False Then _
                    Marshal.ReleaseComObject(objZipFld)
                objZipFld = Nothing
            End If
        Catch ex As Exception
            Throw New Exception( _
                "This PC does not support ZIP compression...")
        End Try

        'コマンドラインを配列で取得する
        Dim cmds() As String
        cmds = System.Environment.GetCommandLineArgs()

        If cmds.Length >= 3 Then
            ' 圧縮後のファイル名(ZIP)
            Dim zipFile As String = _
                IO.Path.GetFullPath(cmds(1))
            ' 圧縮するファイル名
            Dim srcPath As String = _
                IO.Path.GetFullPath(cmds(2))

            Dim sw As IO.StreamWriter = Nothing
            Try
                ' 空のZIPファイル保存
                Dim karaZip As String = _
                    "PK" & Chr(5) & Chr(6) & _
                    New String(Chr(0), 18)
                sw = New IO.StreamWriter( _
                    zipFile, False, _
                    System.Text.Encoding.GetEncoding( _
                    "SHIFT-JIS"))
                sw.Write(karaZip, True)
                sw.Close()
                sw = Nothing

                ' 圧縮
                Dim shellcls As Object = Nothing
                Dim srcFlder As Object = Nothing
                Dim dstFlder As Object = Nothing
                Dim srcItems As Object = Nothing
                Try
                    ' ZIP圧縮用のオブジェクト生成と圧縮実行
                    shellcls = CreateObject( _
                        "Shell.Application")
                    srcFlder = shellcls.NameSpace( _
                        IO.Path.GetDirectoryName(srcPath))
                    dstFlder = shellcls.NameSpace( _
                        IO.Path.GetFullPath(zipFile))
                    srcItems = srcFlder.ParseName( _
                        IO.Path.GetFileName(srcPath))
                    dstFlder.CopyHere(srcItems)

                    ' ZIP圧縮処理完了待ち
                    Dim cnt As Integer = 0
                    Dim siz0 As Integer = 0
                    Dim siz1 As Integer = 0
                    Do
                        Threading.Thread.Sleep(100)
                        cnt += 1
                        Try
                            Dim fi As New IO.FileInfo(zipFile)
                            siz1 = fi.Length
                            If siz1 > karaZip.Length _
                                AndAlso siz0 = siz1 Then
                                Exit Do
                            End If
                            siz0 = siz1
                        Catch ex As Exception
                        End Try
                        If cnt >= 10 * 60 Then Exit Do
                    Loop
                Catch ex As Exception
                    Throw ex
                Finally
                    ' ZIP圧縮用のオブジェクト解放
                    If srcItems Is Nothing = False Then _
                        Marshal.ReleaseComObject(srcItems)
                    If dstFlder Is Nothing = False Then _
                        Marshal.ReleaseComObject(dstFlder)
                    If srcFlder Is Nothing = False Then _
                        Marshal.ReleaseComObject(srcFlder)
                    If shellcls Is Nothing = False Then _
                        Marshal.ReleaseComObject(shellcls)
                    srcItems = Nothing
                    dstFlder = Nothing
                    srcFlder = Nothing
                    shellcls = Nothing
                End Try
            Catch ex As Exception
                Try
                    IO.File.Delete(zipFile)
                Catch ex2 As Exception
                End Try
                Throw ex
            Finally
                If sw Is Nothing = False Then sw.Close()
            End Try
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message, "ERROR", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

    ' ZIPフォルダを元に戻す
    Try
        If blnZipFld = False Then
            Shell("regsvr32 /u /s ZIPFLDR.DLL", _
                AppWinStyle.Hide)
        End If
    Catch ex As Exception
    End Try
End Sub
' ----------------------------------------------------------
スポンサーサイト

WEBページ保存(ツール) | トップページへ戻る | 漢字コード判定(C#/VB.NET)

このページのトップに戻る

コメント

名前
題名
メールアドレス
WEBサイト
 
コメント
パスワード
  管理者にだけ表示を許可する

このページのトップに戻る

トラックバック

このページのトップに戻る