Cloudinary Blog

Dynamically generating ZIP files with one line of code

Dynamic ZIP files generation with a single line of code

As a developer, you want to allow your users to download multiple files in a single click. An easy way to download multiple files and share them is to generate a ZIP file. When images are involved, you may also want to normalize the original images before including them in the ZIP file, by scaling them down to the same maximum resolution or converting them to the same format.

In the following simple example, 3 images of cats have been uploaded to the cloud.

fat_cat.jpg fat_cat kitten.jpg kitten hungry_cat.jpg hungry_cat

With one line of code, you can generate a dynamic URL that, for example, automatically creates and then delivers a ZIP file containing the cat images, all scaled down to a width of 200 pixels:

The ability to dynamically generate and deliver ZIP files to your users by including one line of code can be useful for developers in a number of ways. For example:

  • Social and messaging apps that allow users to select multiple files to send to another recipient, who subsequently receives all the files in one ZIP file (e.g., the Gmail feature where you can download all attachments as a single ZIP file).
  • Applications that include image galleries and allow users to select multiple images and then download a ZIP file with all the selected images (e.g., as Google Photos has implemented).
  • Allowing your users to download multiple images simultaneously in a single ZIP file, where all the images have been normalized to a certain size, format or quality, (or any other image transformation you want to apply to all the images).

Generating ZIP files of images in the cloud

Cloudinary supports generating ZIP files using the generate_archive Upload API method, which can include any type of file, and offers various options for determining which files to include in the ZIP file (e.g., according to the file's name, all files in the same folder, etc). The method also allows you to apply transformations to all the images before including them in the file and set various options for generating the ZIP file (e.g., naming the file). For more information on all the options available for generating ZIP files, see the generate_archive documentation.

Cloudinary enables you to create and deliver ZIP files in one of the following two ways: * Pre-create the ZIP file and upload it to the cloud. * Generate a dynamic URL for creating and downloading a ZIP file on demand.

The Cloudinary SDKs wrap the generate_archive API method and provide 2 separate methods that accomplish these two goals.

Create a ZIP file of images

To pre-create a ZIP file, use the create_zip SDK method, which also automatically uploads the ZIP file to the cloud, and then gives your users a link for downloading it. This option is best if multiple users will be downloading the resulting ZIP file.

For example, to create a ZIP file called that contains small (50x50) thumbnails of all of the images in your account that have been tagged as "cats":

Cloudinary::Uploader.create_zip(:tags => 'cats', 
    :resource_type => 'image', :target_public_id => '',
    :transformations => {:width => 50, :height => 50, :crop => :fill})
    'tags' => 'cats', 'resource_type' => 'image', 
    'target_public_id' => '', 'transformations' => array(
        'width' => 50, 'height' => 50, 'crop' => 'fill')));
    tags = 'cats', resource_type = 'image', 
    target_public_id = '', transformations = {
        width = 50, height = 50, crop => 'fill'})
    { tags: 'cats', resource_type: 'image', 
    target_public_id: '', transformations: {
        width: 50, height: 50, crop: 'fill'}},
    function(error,result) {console.log(result) });
    ObjectUtils.asMap('tags', 'cats', 'resource_type', 'image',
    'target_public_id', '', 'transformations', 
        new Transformation().width(50).height(50).crop('fill')));
curl -X POST --data 'tags=cats$resource_type=image&'

The response to the API call includes all pertinent information about the created zip file, including the URL needed to access it, in this case:

<Image publicId="" >

<cl-image public-id="" >


Generate a dynamic URL for downloading a ZIP file on demand

Instead of pre-creating the ZIP file, you can generate a signed URL for creating a ZIP file on-the-fly and on-demand with the download_archive_url method of the Utils API. The ZIP file is created and streamed to your user only when the URL is accessed. The resulting ZIP file is not cached or stored in your Cloudinary account, so this option is best if only a single user downloads the resulting ZIP file and avoids waste if the URL is not accessed by the user.

For example, to generate a signed URL for creating and delivering a ZIP file that contains the 'fat_cat' and 'kitten' images:

    :public_ids => ['fat_cat', 'kitten'], 
    :resource_type => 'image')
        'public_ids' => array('fat_cat', 'kitten'), 
        'resource_type' => 'image'));
    public_ids = ['fat_cat', 'kitten'], 
    resource_type = 'image')
    { public_ids: ['fat_cat', 'kitten'], resource_type: 'image'},
    function(error,result) {console.log(result) });
    ObjectUtils.asMap('public_ids', Arrays.asList('fat_cat', 'kitten'), 
        'resource_type', 'image'));
curl -X POST --data 'public_ids[]=fat_cat&public_ids[]=kitten$resource_type=image&mode=download&timestamp=173719931&api_key=436464676&signature=a788d68f86a6f868af'

The API call returns the URL needed to dynamically create and deliver the ZIP file, in this case:

Dynamic ZIP files with a single line of code

Generating ZIP files with a single line of code allows you to organize, streamline, normalize and optimize multiple image delivery to your users. Either create the ZIP file and upload it to the cloud, or generate a dynamic URL that creates and delivers the ZIP file on demand. For more information on all the options available for generating ZIP files, see the generate_archive documentation. The feature is available for use with all Cloudinary accounts, including the free tier.

Recent Blog Posts

An Eye-Opening Talk: Building Apps for the Next Billion Users in Africa

William (iChuloo) Imoh, who hails from Lagos, Nigeria, recently embarked on a U.S. speaking tour, February 20-March 12, during which he powwowed with technical and product teams and communities at such renowned enterprises as Netlify, Pluralsight, Lucidchart, Twilio, and more in Salt Lake City, Dallas, Las Vegas, and San Francisco. On March 5, he gave an enlightening talk, entitled International Developers and Development: Building for the Next Billion Users at Cloudinary in Santa Clara, California. Below is a synopsis. For details, see the related slides.

Read more
The Debut of the Cloudinary Customer Advisory Board

Focus on customers has always been Cloudinary’s mantra. Because we owe them our success, we are constantly reaching out to our customers, not just for feedback on our offerings, but also for their vision, wish list, and buy-in of what Cloudinary can do to meet their needs and make them succeed. About six months ago, it occurred to us that it would be beneficial if we could meet regularly with those who are behind innovation at our key customers—executives, product gurus, developers, content managers—to swap strategies, product roadmaps, best practices, and such. In particular, we’d like to solicit actionable feedback as a foundation for our plans of product enhancements.

Read more
Media Management With the Cloudinary-Netlify CMS Integration

Static sites and the JAMstack are quickly becoming a standard for developing safe and performant websites with an optimal workflow for developers. Netlify CMS (not to be confused with the company that created it, Netlify) is an open source content management solution that works especially with static site generators such as Gatsby, Hugo, etc... enabling content storage in your Git repository along with your code for easier versioning, multichannel publishing, and direct content updates in Git.

Read more
Vitaly Friedman's Insights on Media Conferences

Vitaly Friedman is a die-hard devotee of beautiful content. Born in Minsk, Belarus, he studied Computer Science and Mathematics in Germany, unearthing in himself a passion for typography, writing, and design in the interim. After a six-year stint as a freelance designer and developer, he co-founded Smashing Magazine, a leading online publication on web design and development. You can follow SmashingMag on Twitter @SmashingMag.

Read more