将多个文件写到一个文件(并合文件),也可从该并合文件中获得其中某个文件的流(具体不多写了,压缩功能未加进,有兴趣留言)
2008-11-02 00:28
288 查看
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Public Class Form1
Dim cfm As New CoalescentFileManager
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each x In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyPictures, FileIO.SearchOption.SearchTopLevelOnly, ("*.jpg"))
cfm.AddSourceFile(x)
Console.WriteLine(x)
Next
'
cfm.WriteCoalescentFile(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "/" & "xx.dat")
cfm.LoadCoalescentFile(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "/" & "xx.dat")
ListBox1.Items.AddRange(cfm.CurrentCoalescentFile.NameList)
'cfm.CurrentCoalescentFile.RevertCoalescentFileToDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "/xx/")
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Me.BackgroundImage = Image.FromStream(cfm.CurrentCoalescentFile.GetStream(ListBox1.SelectedIndex))
End Sub
End Class
Public Class CoalescentFileManager
Dim _fullFilenameList As New List(Of String)
Dim _coaFileLoaded As Boolean
Dim _cf As CoalescentFile
Sub New()
End Sub
Sub AddSourceFile(ByVal source As String)
If Not File.Exists(source) Then Return
_fullFilenameList.Add(source)
End Sub
Public Sub WriteCoalescentFile(ByVal filename As String)
WriteCoalescentFile(filename, False)
End Sub
Public Sub WriteCoalescentFile(ByVal filename As String, ByVal useDeflate As Boolean)
Using fs As New FileStream(filename, FileMode.Create, FileAccess.ReadWrite)
Dim bw As New BinaryWriter(fs)
'
bw.Write("CoalescentFile")
'
bw.Write(useDeflate)
'占用了4字节
bw.Write(_fullFilenameList.Count)
For Each f In _fullFilenameList
Dim fi As New FileInfo(f)
'每个占用8字节
bw.Write(fi.Length)
fi = Nothing
Next
For Each f In _fullFilenameList
bw.Write(Path.GetFileName(f))
Next
For Each f In _fullFilenameList
bw.Write(File.ReadAllBytes(f))
bw.Flush()
Next
bw.Close()
End Using
End Sub
Public Sub LoadCoalescentFile(ByVal coalescentFile As String)
If Not File.Exists(coalescentFile) Then
Throw New InvalidCoalescentFileException("所指示文件不存在。")
End If
Using fs As New FileStream(coalescentFile, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
Dim headStr As String = br.ReadString
If headStr <> "CoalescentFile" Then
Throw New InvalidCoalescentFileException("该文件不是有效的合并文件。")
End If
End Using
_cf = New CoalescentFile(coalescentFile)
_coaFileLoaded = True
End Sub
Public ReadOnly Property CurrentCoalescentFile() As CoalescentFile
Get
Return _cf
End Get
End Property
Public Class CoalescentFile
Dim _shortNameList As List(Of String)
Dim _fileLengthList As List(Of Long)
Dim _fileCount As Integer
Dim _isZip As Boolean
Dim _cf As String
Dim _contentStartPos As Long
Sub New(ByVal cf As String)
_shortNameList = New List(Of String)
_fileLengthList = New List(Of Long)
_cf = cf
Using fs As New FileStream(_cf, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
br.ReadString()
_isZip = br.ReadBoolean
_fileCount = br.ReadInt32
For i = 1 To _fileCount
_fileLengthList.Add(br.ReadInt64())
Next
For i = 1 To _fileCount
_shortNameList.Add(br.ReadString)
Next
_contentStartPos = fs.Position
br.Close()
End Using
End Sub
Public Sub RevertCoalescentFileToDirectory(ByVal dir As String)
If Not Directory.Exists(dir) Then Directory.CreateDirectory(dir)
Using fs As New FileStream(_cf, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
fs.Position = _contentStartPos
For i = 0 To _fileCount - 1
Dim buffer = br.ReadBytes(_fileLengthList(i))
File.WriteAllBytes(dir & _shortNameList(i), buffer)
Next
br.Close()
End Using
End Sub
Public Sub WriteOneToFile(ByVal index As Integer, ByVal file As String)
Using fs As New FileStream(file, FileMode.Create, FileAccess.Write)
Using mems As MemoryStream = GetStream(index)
Dim bw As New BinaryWriter(fs)
'若用 memoryStream.GetBuffer 方法则会在末尾多出一些 空符(字符码为零)
bw.Write(mems.ToArray)
bw.Close()
End Using
End Using
End Sub
Public Function GetStream(ByVal index As Integer) As MemoryStream
Dim startPos As Long = _contentStartPos
For i = 0 To index - 1
startPos += _fileLengthList(i)
Next
Dim buffer As Byte()
Using fs As New FileStream(_cf, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
fs.Position = startPos
buffer = br.ReadBytes(_fileLengthList(index))
br.Close()
End Using
Dim mems As MemoryStream
mems = New MemoryStream(buffer)
Erase buffer
GC.Collect()
Return mems
End Function
Public ReadOnly Property NameList() As String()
Get
Return _shortNameList.ToArray
End Get
End Property
Public ReadOnly Property FileCount() As Integer
Get
Return _fileCount
End Get
End Property
Public ReadOnly Property CurrentFile() As String
Get
Return _cf
End Get
End Property
Public ReadOnly Property IsDeflate() As Boolean
Get
Return _isZip
End Get
End Property
End Class
End Class
Public Class InvalidCoalescentFileException
Inherits Exception
Sub New(ByVal text As String)
MyBase.New(text)
End Sub
End Class
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Public Class Form1
Dim cfm As New CoalescentFileManager
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each x In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyPictures, FileIO.SearchOption.SearchTopLevelOnly, ("*.jpg"))
cfm.AddSourceFile(x)
Console.WriteLine(x)
Next
'
cfm.WriteCoalescentFile(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "/" & "xx.dat")
cfm.LoadCoalescentFile(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "/" & "xx.dat")
ListBox1.Items.AddRange(cfm.CurrentCoalescentFile.NameList)
'cfm.CurrentCoalescentFile.RevertCoalescentFileToDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "/xx/")
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Me.BackgroundImage = Image.FromStream(cfm.CurrentCoalescentFile.GetStream(ListBox1.SelectedIndex))
End Sub
End Class
Public Class CoalescentFileManager
Dim _fullFilenameList As New List(Of String)
Dim _coaFileLoaded As Boolean
Dim _cf As CoalescentFile
Sub New()
End Sub
Sub AddSourceFile(ByVal source As String)
If Not File.Exists(source) Then Return
_fullFilenameList.Add(source)
End Sub
Public Sub WriteCoalescentFile(ByVal filename As String)
WriteCoalescentFile(filename, False)
End Sub
Public Sub WriteCoalescentFile(ByVal filename As String, ByVal useDeflate As Boolean)
Using fs As New FileStream(filename, FileMode.Create, FileAccess.ReadWrite)
Dim bw As New BinaryWriter(fs)
'
bw.Write("CoalescentFile")
'
bw.Write(useDeflate)
'占用了4字节
bw.Write(_fullFilenameList.Count)
For Each f In _fullFilenameList
Dim fi As New FileInfo(f)
'每个占用8字节
bw.Write(fi.Length)
fi = Nothing
Next
For Each f In _fullFilenameList
bw.Write(Path.GetFileName(f))
Next
For Each f In _fullFilenameList
bw.Write(File.ReadAllBytes(f))
bw.Flush()
Next
bw.Close()
End Using
End Sub
Public Sub LoadCoalescentFile(ByVal coalescentFile As String)
If Not File.Exists(coalescentFile) Then
Throw New InvalidCoalescentFileException("所指示文件不存在。")
End If
Using fs As New FileStream(coalescentFile, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
Dim headStr As String = br.ReadString
If headStr <> "CoalescentFile" Then
Throw New InvalidCoalescentFileException("该文件不是有效的合并文件。")
End If
End Using
_cf = New CoalescentFile(coalescentFile)
_coaFileLoaded = True
End Sub
Public ReadOnly Property CurrentCoalescentFile() As CoalescentFile
Get
Return _cf
End Get
End Property
Public Class CoalescentFile
Dim _shortNameList As List(Of String)
Dim _fileLengthList As List(Of Long)
Dim _fileCount As Integer
Dim _isZip As Boolean
Dim _cf As String
Dim _contentStartPos As Long
Sub New(ByVal cf As String)
_shortNameList = New List(Of String)
_fileLengthList = New List(Of Long)
_cf = cf
Using fs As New FileStream(_cf, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
br.ReadString()
_isZip = br.ReadBoolean
_fileCount = br.ReadInt32
For i = 1 To _fileCount
_fileLengthList.Add(br.ReadInt64())
Next
For i = 1 To _fileCount
_shortNameList.Add(br.ReadString)
Next
_contentStartPos = fs.Position
br.Close()
End Using
End Sub
Public Sub RevertCoalescentFileToDirectory(ByVal dir As String)
If Not Directory.Exists(dir) Then Directory.CreateDirectory(dir)
Using fs As New FileStream(_cf, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
fs.Position = _contentStartPos
For i = 0 To _fileCount - 1
Dim buffer = br.ReadBytes(_fileLengthList(i))
File.WriteAllBytes(dir & _shortNameList(i), buffer)
Next
br.Close()
End Using
End Sub
Public Sub WriteOneToFile(ByVal index As Integer, ByVal file As String)
Using fs As New FileStream(file, FileMode.Create, FileAccess.Write)
Using mems As MemoryStream = GetStream(index)
Dim bw As New BinaryWriter(fs)
'若用 memoryStream.GetBuffer 方法则会在末尾多出一些 空符(字符码为零)
bw.Write(mems.ToArray)
bw.Close()
End Using
End Using
End Sub
Public Function GetStream(ByVal index As Integer) As MemoryStream
Dim startPos As Long = _contentStartPos
For i = 0 To index - 1
startPos += _fileLengthList(i)
Next
Dim buffer As Byte()
Using fs As New FileStream(_cf, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
fs.Position = startPos
buffer = br.ReadBytes(_fileLengthList(index))
br.Close()
End Using
Dim mems As MemoryStream
mems = New MemoryStream(buffer)
Erase buffer
GC.Collect()
Return mems
End Function
Public ReadOnly Property NameList() As String()
Get
Return _shortNameList.ToArray
End Get
End Property
Public ReadOnly Property FileCount() As Integer
Get
Return _fileCount
End Get
End Property
Public ReadOnly Property CurrentFile() As String
Get
Return _cf
End Get
End Property
Public ReadOnly Property IsDeflate() As Boolean
Get
Return _isZip
End Get
End Property
End Class
End Class
Public Class InvalidCoalescentFileException
Inherits Exception
Sub New(ByVal text As String)
MyBase.New(text)
End Sub
End Class
相关文章推荐
- 使用反射机制 来把一个具体的bean 处理成一个map。并且获得其中的field和值
- 编写一个类,该类具有统计文件长度,实现文件复制的函数。 其中复制功能函数如: void copy(string srcFileName,string targetFileName);
- 使用反射机制 来把一个具体的bean 处理成一个map。并且获得其中的field和值
- 编写一个类,该类具有统计文件长度,实现文件复制的函数。 其中复制功能函数如: void copy(string srcFileName,string targetFileName);
- java生成一个XML文件,并且将该文件压缩成ZIP格式后再写到硬盘上
- JAVA编写的浏览器,在别人的基础上做了些许修改,实现了前进,后退,刷新功能,添加了一个搜索框,具体情况在运行结果中的文件-->注意中
- 根据公式计算y的值。其中∑表示求各项的和,∏表示求各项的积。定义一个类F,实现上述功能。具体要求如下:
- 字符串空格的压缩:给定一个字符串,将其中连续出现的空格压缩为1个后,将其中已空格分隔的每个字符串逆序打印出来
- 一个压缩解压缩文件的dll的声明说明
- web播放器插件代码 实现一个在线查听音频文件的功能
- java实现图片或文件的上传功能具体思路及代码
- \t\t启用IIS的压缩网页文件功能 图文讲解
- 基本字符串压缩 利用字符重复出现的次数,编写一个方法,实现基本的字符串压缩功能。比如,字符串“aabcccccaaa”经压缩会变成“a2b1c5a3”。若压缩后的字符串没有变短,则返回原先的字符串。
- 关闭Windows XP/2003 的ZIP压缩文件读写功能
- 假设一个文本文件中保存着100个整数,请将这100个整数按从小到大的顺序写到另一个文件中
- 一个获取rgb的小程序,有兴趣的可以交流交流,好多地方还不够完美,不过功能可以实现(C# winForm)
- 压缩网站的css和js,合并多个文件到一个文件
- java 多文件压缩成一个zip压缩包
- SharePoint 2016 vs部署报错:无法加载功能xxx未能加载文件或程序集xxx或它的某一个依赖项。系统找不到指定的文件
- QT4 一个简单的打开文件功能