Telerik blogs
In part one, we looked at how you can use RadUpload and the open source SharpZipLib to easily upload and expand zip archives. Today, we'll look at the other end of the process: creating new zip files on the server and then making them available for users to download.

To do this, we'll extend the sample we created in part one. As you recall, our sample already enables users to upload a zip file that contains image files and then the server automatically extracts all images to a target directory. We'll extend this demo by creating a text file and then zipping it up with our uploaded images. A link to download the new zip will be presented to our users when the zipping is done to complete the process.

Step 1: Identify files to zip
This step is very basic and may not be necessary if all of the files that you want to zip already exist on the server. To help demonstrate that the zip we're creating is not a copy, though, we'll create a new text file that lists all of the contents in our images directory. This text file will get wrapped-up with our images when we create our new zip archive in step 2. To create our simple text file, we'll use some code that looks like this:



Step 2: Create the new zip file
Just as there were multiple approaches to unzipping a file in part 1, there are different ways to use SharpZipLib to create a zip, too. We'll look at two approaches- the fast way and the manual way again- so that you can choose the method that works best for your projects:

  1. Approach 1: Using FastZip
    The FastZip class also contains a method for easily creating a zip file from a specified directory. The CreateZip method takes a few parameters and then easily zips an entire directory (with an option for recursive behavior that zips all sub directories, too) and saves the zip to the desired location. The code to do this is very simple:

  2. Approach 2: Using the ZipOutputStream
    For a more manual approach to creating your zip files, you can use the ZipOutputStream. Like the ZipInputStream we used to manually extract files from a zip, the OutputStream requires that we iterate through "ZipEntry"s and then use a FileStream to add file contents to the new archive. The following code will create a new zip archive with all of our target files:




    One thing to note is that this code will not preserve our directory structure in the new zip (like the FastZip method will). To do that, we'd need to add some extra code (similar to what we did in part 1 unzipping demo) to create the necessary sub folders.

As you can see once again, creating new zip files is no harder than unzipping existing files. With the SharpZipLib and RadUpload tools in your .NET toolbelt, you can confidently approach projects that deal with zip files and have no problems working with the archives. To see these tools in action in a real world scenario, check out the new batch file converter on the Telerik Code Converter. We use the same general practices outlined in this series to enable people to upload zipped code for conversion and then download the results as a single zip file. Have fun working with zips!

Download sample code used in this article


ToddAnglin_164
About the Author

Todd Anglin

Todd Anglin is Vice President of Product at Progress. Todd is responsible for leading the teams at Progress focused on NativeScript, a modern cross-platform solution for building native mobile apps with JavaScript. Todd is an author and frequent speaker on web and mobile app development. Follow Todd @toddanglin for his latest writings and industry insights.

Comments

Comments are disabled in preview mode.