0

複数のファイルを含むディレクトリがあります。このフォルダーを (Xceed サード パーティの dll ライブラリを使用して) zip に圧縮し、HTTP 経由でユーザーにプッシュしたいと考えています。同時に、フォルダー内のすべてのファイルのログを作成し、それを圧縮ファイルの一部として追加したいと考えています。

私は現在 DotNetZip を使用しており、完全に機能しています。Xceedでこれに相当するものが必要です。

以下は、DotNetZip を使用したコードです。

Imports Ionic.Zip

   ' Tell the browser we're sending a ZIP file!
    Dim downloadFileName As String = String.Format("YourDownload-{0}.zip", DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss"))
    Response.ContentType = "application/zip"
    Response.AddHeader("Content-Disposition", "filename=" & downloadFileName)
' Zip the contents of the selected files
Using zip As New ZipFile()

    ' Construct the contents of the README.txt file that will be included in this ZIP
    Dim readMeMessage As String = String.Format("Your ZIP file {0} contains the following files:{1}{1}", downloadFileName, Environment.NewLine)

    For i As Integer = 0 To MainDirs.Length - 1
        readMeMessage &= String.Concat(vbTab, "* ", MainDirs(i), Environment.NewLine)
        ' Now add the file to the ZIP (use a value of "" as the second parameter to put the files in the "root" folder)
        zip.AddFile(MainDirs(i), "Your Files")
    Next

    ' Add the README.txt file to the ZIP
    zip.AddEntry("README.txt", readMeMessage, Encoding.ASCII)

    ' Send the contents of the ZIP back to the output stream
    zip.Save(Response.OutputStream)
End Using
End Sub
4

1 に答える 1

0

Xceedサポートとのメールのやり取りの後。彼らは私が問題の適切な解決策を見つけるのを手伝ってくれてとても親切でした.

これが正常に動作するコードです。

' Tell the browser we're sending a ZIP file!
        Dim downloadFileName As String = String.Format("YourDownload-{0}.zip", DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss"))
        Response.ContentType = "application/zip"
        Response.AddHeader("Content-Disposition", "filename=" & downloadFileName)

    Dim itemHeader As New ZipItemLocalHeader()

    ' Zip the contents of the selected files
    Using zip As New ZipWriter(Response.OutputStream)

        Dim directoryInfo As New DirectoryInfo(folderpath)
        If directoryInfo.Exists Then

            ' Construct the contents of the README.txt file that will be included in this ZIP
            Dim readMeMessage As String = String.Format("Your ZIP file {0} contains the following " & MainDirs.Length & " files:{1}{1}", downloadFileName, Environment.NewLine)

            'Get files in the current directory and all subdirectories.
            Dim _files As FileInfo() = directoryInfo.GetFiles("*.*", SearchOption.AllDirectories)

            Dim buffer As Byte() = New Byte(65536) {}

            Dim count As Integer = 0
            For Each file As FileInfo In _files
                'Create ZipItemLocalHeader for current item and write to archive.
                Dim zipItemLocalHeader1 As New ZipItemLocalHeader(file.Name)
                zip.WriteItemLocalHeader(zipItemLocalHeader1)

                readMeMessage &= String.Concat(vbTab, (count + 1).ToString & "- ", Path.GetFileName(file.FullName), Environment.NewLine)

                Using fs As FileStream = file.OpenRead()
                    'Write the current item's data to the zip archive
                    zip.WriteItemData(fs, buffer, 0, buffer.Length)
                End Using

                count += 1
            Next file

            itemHeader.FileName = "README.txt"
            zip.WriteItemLocalHeader(itemHeader)

            ' Add the README.txt file to the ZIP
            Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes(readMeMessage)
            zip.WriteItemData(data)

            'Close the Zip archive. Writes the archive's central header.
            zip.CloseZipFile()
        End If

    End Using
于 2013-08-28T08:32:07.843 に答える