我有从VB.NET开发的应用程序日期创建的日志文件,如“Product.15082012.txt”,“Product.16082012.txt”和“Service.15082012.txt”,“Service.16082012.txt” “在”C:\ Logs“下。创建的日志文件格式为“Product.ddMMyyyy.txt”和“Service.ddMMyyyy.txt”
我需要在过去6个月内循环浏览“C:\ Logs”文件夹中的日志文件,并将其压缩为“archive.15082012.zip”和“archive.16082012.zip”在“C:\ Logs”下\存档“通过单独的存档应用程序。
我的意思是在循环浏览文件夹时,它应该将产品和服务压缩在一个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
答案 0 :(得分:1)
请在下面找到适用于您的方案的示例。
Private Shared Function GetZipFile(ByVal zipFileNameWithPath As String, ByVal parallelDeflateThreshold As Integer) As ZipFile
Dim zip = New ZipFile(zipFileNameWithPath)
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression
' For using multiple threads to compress the files.
'-1 for single thread and never use parallel deflat
'0 for always use parallel deflate
zip.ParallelDeflateThreshold = parallelDeflateThreshold
'For very larger files of size more than 2GB after compression
zip.UseZip64WhenSaving = Zip64Option.AsNecessary
Return (zip)
End Function
Public Shared Sub ZipFiles(ByVal filesToZip As List(Of System.IO.FileInfo), ByVal zipFileNameWithPath As String, ByVal parallelDeflateThreshold As Integer)
Dim fileInfo As New System.IO.FileInfo(zipFileNameWithPath)
If fileInfo IsNot Nothing Then
fileInfo.Delete()
End If
Using zip = GetZipFile(zipFileNameWithPath, parallelDeflateThreshold)
For Each file In filesToZip
zip.AddFile(file.FullName, String.Empty)
Next
zip.Save()
End Using
End Sub
Public Shared Sub GetFiles(ByVal folderPath As String)
Dim directory As New System.IO.DirectoryInfo(folderPath)
Dim startDate As DateTime = DateTime.Now.AddMonths(-6)
Dim datesFilterList As New Dictionary(Of String, String)
While startDate <= DateTime.Now
datesFilterList.Add(startDate.ToString("ddMMyyyy"), String.Concat("*.", startDate.ToString("ddMMyyyy"), ".txt"))
startDate = startDate.AddDays(1)
End While
For Each filter As KeyValuePair(Of String, String) In datesFilterList
Dim files As System.IO.FileInfo() = directory.GetFiles(filter.Value)
If files.Count > 0 Then
Dim zipFileName As String = String.Concat("\archive.", filter.Key.ToString(), ".zip")
ZipFiles(files.ToList(), String.Concat(folderPath, "\Archive", zipFileName), 0)
End If
Next
End Sub
您需要引用以下Iconic.Zip.dll,可以从here下载。
注意:我假设您的文件夹结构如下所示: