Last Updated: 10 September, 2026

How Compression Works Inside EPUB, DOCX, XLSX, & PPTX: The Hidden ZIP Architecture

How Compression Works Inside EPUB, DOCX, XLSX, and PPTX

If you rename a .docx, .xlsx, .pptx, or .epub file to .zip and double-click it, something surprising happens: it doesn’t throw an error. Your operating system opens it as a folder packed with subdirectories, XML configuration files, styling sheets, fonts, and embedded images.

Modern document architectures abandoned monolithic binary blobs decades ago. In their place, industry standards—namely Open Packaging Conventions (OPC) for Microsoft Office and the Open Container Format (OCF) for EPUB—adopted a surprisingly elegant foundation: the humble ZIP archive.

Understanding how compression operates inside these formats reveals why modern documents are so resilient, lightweight, and extensible—and why some files compress by 90% while others barely shrink at all.

1. The Container Architecture: Disguised ZIP Packages

Before understanding the compression algorithm itself, it helps to understand why modern document formats are structured as packages rather than standalone raw files.

The Problem with Legacy Binary Formats

Throughout the 1990s and early 2000s, Microsoft Office used proprietary binary formats (.doc, .xls, .ppt). These files were essentially memory dumps structured around the Compound File Binary Format (CFBF). They were notoriously fragile:

  • A single bit flip could corrupt the entire file structure.
  • Embedding images caused file sizes to balloon unpredictably.
  • Parsing required reverse-engineering dense binary specifications.
  • Cross-platform interoperability was a nightmare.

The Shift to Open, Modular Containers

In the mid-2000s, two parallel evolutions took place:

  1. Office Open XML (OOXML / ISO/IEC 29500): Microsoft introduced the XML-based formats ending with an x (DOCX, XLSX, PPTX). Under the hood, these files follow the Open Packaging Conventions (OPC).
  2. EPUB (IDPF / W3C): Digital publishing moved away from proprietary reader formats toward standard web tech (HTML, CSS, SVG) bundled inside the EPUB Open Container Format (OCF).

Both architectures rely on standard PKZIP 2.0 / ZIP specification. The file extension simply dictates the expected schema, the default viewer application, and mime-type declarations.

Sample DOCX File (Unzipped):
├── [Content_Types].xml        <-- Registry of MIME types for parts
├── _rels/                     <-- Package-level relationships
│   └── .rels
├── docProps/                  <-- Core and extended metadata
│   ├── app.xml
│   └── core.xml
└── word/                      <-- Main content payload
    ├── document.xml           <-- Text, paragraphs, and tags
    ├── styles.xml             <-- Typography and presets
    ├── numbering.xml          <-- Lists and counters
    ├── media/                 <-- Embedded images (PNG, JPG)
    └── _rels/
        └── document.xml.rels  <-- Internal hyperlinks & resource pointers

2. The Engine Under the Hood: The DEFLATE Algorithm

When a software application saves a DOCX or EPUB file, it doesn’t just store files into an uncompressed archive. It compresses the internal assets using DEFLATE (specified in RFC 1951).

DEFLATE is a two-tier lossless compression system combining two fundamental computer science algorithms:

Step 1: LZ77 (Lempel-Ziv 1977) — Sliding Window Redundancy Removal

XML and HTML are extremely verbose. Consider how often tags appear in a standard document.xml or chapter1.xhtml file:

  • <w:p><w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:t> repeats thousands of times in Word.
  • row r="1" spans="1:15"><c r="A1" t="s"><v> repeats in Excel across tens of thousands of cells.
  • <p class="calibre1"><span class="body-text"> repeats across EPUB book chapters.

LZ77 scans the data stream using a sliding dictionary window (typically 32 KB). When it encounters a string of characters it has seen recently, it replaces the duplicate text with a tiny backward pointer:

  • (distance, length) — e.g., “go back 142 bytes, copy 28 bytes”.

Instead of storing verbose markup over and over, LZ77 collapses thousands of repetitive XML tags into compact coordinate references.

Step 2: Huffman Coding — Variable-Length Frequency Encoding

After LZ77 replaces redundant sequences with length-distance tokens, Huffman coding analyzes the frequency of every symbol in the stream:

  • Frequently appearing symbols (like common characters e, t, spaces, or common distance markers) are assigned short binary bit codes (e.g., 2 to 4 bits).
  • Rarely used symbols receive longer binary bit codes (e.g., 12 to 16 bits).

The result is a stream of variable-length bit codes that squeeze plain-text XML down by 75% to 88%.

3. Format-by-Format Breakdown: How Each Handles Compression

While DOCX, XLSX, PPTX, and EPUB all use the same ZIP envelope, their internal data characteristics differ wildly.

A. DOCX: The Text and Styling Balancing Act

  • What’s inside: Plain XML (word/document.xml), font tables, styles, relationship catalogs, and a word/media/ folder.
  • How compression performs:
    • The raw text and XML markup experience massive compression ratios (often dropping from 5 MB of raw XML down to 500 KB).
    • However, modern documents often embed screenshots, illustrations, and photos. Because JPEG and PNG files are already compressed, DEFLATE cannot shrink them further. In fact, running DEFLATE over an already-compressed image yields virtually 0% savings (and can even slightly increase size due to compression headers).
    • Consequently, DOCX files without images are exceptionally tiny, whereas image-heavy reports reflect almost the exact size of their contained image files.

B. XLSX: High-Volume Numeric Data & Shared Strings

