以下是在目录中压缩7天以上文件并创建具有相同名称的压缩文件的代码示例,然后删除现有文件。在此示例中,我们将使用.txt文件,但您可以将其更改为适合。
e.g。
myfile1.txt ---> myfile1.zip --->删除myfile1.txt
myfile2.txt ---> myfile2.zip --->删除myfile2.txt
Dim NumDays As Integer = 7
Dim reportDbPath As String = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName() & "\report"
Dim fileEntries As String() = Directory.GetFiles(reportDbPath)
For Each fileName In fileEntries
If Path.GetExtension(fileName) = ".txt" AndAlso Directory.GetCreationTime(fileName) < DateAdd(DateInterval.Day, -NumDays, Now()) Then
Dim fi As New FileInfo(fileName)
Dim archiveFileName As String = Path.GetFileNameWithoutExtension(fileName) & ".zip"
Dim fsOut As FileStream = File.Create(Path.Combine(reportDbPath, archiveFileName))
Dim zipStream As New ZipOutputStream(fsOut)
zipStream.SetLevel(9)
Dim newEntry As New ZipEntry(ZipEntry.CleanName(Path.GetFileName(fileName)))
newEntry.DateTime = fi.LastWriteTime
newEntry.Size = fi.Length
zipStream.PutNextEntry(newEntry)
Dim buffer As Byte() = New Byte(4095) {}
Using streamReader As FileStream = File.OpenRead(fileName)
StreamUtils.Copy(streamReader, zipStream, buffer)
End Using
zipStream.CloseEntry()
zipStream.IsStreamOwner = True
zipStream.Close()
File.Delete(fileName)
End If
Next fileName
希望它有用!
答案 0 :(得分:0)
已经有正确的代码,所以问题不需要回答 - 转换成HOWTO