microsoft-visualbasic-runtime/ApplicationServices/Tools/Zip/StreamReader.vb

119 lines
4.3 KiB
VB.net
Raw Normal View History

2020-05-21 22:27:25 +08:00
#Region "Microsoft.VisualBasic::b7df676ea198508f698deedff360a1a1, Microsoft.VisualBasic.Core\ApplicationServices\Tools\Zip\StreamReader.vb"
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
' /********************************************************************************/
' Summaries:
' Module ZipStreamReader
'
2020-04-04 21:15:06 +08:00
' Function: Decompress, GetZipSubStream, LoadZipArchive
'
'
' /********************************************************************************/
2019-07-13 23:56:14 +08:00
#End Region
Imports System.IO
2019-06-29 00:43:04 +08:00
Imports System.IO.Compression
2019-08-12 23:12:53 +08:00
Imports System.Runtime.CompilerServices
2019-06-29 00:56:16 +08:00
Imports Microsoft.VisualBasic.ComponentModel.Collection
2019-06-29 00:43:04 +08:00
Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
2019-06-29 00:56:16 +08:00
Imports Microsoft.VisualBasic.Linq
2019-06-29 00:43:04 +08:00
Namespace ApplicationServices.Zip
2020-03-30 20:09:26 +08:00
''' <summary>
''' zip file stream helper
''' </summary>
Public Module ZipStreamReader
2019-06-29 00:43:04 +08:00
2019-08-12 23:12:53 +08:00
''' <summary>
''' 将zip文件中的所有数据包一次性的返回给上层调用代码
''' </summary>
''' <param name="zipFile"></param>
''' <param name="takes"></param>
''' <returns></returns>
2019-06-29 00:56:16 +08:00
Public Iterator Function LoadZipArchive(zipFile As String, Optional takes As IEnumerable(Of String) = Nothing) As IEnumerable(Of NamedValue(Of MemoryStream))
Dim takeIndex As Index(Of String) = takes.SafeQuery.ToArray
Dim entries As IEnumerable(Of ZipArchiveEntry)
2019-06-29 00:43:04 +08:00
Using zip As New ZipArchive(zipFile.Open(doClear:=False), ZipArchiveMode.Read)
2019-06-29 00:56:16 +08:00
If takes Is Nothing Then
entries = zip.Entries.OrderBy(Function(e) e.Name)
Else
entries = Iterator Function() As IEnumerable(Of ZipArchiveEntry)
For Each item In zip.Entries
If item.Name Like takeIndex Then
Call takeIndex.Delete(item.Name)
Yield item
End If
If takeIndex.Count = 0 Then
Exit For
End If
Next
End Function()
End If
For Each entry As ZipArchiveEntry In entries
2020-03-30 20:09:26 +08:00
Yield New NamedValue(Of MemoryStream) With {
.Name = entry.Name,
.Value = Decompress(file:=entry)
}
Next
End Using
End Function
2019-06-29 00:43:04 +08:00
2020-04-06 15:02:40 +08:00
<Extension>
2020-03-30 20:09:26 +08:00
Public Function Decompress(file As ZipArchiveEntry) As MemoryStream
Using ref As Stream = file.Open
Dim ms As New MemoryStream()
2019-06-29 00:43:04 +08:00
2020-03-30 20:09:26 +08:00
Call ref.CopyTo(ms)
Call ms.Seek(0, SeekOrigin.Begin)
Return ms
2019-06-29 00:43:04 +08:00
End Using
End Function
2019-08-12 23:12:53 +08:00
''' <summary>
''' 如果这个函数返回的是空值说明zip压缩文件中没有该目标
''' </summary>
''' <param name="zipFile$"></param>
''' <param name="name$"></param>
''' <returns></returns>
'''
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Public Function GetZipSubStream(zipFile$, name$) As MemoryStream
Return LoadZipArchive(zipFile, {name}).FirstOrDefault
End Function
2019-06-29 00:43:04 +08:00
End Module
2019-07-13 23:56:14 +08:00
End Namespace