酢ろぐ!

カレーが嫌いなスマートフォンアプリプログラマのブログ。

Windows MobileでSharpZipLibを使ってZip形式のアーカイブを展開(解凍)する

SharpZipLibを使用してZip形式のアーカイブを展開(解凍)します。ここでは、SharpZipLibのバージョンは0.85.5を使用しています。

Zipアーカイブを指定したフォルダに展開します。

**展開(解凍)処理

|vb| Private Sub UnCompactionZip(ByVal zipPath As String, _ ByVal dirPath As String, _ Optional ByVal password As String = "")

Using fs As New FileStream(zipPath, FileMode.Open, FileAccess.Read, FileShare.Read), _
      zis As New ICSharpCode.SharpZipLib.Zip.ZipInputStream(fs)

    'ZIP内のファイル情報を取得
    Dim entry As ICSharpCode.SharpZipLib.Zip.ZipEntry = zis.GetNextEntry()
    While entry IsNot Nothing

        ' ディレクトリの場合は処理しない
        If (entry.IsDirectory) Then
            Continue While
        End If

        ' 展開先のフォルダを作成する
        Dim entryPath = "." & entry.Name
        Dim destPath As String = Path.Combine(dirPath, entryPath)
        Dim info = Directory.CreateDirectory(Path.GetDirectoryName(destPath))

        ' ファイルを出力していく
        Using fileStrm As New FileStream(destPath, FileMode.Create, _
                                         FileAccess.Write, FileShare.Write)

            Dim readSize As Integer = 0
            Dim remain As Integer = zis.Length

            While remain > 0
                ' 読み取り用のバッファを用意する
                Dim buf(Math.Min(10240, remain) - 1) As Byte
                readSize = zis.Read(buf, 0, buf.Length)
                fileStrm.Write(buf, 0, readSize)
                remain -= readSize
            End While

        End Using

        entry = zis.GetNextEntry()
    End While
End Using

End Sub ||<

**呼び元の処理

|vb| Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click

' 展開するZIPファイル
Dim zipPath As String = "C:\test.zip"
' 展開先のフォルダパス
Dim dirPath As String = "C:\dir"

' Zipアーカイブを展開する
UnCompactionZip(zipPath, dirPath)

End Sub ||<

関連記事