> For the complete documentation index, see [llms.txt](https://cs61b-2.gitbook.io/cs61b-textbook-fall-2026/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cs61b-2.gitbook.io/cs61b-textbook-fall-2026/38.-compression-and-complexity/38.1-introduction-to-compression.md).

# 38.1 Introduction to Compression

As an introduction to compression, consider the processes of creating and unzipping a zip file.

<pre class="language-bash"><code class="lang-bash">$ zip mobydick.zip mobydick.txt 
adding: mobydick.txt (deflated 59%)

$ ls -l
<strong>-rw-rw-r-- 1 jug jug 643207 Apr 24 10:55 mobydick.txt
</strong>-rw-rw-r-- 1 jug jug 261375 Apr 24 10:55 mobydick.zip
</code></pre>

Note that before and after unzipping, the file size changes!

## Compression Model 1: Algorithms on Bits

In our first model of compression, we consider compression as applying a *compression algorithm* on a sequence of bits. To reverse the compression, we apply the inverse *decompression algorithm.*

<figure><img src="https://2512475561-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBGDh1mPw1Aw5u4IlOFCn%2Fuploads%2Fgit-blob-6487d81999ba95417decdd39158aeb8d1f6fab17%2Fimage%20(25).png?alt=media" alt=""><figcaption><p>Compression and decompression.</p></figcaption></figure>

Say you had a text file called `example.txt`. If you were to zip that text file, you'd get `example.zip`, a zip file with a size much lesser than the original `example.txt` file. This is the main idea behind compression--a technique used to reduce file size.

Then, if you were to unzip `example.zip` into a file called `unzippedexample.txt`, you would notice no difference between `example.txt` and `unzippedexample.txt.` This is an indicator of **lossless** compression, where no information is lost.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://cs61b-2.gitbook.io/cs61b-textbook-fall-2026/38.-compression-and-complexity/38.1-introduction-to-compression.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
