ZIP ファイルからメモリ(例 Byte 配列)にファイルを抽出するには、以下の関数を使用します。
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
Private Function GetDataFromZipFile(zipFileName As String, entryName As String) As Byte()
' ZIP ファイルからエントリを取得します。
Dim zip As New C1ZipFile()
zip.Open(zipFileName)
Dim ze As C1ZipEntry = zip.Entries(entryName)
'エントリのデータをメモリストリームにコピーします。
Dim ms As New MemoryStream()
Dim buf(1000) As Byte
Dim s As Stream = ze.OpenReader()
Try
While True
Dim read As Integer = s.Read(buf, 0, buf.Length)
If read = 0 Then
Exit While
End If
ms.Write(buf, 0, read)
End While
Finally
s.Dispose()
End Try
s.Close()
' 結果を返します。
Return ms.ToArray()eturn ms.ToArray()
End Function
|
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
private byte[] GetDataFromZipFile(string zipFileName, string entryName)
{
// ZIP ファイルからエントリを取得します。
C1ZipFile zip = new C1ZipFile();
zip.Open(zipFileName);
C1ZipEntry ze = zip.Entries[entryName];
// エントリのデータをメモリストリームにコピーします。
MemoryStream ms = new MemoryStream();
byte[] buf = new byte[1000]
using (Stream s = ze.OpenReader())
{
for (;;)
{
int read = s.Read(buf, 0, buf.Length);
if (read == 0) break;
ms.Write(buf, 0, read);
}
}
// C# で using ステートメントを使用しているため、Close メソッドを呼び出す必要はありません。
// しかし、VB では必要です。
//s.Close();
// 結果を返します。
return ms.ToArray();
}
|
|