タスク別ヘルプ > Zip エントリからメモリにファイルを抽出する |
zip からメモリ変数(バイト配列など)にファイルを抽出するには、次のコードを追加します。コードの先頭に、これらの Imports 文 (Visual Basic)/(C#) を追加してください:
コードのコピー
|
|
---|---|
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() End Function |
コードのコピー
|
|
---|---|
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(); } |