Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET compress a directory to a zip

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #20515 |

    I have a directory that contains several files. I want to compress this folder to a zip (using Xceed third party dll library) and push to user through HTTP. At the same time I would like to create a log of all the files inside the folder and append that as part of the compressed file.

    I am currently using DotNetZip and its working perfectly. I need the equivalant of this in Xceed.

    Imported from legacy forums. Posted by A.Salam (had 1257 views)

    User (Old forums)
    Member
    Post count: 23064

    This got solved by Fawzi with many thanks. Here is the code he corrected:

            ‘ 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)

     

           
    Xceed.Zip.Licenser.LicenseKey = “ZRT45-2WBMK-STUBD-XB6A”

     

            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 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) {}

     

                    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)

     

                       
    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

                   
    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 

    Imported from legacy forums. Posted by A.Salam (had 463 views)

    User (Old forums)
    Member
    Post count: 23064

    Very practical code, thanks for sharing, bookmark…..

    Imported from legacy forums. Posted by Uchiha (had 822 views)

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.