我有从 VB.NET 开发的应用程序按日期创建的日志文件,例如“Product.15082012.txt”、“Product.16082012.txt”和“Service.15082012.txt”、“Service.16082012.txt”下的“ C:\日志”。创建的日志文件格式为“Product.ddMMyyyy.txt”和“Service.ddMMyyyy.txt”
我需要遍历文件夹“C:\Logs”中过去 6 个月的日志文件,并将其压缩为“C:\Logs\Archive”下的“archive.15082012.zip”和“archive.16082012.zip”通过单独的存档应用程序。
我的意思是在遍历文件夹时,它应该按日期将产品和服务压缩到一个 zip 文件中。
我怎样才能做到这一点?我知道如何压缩文件,但不知道如何按日期提取文件并将“Product.ddMMyyyy.txt”和“Service.ddMMyyyy.txt”分组
Private Sub AddToArchive(ByVal zip As Package, ByVal fileToAdd As String)
'Replace spaces with an underscore (_)
Dim uriFileName As String = fileToAdd.Replace(" ", "_")
'A Uri always starts with a forward slash "/"
Dim zipUri As String = String.Concat("/", _
IO.Path.GetFileName(uriFileName))
Dim partUri As New Uri(zipUri, UriKind.Relative)
Dim contentType As String = _
Net.Mime.MediaTypeNames.Application.Zip
'The PackagePart contains the information:
' Where to extract the file when it's extracted (partUri)
' The type of content stream (MIME type): (contentType)
' The type of compression: (CompressionOption.Normal)
Dim pkgPart As PackagePart = zip.CreatePart(partUri, _
contentType, CompressionOption.Normal)
'Read all of the bytes from the file to add to the zip file
Dim bites As Byte() = File.ReadAllBytes(fileToAdd)
'Compress and write the bytes to the zip file
pkgPart.GetStream().Write(bites, 0, bites.Length)
End Sub