Noupe Editorial Team November 3rd, 2016

Dynamically Generating Zip/TGZ Files with One Line of Code

When you work on media-heavy web projects, you will have to manage large sets of images and, at some point, have to share these images with others. Sharing collections is more efficient when the images are archived because it enables easier transfer between servers and ensures that images can be delivered in a relatively smaller sized package. ziptgz-cloudinary-teaser_en You also may need to transform these sets of images before they are archived and shared. This strategy is common because it helps optimize files that are being sent from one machine to another, in scenarios such as:
  • Email - Providers, such as Gmail, now offer features that enable users to download all attachments for a given email as archived files.
  • Online schools and tutors - Companies such as Udacity and Pluralsight enable course downloads - which contain exercises, images and videos – that can created with a single click, as archived files are sent directly to your computer.
  • Automated backups - These backups are done by archiving all the media files on the server.
If your project entails uploading, transforming, zipping and sharing images, you typically have to reserve several hours to complete these tasks, writing numerous lines of code to support the efforts. The typical process typically requires:
  1. Uploading images to the server via the browser
  2. Transforming images on your server for optimization purposes
  3. Archiving images using a fairly complex logic
  4. Downloading an archived file
Each of the above step takes time and effort to complete. But it doesn't have to be so complex. A solution from Cloudinary, a leading provider of cloud-based image and video management, will enable you to condense these steps into a single line of code.

Generating Images ZIP/TGZ Files with Cloudinary

With Cloudinary, we can address the complex process of archiving and sharing images quickly and with just a line of code. Cloudinary supports generating ZIP/TGZ files using the generate_archive Upload API method. This approach can include any type of file, and enables options for determining which files to include in the archive file. For example, you can choose based on the file's name, all files in the same folder, or some other criteria. Cloudinary also enables you to apply transformations to all the images before including them in the file and set various options for generating the archive file, such as naming the file. Here's one way that Cloudinary enables you to archive and share images: https://api.cloudinary.com/v1_1/demo/image/generate_archive?api_key=373364719177799&expires_at=1557919777&mode=download&public_ids%5B%5D=fat_cat&public_ids%5B%5D=kitten&public_ids%5B%5D=hungry_cat&signature=a2f86b73d32d2a778493d6759d59059d0a30076d&timestamp=1464179836&transformations=c_scale%2Cw_200 If you click on the link above, you can download an archive of cat images. The signature parameter is required and is generated with your credentials using the Cloudinary SDK. Let's now look at performing these task in our various dev environments, using the SDK. We have two options for archiving image files:
  1. Pre-creating an image zip/TGZ using your cloud images based on specific parameters
  2. Using a dynamic URL to download existing images from your cloud as an archive, which is the approach taken with the cat image example above.
Looking more closely at these two options, we'll go into detail about the process of generating the archive link.

Pre-creating and Uploading Image ZIP/TGZ Files

To pre-create a ZIP/TGZ file, use the create_zip SDK method. With this, you can automatically upload the archive file to the cloud, and then give your users a link for downloading it. This option is best if multiple users will be downloading the resulting archive file. The pre-create option creates an archive file based on parameters you supply. The response for the archive requests gives us a URL to download the files. The following line shows how to create an archive that contains images of our cloud-tagged cat:
// JavaScript
cloudinary.v2.uploader.create_zip(
    { tags: 'cats', resource_type: 'image', 
    target_public_id: 'cute_cats.zip', transformations: {
        width: 50, height: 50, crop: 'fill'}},
    function(error,result) {console.log(result) });
// PHP
\Cloudinary\Uploader::create_zip(array(
    'tags' => 'cats', 'resource_type' => 'image', 
    'target_public_id' => 'cute_cats.zip', 'transformations' => array(
        'width' => 50, 'height' => 50, 'crop' => 'fill')));
# Ruby
Cloudinary::Uploader.create_zip(:tags => 'cats', 
    :resource_type => 'image', :target_public_id => 'cute_cats.zip',
    :transformations => {:width => 50, :height => 50, :crop => :fill})
Created zip We then can gain access to the archived file using the image method:
// JavaScript
cloudinary.image("cute_cats.zip")
// PHP
cl_image_tag("cute_cats.zip")
# Ruby
cl_image_tag("cute_cats.zip")

Dynamic URLs for Downloading Archived Images

Another option is generating a signed URL for creating a ZIP/TGZ file on-the-fly and on-demand. The ZIPTGZ file is created and sent to your user only when the URL is accessed. The resulting ZIP/TGZ file is not cached or stored in your Cloudinary account, so this option is ideal for sharing images with a single user. The SDK method to complete this is download_zip_url, which also takes parameters of the images we need to download and returns a URL to download the archived file:
// JavaScript
cloudinary.v2.utils.download_zip_url(
    { public_ids: ['cat3_tqjcms', 'cat4_atunfc', 'cat5_rp7bpa'], resource_type: 'image', transformations: {
        width: 50, height: 50, crop: 'fill'}},
    function(error,result) {console.log(result) });
// PHP
\Cloudinary\Utils::download_zip_url(
    array(
        'public_ids' => array('cat3_tqjcms', 'cat4_atunfc', 'cat5_rp7bpa', 'transformations' => array(
        'width' => 50, 'height' => 50, 'crop' => 'fill')), 
        'resource_type' => 'image'));
# Ruby
Cloudinary::Utils.download_zip_url(
    :public_ids => ['cat3_tqjcms', 'cat4_atunfc', 'cat5_rp7bpa'], 
    :resource_type => 'image',
    :transformations => {:width => 50, :height => 50, :crop => :fill})
The code above generates the URL like the shown earlier, which you can use to download the file. It's important to note that the URL contains an expiration date, which defaults to one hour, but can be extended with expires_at when setting the download option.

The Archive Alternative

We have been using the term zip and archive interchangeably in this guide. This is a very common phenomenon but they are quite different. The examples are for ZIP/TGZ files but Cloudinary also supports archives. create_archive and download_archive_url are the archive alternative API methods for pre-creating archives and downloading archives respectively. The methods receive the same arguments as the ZIP/TGZ alternatives. The difference is just in the name and the output. Using a single line of code to generate ZIP/TGZ files offers numerous benefits when it comes to organizing, streamlining, normalizing and optimizing the delivery of multiple images. Cloudinary gives you options to best fit your needs, either creating the ZIP file and uploading it to the cloud, or generating a dynamic URL that creates and delivers the ZIP/TGZ file on demand. This short guide gives you a quick and easy alternative for moving collections of images by using Cloudinary's archiving feature, which also enables you to transform and optimize images before creating an archive. Want to try it yourself? Go to the Cloudinary website and test drive the solution for free. Disclaimer: This article is sponsored by Cloudinary.

Noupe Editorial Team

The jungle is alive: Be it a collaboration between two or more authors or an article by an author not contributing regularly. In these cases you find the Noupe Editorial Team as the ones who made it. Guest authors get their own little bio boxes below the article, so watch out for these.

Leave a Reply

Your email address will not be published. Required fields are marked *