Developer forum

Forum » CMS - Standard features » Is it possible to zip all files in a folder, on demand?

Is it possible to zip all files in a folder, on demand?

Hans Ravnsfjall
Hans Ravnsfjall
Reply

What we have, is a client who has several files in a folder - and they link to these files on different occations. Now they are asking if it is possible to zip all files in a folder and linking to it, without having to manually creating/uploading a zip file. Is this possible somehow? Eg with url parameters  something like this - www.website.com/?folder=/Files/Folder&zip=true&download=true

Or in some other way in eg. a Razor template?

/Hans


Replies

 
Nicolai Pedersen
Reply

Hi Hans

It is only possible from the backend.

Here is some code that you can use that utilizes Dynamicweb.Content.Files namespace (9.5+).

Be aware that this is a security issue if expose this in the frontend for users not logged in. Be careful!

Sub DoDownload()
        If Not String.IsNullOrEmpty(_folder) Then
            _file = _folder.Substring(_folder.LastIndexOf("/") + 1) + ".zip"
            _folder = HttpContext.Current.Server.MapPath(_folder)
            Dim fi As New IO.FileInfo($"{_folder}\{IO.Path.GetRandomFileName()}")
            For i As Integer = 0 To 10
                If fi.Exists Then
                    fi = New IO.FileInfo($"{_folder}\{IO.Path.GetRandomFileName()}")
                Else
                    Exit For
                End If
            Next

            Try
                ZipFile.ZipFolder(New IO.DirectoryInfo(_folder), fi)
                Response.Clear()
                Response.ContentType = "text/html"
                Response.AppendHeader("Content-Disposition", $"attachment; filename=""{_file}""")
                Response.TransmitFile(fi.FullName)
                Response.Flush()
            Finally
                fi.Delete()
            End Try

            Response.Close()
        End If
    End Sub

 

You must be logged in to post in the forum