Spreadsheets present a unique challenge: a sheet can contain hundreds of thousands of rows, leading to astronomical XML file sizes if not handled smartly.

  • The Shared Strings Strategy (xl/sharedStrings.xml):
    • If a text label like “United States” or “In Progress” appears 50,000 times in a spreadsheet, storing <c t="inlineStr"><is><t>United States</t></is></c> in 50,000 cells would bloat the uncompressed XML to gigabytes.
    • Excel deduplicates text before compression by storing every unique string once in a shared string table and referencing it by numerical index (e.g., <v>0</v>, <v>1</v>).
  • Why XLSX Compresses Dramatically:
    • Numeric row records (sheet1.xml) follow repetitive, predictable syntax.
    • DEFLATE easily detects repetitive patterns in tabular XML. It is common for a 120 MB raw sheet1.xml file to shrink to less than 6 MB inside an XLSX archive.

C. PPTX: Media-Heavy Slide Decks

Presentations are fundamentally different from documents and spreadsheets:

  • The Image Dilemma: PPTX files are typically dominated by vector shapes, background graphics, video clips, and high-resolution slides.
  • Compression Profile: While ppt/slides/slide1.xml through slideN.xml compress efficiently, the media payload (ppt/media/) accounts for 85% to 95% of the total archive weight.
  • Why Re-Zipping a PPTX Changes Nothing: If you try to compress an already-saved PPTX file with 7-Zip or WinRAR, you will notice almost no size reduction. Because the interior is already a DEFLATE-compressed ZIP archive containing pre-compressed JPEGs and MP4s, the entropy is already near maximum.

D. EPUB: Web Technologies with a Mandatory Uncompressed Header

An EPUB file is essentially a responsive, packaged micro-website containing XHTML chapters, CSS stylesheets, TTF/WOFF fonts, and metadata. However, EPUB has one strict packaging rule that differentiates it from Microsoft Office files:

EPUB Internal Structure:
├── mimetype                    <-- MUST be uncompressed (Stored) & at byte offset 38
├── META-INF/
│   └── container.xml           <-- Tells reader where the OPF manifest lives
└── OEBPS/ (or EPUB/)
    ├── content.opf             <-- Manifest of all book assets
    ├── toc.ncx / nav.xhtml     <-- Table of contents navigation
    ├── styles/style.css        <-- CSS formatting
    ├── images/                 <-- Book cover & illustrations
    └── text/                   <-- chapter1.xhtml, chapter2.xhtml
  • The Magic mimetype File:
    • E-readers need to identify an EPUB immediately without extracting the entire archive or running decompression pipelines.
    • The Open Container Format (OCF) mandates that the mimetype file:
      1. Must be the very first file in the ZIP archive.
      2. Must contain exactly the string application/epub+zip.
      3. Must not be compressed (ZIP compression method 0 / “Stored”).
      4. Must have no extra field data, ensuring the MIME string always begins at byte 38 of the physical file.
  • Text Compression: All remaining files (.xhtml, .css, .opf) are compressed using standard DEFLATE (ZIP method 8), allowing full-length novels to shrink down to a few hundred kilobytes.

4. Comparing Compression Across Formats

FormatCore PayloadPrimary Redundancy SourceTypical Compression Ratio (Text/Markup)Media Handling
DOCXWordprocessingML (document.xml)Repetitive XML paragraph/run tags75% – 85%Stored in word/media/ (mostly pre-compressed)
XLSXSpreadsheetML (sheet*.xml)Repetitive cell/row tags; Shared Strings80% – 92%Sparse; images/charts in xl/media/
PPTXPresentationML (slide*.xml)Slide layout metadata, shape coordinates70% – 80%Heavy ppt/media/ payload limits overall savings
EPUBXHTML, CSS, OPF, NCXHTML tags, repetitive CSS selectors65% – 80%mimetype uncompressed; media in subfolders

5. Practical Takeaways: How to Optimize Your Documents

Because you now know how internal packaging works, you can leverage compression mechanics to solve real-world problems:

  1. Fixing Corrupted Documents: If a document refuses to open, changing the extension to .zip allows you to extract the contents and recover the raw text from document.xml or individual chapters from the EPUB’s text/ directory.
  2. Shrinking Giant Office Files: Since XML compression is already optimized, giant files are almost always caused by unoptimized images in media/. Rather than using third-party PDF or DOCX compressors, open the ZIP container, extract the images, run them through an image optimizer (like WebP, TinyPNG, or MozJPEG), and replace them inside the archive.
  3. Automating Document Generation: Developers don’t need heavyweight office suites to build reports. You can generate raw XML templates, bundle them using standard zlib/ZIP libraries, and output valid DOCX or XLSX files programmatically in milliseconds.

6. Frequently Asked Questions (FAQ)

Can I convert a DOCX or EPUB to a ZIP file just by renaming the file extension? Yes; renaming the extension to .zip allows any standard archive tool (like 7-Zip, macOS Archive Utility, or Windows Explorer) to open and inspect the internal files directly.

Why doesn’t compressing a DOCX or PPTX with 7-Zip make it noticeably smaller? Because the file is already an internally compressed ZIP archive containing DEFLATE-encoded XML and pre-compressed images, leaving minimal redundancy for an external tool to remove.

Why does the EPUB specification require the mimetype file to be uncompressed? It allows e-reader software to verify that the file is an authentic EPUB by checking the MIME string at a fixed byte offset without having to initialize a decompression engine.

Does changing cell formatting in Excel increase the compressed XLSX file size? Yes; extensive custom formatting breaks uniform pattern repetition across cells, creating longer XML definitions that reduce DEFLATE’s compression efficiency.

Is it possible to extract high-resolution original images from a Word or PowerPoint file without quality loss? Yes; rename the file to .zip, open the word/media or ppt/media folder, and you will find the original, uncompressed source images exactly as they were inserted.

See Also