This is a migrated thread and some comments may be shown as answers.

Can ZipPackage zip folders?

3 Answers 225 Views
ZipLibrary
This is a migrated thread and some comments may be shown as answers.
Matt Smith
Top achievements
Rank 1
Matt Smith asked on 05 Mar 2013, 12:28 PM
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.

3 Answers, 1 is accepted

Sort by
0
Accepted
MasterChiefMasterChef
Top achievements
Rank 2
answered on 05 Mar 2013, 06:58 PM
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
0
Matt Smith
Top achievements
Rank 1
answered on 06 Mar 2013, 12:03 AM
Master Chief,

Thanks for pointing me in the right direction.

Cheers,

Matt.
0
maxscan
Top achievements
Rank 1
answered on 21 May 2013, 09:29 AM
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\
        package.Add(subDirectory)
 
        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
        Next
 
    Next
 
    package.Close(False)
 
    memStream.Position = 0
 
    If memStream IsNot Nothing AndAlso memStream.Length > 0 Then
 
        Response.Buffer = True
        Response.Clear()
        Response.AddHeader("content-disposition", "attachment; filename=files.zip")
        Response.ContentType = "application/zip"
        Response.BinaryWrite(memStream.ToArray())
        Response.End()
 
    End If
 
End Using


Hope this helps,
Max
Tags
ZipLibrary
Asked by
Matt Smith
Top achievements
Rank 1
Answers by
MasterChiefMasterChef
Top achievements
Rank 2
Matt Smith
Top achievements
Rank 1
maxscan
Top achievements
Rank 1
Share this question
or