๐ฆ File Operations ยท 6 min read
How to Compress a CSV File
Compress a CSV with gzip and it typically shrinks to about a fifth of its size. In our test a 10 MB CSV dropped to 1.9 MB with gzip, and to 1.2 MB with bzip2. The trade-off is simple: gzip and zip are fast and work everywhere, xz and bzip2 squeeze out more but run slower, and a CSV is plain text, which is why it compresses so well in the first place.
Compression is the fastest way to get a file under an email or upload limit, and it is lossless, so nothing in the data changes. The catch is that the person or tool on the receiving end has to accept a .gz or .zip file, and some upload forms will only take the raw CSV.
Why a CSV Shrinks So Much
A CSV is uncompressed text. Every value is stored as characters, and real data repeats itself: the same country code in ten thousand rows, the same company name in a hundred, similar email addresses. Compression works by finding that repetition and describing it once instead of writing it out each time.
RFC 4180, which registers the text/csv media type, defines the format as plain characters with no built-in compression. That is the whole reason a CSV can be larger than an XLSX holding the same data: the XLSX is already a zip archive inside, while the CSV leaves all of its redundancy on the table for a compressor to pick up. We go into that comparison in the guide to why a CSV can be bigger than XLSX.
The Four Ways to Compress a CSV
| Format | Measured size (10 MB CSV) | Best for |
|---|---|---|
gzip | 1.9 MB (81% smaller) | the default; fast, universal, good ratio |
zip | 1.9 MB (81% smaller) | sending to someone on Windows who double-clicks files |
xz | 1.4 MB (86% smaller) | the best ratio of the fast-enough group |
bzip2 | 1.2 MB (88% smaller) | maximum size reduction when time does not matter |
gzip and zip use the same underlying algorithm, DEFLATE, which is why their sizes match. That algorithm is described in RFC 1951, and the gzip file wrapper is RFC 1952. xz and bzip2 use different, slower algorithms that find a little more redundancy, which is why they come out smaller.
How to Compress from the Command Line
On Linux and macOS the tools are already installed. The basic gzip command compresses in place and replaces the original:
gzip orders.csv # produces orders.csv.gz and deletes orders.csv gzip -k orders.csv # keeps the original, writes orders.csv.gz gzip -9 orders.csv # maximum effort; -1 is fastest, -9 is smallest
To get the file back, decompress with gunzip orders.csv.gz, which restores the exact original bytes. xz works the same way: xz -k orders.csv writes orders.csv.xz, and unxz reverses it.
On Windows, right-click the file and choose Send to, then Compressed (zipped) folder. This produces a .zip anyone can open, and the same DEFLATE algorithm is behind it, so the ratio is essentially the gzip number in the table above.
What We Measured
Rather than quote a marketing number, we generated a 100,000-row CSV with ten columns (id, names, emails, companies, cities, countries, amounts, dates and a notes field), then compressed the same bytes four ways. The environment was Python 3.13 with the standard library modules for gzip, bzip2, lzma and zipfile.
original CSV 10,067,751 bytes (10.07 MB) gzip -1 2,787,137 bytes 27.7% of original gzip -6 1,969,121 bytes 19.6% of original gzip -9 1,925,935 bytes 19.1% of original zip (deflate) 1,926,035 bytes 19.1% of original xz (lzma, preset 6) 1,414,596 bytes 14.1% of original bzip2 1,201,939 bytes 11.9% of original
Two things stand out. First, gzip -6 and gzip -9 are nearly identical here, a difference of about 2%, so the default level is almost always the right call. Second, the gap between gzip and bzip2 is real but small in absolute terms, about 0.7 MB on a 10 MB file, which you would only chase when the file has to fit under a hard byte limit.
Small files are the exception. A six-row, 84-byte CSV compressed to 78 bytes with gzip, because the format adds a small header of its own. Below a few hundred bytes, compression is not worth the effort.
When Compression Is the Wrong Tool
Compression only fixes a byte-size problem. If the limit you are running into is a row count, a cell count or a per-sheet ceiling, a smaller file will not help, you need to split the file instead. Splitting keeps the header row on every part and stays readable, which compression does not give you.
It also does nothing for files that are already compressed. Zipping an XLSX, a PNG or a PDF barely changes the size, because those formats already compress their internals. And if a tool, form or API rejects anything but a raw CSV, the answer is to shrink the data itself, fewer columns and fewer rows, rather than hand over a .gz it will refuse.
Frequently Asked Questions
How do I compress a CSV file?
From a terminal, run gzip orders.csv and you get orders.csv.gz. To keep the original add -k: gzip -k orders.csv. On Windows you can right-click the file and pick Send to > Compressed (zipped) folder. All of these are lossless, so the original CSV comes back byte for byte when you decompress.
Does compressing a CSV lose any data?
No. gzip, zip and xz are lossless formats, so decompressing restores the exact original file. There is no rounding, no dropped rows and no re-encoding, unlike a format conversion. Compression only removes redundancy in how the bytes are stored.
What is the best way to compress a CSV file?
gzip is the everyday choice: it is fast, works everywhere and in our test shrank a 10 MB CSV to 1.9 MB. xz and bzip2 squeeze out more (1.4 MB and 1.2 MB) but run slower. Use zip when the person on the other end is on Windows and expects a .zip file they can double-click.
Can I compress a CSV before uploading it somewhere?
Only if the tool you are uploading to accepts .gz or .zip files. Many upload forms and APIs want the raw CSV and will reject a compressed file, so check the accepted file types first. If the limit is the file size, compression helps; if the limit is the row count, splitting the file is the right fix instead.
How much smaller does a CSV get when compressed?
It depends on how repetitive the data is. In our test a 100,000-row, 10 MB CSV shrank to 1.9 MB with gzip (about 81% smaller), 1.4 MB with xz and 1.2 MB with bzip2. Text compresses well because names, emails and repeated values share a lot of structure.
Why does my CSV compress a lot but my XLSX file barely shrinks?
Because an .xlsx file is already compressed. Internally it is a zip archive of XML files, so zipping it again gains almost nothing. A CSV is uncompressed plain text, so it has the most redundancy to remove. That is the same reason a CSV can be larger than an XLSX that holds the same data.
Shrink the File, Not Just the Size
If the real problem is too many rows or too many columns, the splitter cuts the file into parts that keep the header, and the analyzer shows you which columns you can drop.
Related reading
File Operations โ other guides that pair well with this one.
- Split a Large CSV File Online
- Compare Two CSV Files Online
- CSV File Size Limit: How Big Can a CSV Get?
- CSV to Excel (XLSX): Convert Without Losing Leading Zeros or Dates
Browse all guides in the NoCodeCSV blog.