将多个文件写到一个文件(并合文件),也可从该并合文件中获得其中某个文件的流(具体不多写了,压缩功能未加进,有兴趣留言)

  1. Imports System.IO
  2. Imports System.Runtime.InteropServices
  3. Imports System.ComponentModel
  4. Public Class Form1
  5.     Dim cfm As New CoalescentFileManager
  6.     Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load
  7.         For Each x In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyPictures, FileIO.SearchOption.SearchTopLevelOnly, ("*.jpg"))
  8.             cfm.AddSourceFile(x)
  9.             Console.WriteLine(x)
  10.         Next
  11.         '
  12.         cfm.WriteCoalescentFile(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "/" & "xx.dat")
  13.         cfm.LoadCoalescentFile(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "/" & "xx.dat")
  14.         ListBox1.Items.AddRange(cfm.CurrentCoalescentFile.NameList)
  15.         'cfm.CurrentCoalescentFile.RevertCoalescentFileToDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "/xx/")
  16.     End Sub
  17.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As ObjectByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
  18.         Me.BackgroundImage = Image.FromStream(cfm.CurrentCoalescentFile.GetStream(ListBox1.SelectedIndex))
  19.     End Sub
  20. End Class
  21. Public Class CoalescentFileManager
  22.     Dim _fullFilenameList As New List(Of String)
  23.     Dim _coaFileLoaded As Boolean
  24.     Dim _cf As CoalescentFile
  25.     Sub New()
  26.     End Sub
  27.     Sub AddSourceFile(ByVal source As String)
  28.         If Not File.Exists(source) Then Return
  29.         _fullFilenameList.Add(source)
  30.     End Sub
  31.     Public Sub WriteCoalescentFile(ByVal filename As String)
  32.         WriteCoalescentFile(filename, False)
  33.     End Sub
  34.     Public Sub WriteCoalescentFile(ByVal filename As StringByVal useDeflate As Boolean)
  35.         Using fs As New FileStream(filename, FileMode.Create, FileAccess.ReadWrite)
  36.             Dim bw As New BinaryWriter(fs)
  37.             '
  38.             bw.Write("CoalescentFile")
  39.             '
  40.             bw.Write(useDeflate)
  41.             '占用了4字节
  42.             bw.Write(_fullFilenameList.Count)
  43.             For Each f In _fullFilenameList
  44.                 Dim fi As New FileInfo(f)
  45.                 '每个占用8字节
  46.                 bw.Write(fi.Length)
  47.                 fi = Nothing
  48.             Next
  49.             For Each f In _fullFilenameList
  50.                 bw.Write(Path.GetFileName(f))
  51.             Next
  52.             For Each f In _fullFilenameList
  53.                 bw.Write(File.ReadAllBytes(f))
  54.                 bw.Flush()
  55.             Next
  56.             bw.Close()
  57.         End Using
  58.     End Sub
  59.     Public Sub LoadCoalescentFile(ByVal coalescentFile As String)
  60.         If Not File.Exists(coalescentFile) Then
  61.             Throw New InvalidCoalescentFileException("所指示文件不存在。")
  62.         End If
  63.         Using fs As New FileStream(coalescentFile, FileMode.Open, FileAccess.Read)
  64.             Dim br As New BinaryReader(fs)
  65.             Dim headStr As String = br.ReadString
  66.             If headStr <> "CoalescentFile" Then
  67.                 Throw New InvalidCoalescentFileException("该文件不是有效的合并文件。")
  68.             End If
  69.         End Using
  70.         _cf = New CoalescentFile(coalescentFile)
  71.         _coaFileLoaded = True
  72.     End Sub
  73.     Public ReadOnly Property CurrentCoalescentFile() As CoalescentFile
  74.         Get
  75.             Return _cf
  76.         End Get
  77.     End Property
  78.     Public Class CoalescentFile
  79.         Dim _shortNameList As List(Of String)
  80.         Dim _fileLengthList As List(Of Long)
  81.         Dim _fileCount As Integer
  82.         Dim _isZip As Boolean
  83.         Dim _cf As String
  84.         Dim _contentStartPos As Long
  85.         Sub New(ByVal cf As String)
  86.             _shortNameList = New List(Of String)
  87.             _fileLengthList = New List(Of Long)
  88.             _cf = cf
  89.             Using fs As New FileStream(_cf, FileMode.Open, FileAccess.Read)
  90.                 Dim br As New BinaryReader(fs)
  91.                 br.ReadString()
  92.                 _isZip = br.ReadBoolean
  93.                 _fileCount = br.ReadInt32
  94.                 For i = 1 To _fileCount
  95.                     _fileLengthList.Add(br.ReadInt64())
  96.                 Next
  97.                 For i = 1 To _fileCount
  98.                     _shortNameList.Add(br.ReadString)
  99.                 Next
  100.                 _contentStartPos = fs.Position
  101.                 br.Close()
  102.             End Using
  103.         End Sub
  104.         Public Sub RevertCoalescentFileToDirectory(ByVal dir As String)
  105.             If Not Directory.Exists(dir) Then Directory.CreateDirectory(dir)
  106.             Using fs As New FileStream(_cf, FileMode.Open, FileAccess.Read)
  107.                 Dim br As New BinaryReader(fs)
  108.                 fs.Position = _contentStartPos
  109.                 For i = 0 To _fileCount - 1
  110.                     Dim buffer = br.ReadBytes(_fileLengthList(i))
  111.                     File.WriteAllBytes(dir & _shortNameList(i), buffer)
  112.                 Next
  113.                 br.Close()
  114.             End Using
  115.         End Sub
  116.         Public Sub WriteOneToFile(ByVal index As IntegerByVal file As String)
  117.             Using fs As New FileStream(file, FileMode.Create, FileAccess.Write)
  118.                 Using mems As MemoryStream = GetStream(index)
  119.                     Dim bw As New BinaryWriter(fs)
  120.                     '若用 memoryStream.GetBuffer 方法则会在末尾多出一些  空符(字符码为零)
  121.                     bw.Write(mems.ToArray)
  122.                     bw.Close()
  123.                 End Using
  124.             End Using
  125.         End Sub
  126.         Public Function GetStream(ByVal index As IntegerAs MemoryStream
  127.             Dim startPos As Long = _contentStartPos
  128.             For i = 0 To index - 1
  129.                 startPos += _fileLengthList(i)
  130.             Next
  131.             Dim buffer As Byte()
  132.             Using fs As New FileStream(_cf, FileMode.Open, FileAccess.Read)
  133.                 Dim br As New BinaryReader(fs)
  134.                 fs.Position = startPos
  135.                 buffer = br.ReadBytes(_fileLengthList(index))
  136.                 br.Close()
  137.             End Using
  138.             Dim mems As MemoryStream
  139.             mems = New MemoryStream(buffer)
  140.             Erase buffer
  141.             GC.Collect()
  142.             Return mems
  143.         End Function
  144.         Public ReadOnly Property NameList() As String()
  145.             Get
  146.                 Return _shortNameList.ToArray
  147.             End Get
  148.         End Property
  149.         Public ReadOnly Property FileCount() As Integer
  150.             Get
  151.                 Return _fileCount
  152.             End Get
  153.         End Property
  154.         Public ReadOnly Property CurrentFile() As String
  155.             Get
  156.                 Return _cf
  157.             End Get
  158.         End Property
  159.         Public ReadOnly Property IsDeflate() As Boolean
  160.             Get
  161.                 Return _isZip
  162.             End Get
  163.         End Property
  164.     End Class
  165. End Class
  166. Public Class InvalidCoalescentFileException
  167.     Inherits Exception
  168.     Sub New(ByVal text As String)
  169.         MyBase.New(text)
  170.     End Sub
  171. End Class
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值