0
下面是一個代碼示例,用於壓縮目錄中7天以前的文件,並創建一個具有相同名稱的壓縮文件,然後刪除現有的文件。在這個例子中,我們將使用.txt文件,但你可以改變它以適應。如何使用ZipSharpLib壓縮文件並使用現有文件名作爲新的Zip文件名(asp.net VB)
例如
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
希望它是有益的!
你有什麼問題。你在製作壓縮文件,命名壓縮文件,刪除文本文件等方面遇到麻煩嗎? –
你可以發佈你的代碼來壓縮文件嗎? –
究竟是什麼問題? (除了缺少的刪除部分)。我測試了它,它工作正常。它創建單獨的文件。 –