Can ZipPackage zip folders?

4 posts, 1 answers
  1. Matt Smith
    Matt Smith avatar
    19 posts
    Member since:
    Dec 2005

    Posted 05 Mar 2013 Link to this post

    Does anyone know if the ZipPackage can accept a folder or set of folders and build a valid zip file from them? I haven't seen any examples or documentation on this.
  2. Answer
    MasterChiefMasterChef avatar
    152 posts
    Member since:
    Jan 2013

    Posted 05 Mar 2013 Link to this post

    Hi Matt,

    You can look here in the documentation all of the available methods and constructors available for the RadZipPackage. When using the Add() method, you simply need to send the file stream for a specified folder and it will zip up the whole file. 

    If this doesn't work, you can create a ZipPachage in the folder you wish to zip and add each item inside one at a time, resulting in the same information being zipped as it would have if the whole file can be zipped.

    Hopefully this helps,
    Master Chief
  3. Matt Smith
    Matt Smith avatar
    19 posts
    Member since:
    Dec 2005

    Posted 05 Mar 2013 Link to this post

    Master Chief,

    Thanks for pointing me in the right direction.


  4. maxscan
    maxscan avatar
    1 posts
    Member since:
    Nov 2011

    Posted 21 May 2013 Link to this post

    Hi Matt

    You need to add the directory to the zip package first, then add the files with the fileNameInZip set to a relative path...

    This code works for me:

    'Get all the sub-directories in a root directory - eg c:\temp\
    Dim subDirectories() As String = Directory.GetDirectories(Server.MapPath("[PATH TO ROOT DIRECTORY]"))
    Dim memStream As New MemoryStream()
    Using memStream
        Dim package As ZipPackage = ZipPackage.Create(memStream)
        For Each subDirectory As String In subDirectories
            'Add the sub-directory - eg c:\temp\sub-directory\
            Dim files() As String = Directory.GetFiles(subDirectory)
            For Each filepath As String In files
                If File.Exists(filepath) Then
                    'Split the path up so you can get the folder name
                    Dim elements() As String = filepath.Split("\")
                    'Add each file, set the fileNameInZip to include the
                    'sub-directory name - eg sub-directory\filename.doc
                    Dim fileNameInZip As String = _
                    (elements(elements.Length - 2) & "\" & elements(elements.Length - 1))
                    package.Add(filepath, fileNameInZip)
                End If
        memStream.Position = 0
        If memStream IsNot Nothing AndAlso memStream.Length > 0 Then
            Response.Buffer = True
            Response.AddHeader("content-disposition", "attachment;")
            Response.ContentType = "application/zip"
        End If
    End Using

    Hope this helps,
Back to Top