How to Zip Files in Go
Time to read: 5 minutes
how to zip files in Go
Managing file compression is a common task in software development, especially during situations where you're handling large datasets or you want to transfer multiple files.
In Go, there's a standard library to help do just that, providing you with the tools needed for zipping and unzipping files.
In this tutorial, we'll not only look at how to Zip files in Go, we'll add icing to the proverbial cake by using Dropbox to upload the compressed file, and Twilio to send the link to the designated user(s).
Prerequisites
Before we proceed, ensure that you have the following:
- Basic knowledge of Go and its concepts
- Go version 1.22 or higher.
- A Twilio account (either free or paid). If you are new to Twilio, click here to create a free account.
- A Twilio phone number
- A Dropbox account
How the application works
Here’s a brief description of what we'll do. We want to be able to zip one or more files and then upload them to Dropbox. After that, we'll generate a link and use Twilio to send that link to a designated user via SMS.
Set up Dropbox
First, you need to create a Dropbox app.


Login to the Dropbox App Console. In the Choose an API section, select Scoped access. For "Choose the type of access you need", select the one that best fulfills your needs; for this project we’ll be using Full Dropbox. After that, name your app, and click the Create App button.
Once that is done, you’ll be redirected to the Dropbox app's configuration page, where you need to add some permissions. Navigate to the Permissions tab, scroll down to files and folders, and enable files.content.write
and files.content.read
, then press Submit.


You next need to generate an access token. To do that, navigate to the Settings tab, scroll down to "Generated access token" and click Generate.


You should see a token-generated. Copy it to and store it somewhere safe for the time being.


Get your Twilio credentials
Log in to the Twilio Console. From the Account Info panel of the main dashboard, copy your Account SID, Auth Token, and phone number. Again, store them somewhere safe, for the time being.


Set up the project directory
The first thing to do is to create a new directory for your project and initialize a new Go module. Do that by running these commands:
Add the environment variables
Now, you're going to create environment variables for each of the credentials that you retrieved earlier, so that you don't store them directly in the application's source.
To do this, create a .env file in your project's main folder, and copy the following into it into it:
Then, replace each of the placeholders with the respective values. Finally, replace <<recipient_phone_number>>
with your phone number in E.164 format.
Install the required dependencies
You next need to install the following dependencies:
- github.com/dropbox/dropbox-sdk-go-unofficial/v6: This is Dropbox's official Go SDK. It will be used to communicate with Dropbox, where the files will be uploaded
- github.com/twilio/twilio-go: This is the official Twilio Helper Library for Go, which allows you to interact with Twilio services like SMS, voice, and other communication tools pretty easily. For this project, it will be used to send SMS notifications to the product owner whenever a new review is submitted.
- github.com/joho/godotenv: The GoDotEnv package will help us manage our environment variables
- github.com/gin-gonic/gin: The Gin library will be used in the entire project workflow for sending files and receiving responses
To install the dependencies, run the following commands:
Add the application logic
Create a main.go file,and add the code below to the file to import the required packages:
Then, you need to load the environment variables from .env, by adding the following after importing the packages:
Zip the files
The next step is to create a function to create zip files. The function will take a source folder and a target file name and compress the source folder into a ZIP archive with the provided name. Add the following code to the end of main.go:
In the code above, os.Create()
is used to create the output ZIP file, while zip.NewWriter()
initializes a ZIP writer to compress the data. Then, filepath.Walk()
is used to iterate through the directory and compress files into the ZIP archive.
For each file, its relative path is calculated with filepath.Rel()
to preserve the directory structure within the ZIP. Files are then opened, and their contents are copied into the ZIP archive using io.Copy()
.
Upload the zip archive to Dropbox
After zipping the directory, the next step is uploading them to Dropbox. You need to create a function that uploads the file and retrieves a shareable download link. After the ZipFiles()
function, add the following code to the end of main.go:
In the code above, the Dropbox SDK is initialized with the provided accessToken
using dropbox.Config()
. The file specified by filePath
is then opened and uploaded to Dropbox using client.Upload()
.
The upload is then configured with files.NewUploadArg()
, which sets the destination path in Dropbox and specifies the file write mode as "overwrite". After the file is successfully uploaded, a temporary link is generated using client.GetTemporaryLink()
with the file path passed to files.NewGetTemporaryLinkArg()
.
Send a notification SMS with Twilio
To share the Dropbox link, you need to use Twilio’s API to send an SMS, by creating a function that sends a message containing the download link. To do that, add the following code to the end of main.go:
Here, the Twilio client is initialized using accountSID
and authToken
. The message details i.e., to
, from
, and body,
are configured in CreateMessageParams
. The message is sent using client.Api.CreateMessage()
.
Create the main function
Now, you need to put all these functions together inside the main()
function to orchestrate the entire process. Add the following code at the end of main.go:
The code above initializes the Gin web server and defines the entire workflow for handling file uploads, processing them, and notifying users.
It compresses the uploaded files into a ZIP archive by calling the ZipFiles()
function. The ZIP file is then uploaded to Dropbox using the UploadFile()
function, which also generates a temporary download link. Afterward, the download link is sent to a predefined phone number via SMS using the SendSMS()
function.
It also serves as the HTML file used as the user interface. This is the user interface where files are uploaded.
Create the HTML page
For the frontend, we’ll make use of HTMX to handle requests asynchronously and display results dynamically. Create an index.html file in the root folder, and add the following code to the file:
Test the application
To test the application, you first need to start it:
Navigate to http://localhost:8080/ in your browser and test the application.


Once you upload a file, click the Process Files button. You should see something like this:


You‘ll receive a link to the zipped file on your phone via twilio:


That’s how to zip files in Go
In this tutorial, we examined how to zip files in Go. We spiced things up by uploading them to a third-party storage service like Dropbox, which creates a downloadable link. The link is then sent to a designated user using Twilio.
With Go’s simplicity and powerful third-party libraries like Twilio, you can build automation tools for various use cases.
Temitope Taiwo Oyedele is a software engineer and technical writer. He likes to write about things he’s learned and experienced.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.