利用程式碼將多個檔案做成一個壓縮檔(zip,PutNextEntry)

  • 15156
  • 0
  • 2012-11-19

利用程式碼將多個檔案做成一個壓縮檔

 

這是一個將 SharpZipLib - .NET Zip Library
 
改寫過來的.dll,用法基本上差不多,只是宣告名稱變了些,以下介紹步驟:
 
步驟一:先將要把多個壓縮的檔案寫出來,隨便創一個資料夾放在一個user看不見的地方,並且呼叫ZipAllFilesAndDownload()準備將所有檔案壓縮在一起
'動態建立資料夾,但是下次開啟登入網頁時會自動在Global.asax.vb裡面進行刪除資料夾
            Dim strFileDirPath As String = Server.MapPath("~\Temp\" & DateTime.Now.ToString("yyyyMMdd hhmmss") & "_" & Session("UID").ToString & "_" & "ZipAndDownload")
            If Not System.IO.Directory.Exists(strFileDirPath) Then
                System.IO.Directory.CreateDirectory(strFileDirPath)
            End If           
            For k As Integer = 0 To 5
                Dim sw As System.IO.StreamWriter = System.IO.File.CreateText(strFileDirPath & "\LbrJoinRpt" & k.ToString & ".doc")
                sw.Write(wrpt.RtfText)
                sw.Close()
            Next
            '到某路徑將所有檔案加入壓縮檔並且下載壓縮檔
            Salary.ZipAllFilesAndDownload(strFileDirPath, "testZip.zip")
 
步驟二:ZipAllFilesAndDownload()讀取資料夾下面所有的檔案為二進位資料,並且寫入.zip檔案,然後跳出視窗問使用者要不要下載.zip
Imports ICSharpCode.SharpZipLib.Core
Imports ICSharpCode.SharpZipLib.Zip


 Public Shared Sub ZipAllFilesAndDownload(ByVal strFileDir As String, ByVal strZipFileName As String)

        '抓取該路徑下所有檔案
        Dim strFileDirEnteries() As String = System.IO.Directory.GetFileSystemEntries(strFileDir)

        '指定.zip檔案名稱
        Dim strmZipOutputStream As ZipOutputStream = New ZipOutputStream(IO.File.Create(strFileDir & "\" & strZipFileName))
        For Each strEntryFileName As String In strFileDirEnteries
            REM Compression Level: 0-9
            REM 0: no(Compression)
            REM 9: maximum compression            
            strmZipOutputStream.SetLevel(9)
            '將檔案讀取為二進位資料byte()        
            Dim byteBuffer() As Byte = IO.File.ReadAllBytes(strEntryFileName)

            '將此zipEntry加入到.zip之中
            Dim strShortFileName As String = strEntryFileName.Replace(strFileDir + "\", "")
            Dim objZipEntry As ZipEntry = New ZipEntry(strShortFileName)
            objZipEntry.DateTime = DateTime.Now
            objZipEntry.Size = byteBuffer.Length
            strmZipOutputStream.PutNextEntry(objZipEntry)
            strmZipOutputStream.Write(byteBuffer, 0, byteBuffer.Length)

        Next
        strmZipOutputStream.Finish()
        strmZipOutputStream.Close()
        With HttpContext.Current.Response
            .ContentType = "application/zip"
            .AddHeader("Content-Disposition", "attachment; filename=""" & HttpUtility.UrlEncode(strZipFileName, System.Text.Encoding.Default) & """")
            .Clear()
            .WriteFile(strFileDir & "\" & strZipFileName)
            .End()
        End With

    End Sub
 
步驟三:在global.asax.vb加入程式碼,讓每次開啟應用程式都會刪除這些暫存檔案
 
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' 在每個要求開頭引發
        Salary.ClearDirectory(Server.MapPath("\\HRWEB\Temp"), "*ZipAndDownload")
    End Sub
 
ClearDirectory()會刪除某路徑之下所有暫存的或是已經用不到的目錄,而且目錄名稱需符合pattern規則
 
 Public Shared Sub ClearDirectory(ByVal strPath As String, ByVal pattern As String)      
        Dim strManyPaths() As String = System.IO.Directory.GetDirectories(strPath, pattern)
        Dim todayString As String = DateTime.Now.ToString("yyyyMMdd")
        For Each strSubPath As String In strManyPaths
            If Not strSubPath.Contains(todayString) Then
                My.Computer.FileSystem.DeleteDirectory(strSubPath, FileIO.DeleteDirectoryOption.DeleteAllContents)
            End If

        Next
    End Sub

有相關性的文章:

[asp.net]將一個檔案複製到另外一個資料夾