<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ compression - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ compression - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Fri, 05 Jun 2026 20:27:02 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/compression/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Compress PDF Files in the Browser Using JavaScript (Step-by-Step) ]]>
                </title>
                <description>
                    <![CDATA[ PDF files are everywhere. From invoices and reports to résumés and documents, they’re one of the most common file formats we deal with. But there’s a common problem: PDFs can get large quickly. If you ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-compress-pdf-files-in-the-browser-using-javascript/</link>
                <guid isPermaLink="false">69f8b15246610fd606f2d8da</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ pdf ]]>
                    </category>
                
                    <category>
                        <![CDATA[ compression ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Bhavin Sheth ]]>
                </dc:creator>
                <pubDate>Mon, 04 May 2026 14:46:42 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/c46817cf-6587-42c5-b7c1-53b074e77d0a.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>PDF files are everywhere. From invoices and reports to résumés and documents, they’re one of the most common file formats we deal with. But there’s a common problem: PDFs can get large quickly.</p>
<p>If you’ve ever tried to upload a PDF and hit a file size limit, you’ve already seen why compression matters.</p>
<p>Most tools solve this by uploading your file to a server. That works, but it’s not always ideal, especially when dealing with private or sensitive documents.</p>
<p>The good news is that modern browsers are powerful enough to handle basic PDF compression locally.</p>
<p>In this tutorial, you’ll learn how to build a <strong>browser-based PDF compression tool using JavaScript</strong>, where everything runs directly in the browser.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/8f448f4a-980d-4792-8c2f-60f6a64f00d1.png" alt="browser-based PDF compression tool allinonetool" style="display:block;margin:0 auto" width="768" height="162" loading="lazy">

<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p><a href="#heading-how-pdf-compression-works">How PDF Compression Works</a></p>
</li>
<li><p><a href="#heading-project-setup">Project Setup</a></p>
</li>
<li><p><a href="#heading-what-library-are-we-using">What Library Are We Using?</a></p>
</li>
<li><p><a href="#heading-creating-the-upload-interface">Creating the Upload Interface</a></p>
</li>
<li><p><a href="#heading-reading-the-pdf-file">Reading the PDF File</a></p>
</li>
<li><p><a href="#heading-understanding-compression-strategy">Understanding Compression Strategy</a></p>
</li>
<li><p><a href="#heading-compressing-the-pdf">Compressing the PDF</a></p>
</li>
<li><p><a href="#heading-generating-and-downloading-the-file">Generating and Downloading the File</a></p>
</li>
<li><p><a href="#heading-demo-how-the-pdf-compression-tool-works">Demo: How the PDF Compression Tool Works</a></p>
</li>
<li><p><a href="#heading-important-notes-from-real-world-use">Important Notes from Real-World Use</a></p>
</li>
<li><p><a href="#heading-common-mistakes-to-avoid">Common Mistakes to Avoid</a></p>
</li>
<li><p><a href="#heading-conclusion">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-how-pdf-compression-works">How PDF Compression Works</h2>
<p>PDF compression is different from image compression.</p>
<p>A PDF isn't just a single image. It’s a structured document that can include text, images, fonts, and metadata. Because of this, reducing its size involves optimizing multiple parts of the file rather than applying a single compression method.</p>
<p>In most cases, compressing a PDF means lowering image quality where possible, removing unnecessary or unused data, and optimizing how the document is internally structured.</p>
<p>When working in the browser, we don’t have the same level of control as server-side tools. But we can still reduce file size by reprocessing the document and saving it in a more efficient format.</p>
<p>This approach may not achieve extreme compression, but it works well for creating lighter, more efficient files while keeping everything fast and private.</p>
<h2 id="heading-project-setup">Project Setup</h2>
<p>This project is simple.</p>
<p>You only need:</p>
<ul>
<li><p>an HTML file</p>
</li>
<li><p>JavaScript</p>
</li>
<li><p>a PDF library</p>
</li>
</ul>
<p>No backend is required. Everything runs locally in the browser.</p>
<h2 id="heading-what-library-are-we-using">What Library Are We Using?</h2>
<p>We’ll use <strong>pdf-lib</strong>, which allows us to load and recreate PDF files.</p>
<p>Add it using a CDN:</p>
<pre><code class="language-html">&lt;script src="https://unpkg.com/pdf-lib/dist/pdf-lib.min.js"&gt;&lt;/script&gt;
</code></pre>
<h2 id="heading-creating-the-upload-interface">Creating the Upload Interface</h2>
<p>Start with a simple interface:</p>
<pre><code class="language-html">&lt;input type="file" id="upload" accept="application/pdf"&gt;
&lt;button onclick="compressPDF()"&gt;Compress PDF&lt;/button&gt;

&lt;a id="download" style="display:none;"&gt;Download Compressed PDF&lt;/a&gt;
</code></pre>
<p>This allows users to upload a PDF, trigger compression, and download the result once ready.</p>
<h2 id="heading-reading-the-pdf-file">Reading the PDF File</h2>
<p>Now read the uploaded file:</p>
<pre><code class="language-javascript">const fileInput = document.getElementById("upload");

if (!fileInput.files.length) {
  alert("Please upload a PDF");
  return;
}

const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
</code></pre>
<h2 id="heading-understanding-compression-strategy">Understanding Compression Strategy</h2>
<p>Since we’re working in the browser, we don’t have full low-level control over PDF compression.</p>
<p>Instead, we focus on practical optimizations that help reduce file size without affecting usability too much. This includes recreating the document structure in a more efficient way, removing unnecessary metadata, and reducing image quality where possible.</p>
<p>The goal here isn’t perfect compression, but producing a lighter file while maintaining acceptable visual quality and readability.</p>
<h2 id="heading-compressing-the-pdf">Compressing the PDF</h2>
<p>Here’s the core logic:</p>
<pre><code class="language-javascript">async function compressPDF() {
  const fileInput = document.getElementById("upload");

  if (!fileInput.files.length) {
    alert("Please upload a PDF");
    return;
  }

  const file = fileInput.files[0];
  const arrayBuffer = await file.arrayBuffer();

  const { PDFDocument } = PDFLib;

  const originalPdf = await PDFDocument.load(arrayBuffer);
  const newPdf = await PDFDocument.create();

  const pages = await newPdf.copyPages(
    originalPdf,
    originalPdf.getPageIndices()
  );

  pages.forEach(page =&gt; newPdf.addPage(page));

  const pdfBytes = await newPdf.save({
    useObjectStreams: true
  });

  const blob = new Blob([pdfBytes], { type: "application/pdf" });

  const link = document.getElementById("download");
  link.href = URL.createObjectURL(blob);
  link.download = "compressed.pdf";
  link.style.display = "inline";
  link.innerText = "Download Compressed PDF";
}
</code></pre>
<p>This recreates the PDF using optimized object streams, which can reduce file size.</p>
<h2 id="heading-generating-and-downloading-the-file">Generating and Downloading the File</h2>
<p>Once processed:</p>
<pre><code class="language-javascript">link.href = URL.createObjectURL(blob);
link.download = "compressed.pdf";
</code></pre>
<p>The file is downloaded instantly, without any server interaction.</p>
<h2 id="heading-demo-how-the-pdf-compression-tool-works">Demo: How the PDF Compression Tool Works</h2>
<p>Here’s how the full flow looks in a real-world scenario using the browser-based PDF compression tool.</p>
<h3 id="heading-step-1-upload-pdf">Step 1: Upload PDF</h3>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/7ae27903-73d3-4897-9214-bcb061ab256a.png" alt="PDF compression tool interface showing drag and drop upload area with select file button" style="display:block;margin:0 auto" width="1409" height="662" loading="lazy">

<p>Start by uploading your PDF file. You can either drag and drop the file into the upload area or click the “Select PDF” button to choose a file from your device.</p>
<h3 id="heading-step-2-preview-the-pdf">Step 2: Preview the PDF</h3>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/8b713246-98cf-4533-8d39-a5dbd89ac181.png" alt="PDF file preview interface with page navigation controls in browser-based compression tool" style="display:block;margin:0 auto" width="796" height="679" loading="lazy">

<p>Once the file is loaded, the tool displays a preview of the document. You can navigate between pages to confirm that the correct file has been uploaded before applying compression.</p>
<h3 id="heading-step-3-choose-compression-settings">Step 3: Choose Compression Settings</h3>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/e8612b77-502d-4b53-b7d1-835061fc8e37.png" alt="PDF compression settings showing levels like basic, recommended, high and advanced options" style="display:block;margin:0 auto" width="391" height="681" loading="lazy">

<p>Next, select the compression level based on your needs. Lower compression keeps better quality, while higher compression reduces file size more aggressively. You can also explore advanced options like metadata handling.</p>
<h3 id="heading-step-4-compress-the-pdf">Step 4: Compress the PDF</h3>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/9450ece1-b064-4184-a267-f6e58b9cddf9.png" alt="Compress PDF button with start over option in browser-based PDF compression tool" style="display:block;margin:0 auto" width="424" height="97" loading="lazy">

<p>Click the “Compress PDF” button to start the process. The tool processes everything directly in your browser, without uploading files to any server.</p>
<h3 id="heading-step-5-download-the-compressed-file">Step 5: Download the Compressed File</h3>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/a815b5c7-be0b-493d-b1be-6dc7d29b955e.png" alt="PDF compression result showing reduced file size and download button for optimized file" style="display:block;margin:0 auto" width="1085" height="802" loading="lazy">

<p>After compression is complete, you’ll see the final result along with the reduced file size. You can then rename and download the optimized PDF instantly.</p>
<h2 id="heading-important-notes-from-real-world-use">Important Notes from Real-World Use</h2>
<p>When working with PDF compression in the browser, handling large files becomes important.</p>
<p>If a user uploads a very large PDF, processing everything at once can slow down the browser or even cause it to freeze. Instead of trying to process everything blindly, it’s better to add checks and handle files carefully.</p>
<p>For example, you can limit the file size before processing:</p>
<pre><code class="language-javascript">const MAX_SIZE = 10 * 1024 * 1024; // 10MB

if (file.size &gt; MAX_SIZE) {
  alert("File is too large. Please upload a file under 10MB.");
  return;
}
</code></pre>
<p>This prevents performance issues and keeps the tool responsive.</p>
<p>Another useful approach is to process files step by step instead of doing everything at once:</p>
<pre><code class="language-javascript">const { PDFDocument } = PDFLib;

const originalPdf = await PDFDocument.load(arrayBuffer);
const newPdf = await PDFDocument.create();

for (let i = 0; i &lt; originalPdf.getPageCount(); i++) {
  const [page] = await newPdf.copyPages(originalPdf, [i]);
  newPdf.addPage(page);
}
</code></pre>
<p>This spreads the work across smaller steps and avoids blocking the browser.</p>
<p>It’s also important to remember that everything runs client-side. This means files never leave the user’s device, which is great for privacy. But it also means performance depends on the user’s device, so keeping processing efficient is important.</p>
<h2 id="heading-common-mistakes-to-avoid">Common Mistakes to Avoid</h2>
<p>One common mistake is not validating user input properly before processing the file.</p>
<p>For example, users might try to upload an empty file, a non-PDF file, or even trigger the compression without selecting anything. It’s important to check these cases early to avoid errors later in the process:</p>
<pre><code class="language-javascript">const fileInput = document.getElementById("upload");

if (!fileInput.files.length) {
  alert("Please upload a PDF file.");
  return;
}

const file = fileInput.files[0];

if (file.type !== "application/pdf") {
  alert("Only PDF files are supported.");
  return;
}
</code></pre>
<p>Another issue is allowing invalid or unexpected input to pass through. Even something as simple as an empty or corrupted file can cause the PDF processing to fail, so basic validation makes the tool much more reliable.</p>
<p>Handling large files without any checks is another common problem. If a very large PDF is processed without limits, it can slow down the browser or even make the page unresponsive. Adding a simple file size check helps prevent this:</p>
<pre><code class="language-javascript">const MAX_SIZE = 10 * 1024 * 1024; // 10MB

if (file.size &gt; MAX_SIZE) {
  alert("File is too large. Please upload a file under 10MB.");
  return;
}
</code></pre>
<p>Another mistake is assuming that compression will always produce a significantly smaller file. In reality, browser-based compression is limited compared to dedicated server-side tools, so results can vary depending on the content of the PDF.</p>
<p>In practice, most issues come from missing validation and handling edge cases. Adding a few simple checks early makes the tool more stable and improves the overall user experience.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, you built a browser-based PDF compression tool using JavaScript.</p>
<p>You learned how to read and recreate PDF files, apply basic optimizations, and generate a downloadable file entirely in the browser.</p>
<p>If you’d like to try a complete version of this idea, you can check it out here: <a href="https://allinonetools.net/pdf-compressor/">https://allinonetools.net/pdf-compressor/</a></p>
<p>This approach keeps everything fast, private, and simple to use.</p>
<p>Once you understand this pattern, you can extend it further to build more advanced document tools.</p>
<p>And that’s where things start getting really interesting.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Compress Files to ".gz" on the Windows Operating System ]]>
                </title>
                <description>
                    <![CDATA[ You may often need to compress files and folders for various reasons. And "Gzip" compression is a good choice for many scenarios.  Recently, I have faced an issue where I needed to compress a lot of files individually, and manual intervention to comp... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-convert-files-to-gzip-on-windows/</link>
                <guid isPermaLink="false">66b902c3558588891017dd82</guid>
                
                    <category>
                        <![CDATA[ compression ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Windows ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Md. Fahim Bin Amin ]]>
                </dc:creator>
                <pubDate>Fri, 01 Mar 2024 19:15:53 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/02/freeCodeCamp---Fahim.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>You may often need to compress files and folders for various reasons. And "Gzip" compression is a good choice for many scenarios. </p>
<p>Recently, I have faced an issue where I needed to compress a lot of files individually, and manual intervention to compress each file one by one using traditional 7zip became a hassle.</p>
<p>If you are in love with the Windows operating system like I am (I know, I know, sometimes Windows can become quite a pain. Maybe I like the pain and also like to resolve issues all by myself, who knows!), then you may also face issues in batch processing compressing multiple files to the <code>.gzip</code> format.</p>
<p>There are multiple ways to compress a file into the <code>.gzip</code> format. The main issue is that most of the ways do not support batch processing the conversion. In this article, I will talk about two of the decent ways you can do this.</p>
<h2 id="heading-method-1-using-7zip-no-batch-processing">Method 1: Using 7zip (No Batch Processing)</h2>
<p><a target="_blank" href="https://www.7-zip.org/">7zip</a> is a free software available for Windows, Linux, and ARM64. Installing 7zip in the Windows operating system is very simple and straightforward.</p>
<p>If you simply want to compress any single file to <code>.gzip</code> format, you need to simply select that file and add it to the 7zip archive. In the GUI, you can select the Archive format as "gzip" and that's it!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/2024-02-21_12-36.png" alt="Image" width="600" height="400" loading="lazy">
<em>gzip in 7zip</em></p>
<h2 id="heading-method-2-using-gzip-from-chocolatey-batch-processing-is-the-main-benefit-of-this">Method 2: Using <code>gzip</code> from Chocolatey (Batch Processing is the Main Benefit of This)</h2>
<p>There are several tools that we can use to compress files and folders on our computers. However, Linux-based operating systems come with a lot of tools and there are a lot of CLI (Command Line Interface) type tools that we can also use to compress multiple files altogether in a batch. </p>
<p>If you use Linux based operating system, then you might have also used GZip. Gzip is a file format and software application that compresses and decompresses files. It also makes files smaller and allows for faster network transfers. However, there are not any official installers of GZip for the Windows operating system.</p>
<p>But, we can install "gzip" directly on Windows and work like we're in a Linux OS. I prefer to download GZip via <a target="_blank" href="https://chocolatey.org/">Chocolatey</a>, a very good package manager for the Windows operating system. </p>
<blockquote>
<p>Chocolatey is a machine-level, command-line package manager and installer for software on Microsoft Windows. It uses the NuGet packaging infrastructure and Windows PowerShell to simplify the process of downloading and installing software.</p>
</blockquote>
<p>If you are using Chocolatey for the first time, then you need to install it first. All of the methods are explained in detail on their official website: <a target="_blank" href="https://chocolatey.org/install">https://chocolatey.org/install</a>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/2024-02-21_12-48.png" alt="Image" width="600" height="400" loading="lazy">
<em>Chocolatey Installation</em></p>
<p>Open your Terminal and run the command for installing "gzip".</p>
<pre><code class="lang-powershell">choco install gzip
</code></pre>
<p>Write "Yes" when it asks for your permission. After a few seconds, you should be ready to use it.</p>
<p>Let's say I want to batch-compress a lot of files into ".gzip". I can open my terminal and go to that directory (where my raw files are that I want to compress using gzip) using the <code>cd path/to/where/I/have/the/files</code> command. </p>
<p>Or, I can simply open my terminal directly using the "Open In Terminal" context menu in that specific directory where I have the files that I want to compress using gzip. Then I can simply use the following command.</p>
<pre><code class="lang-powershell">gzip * <span class="hljs-literal">-r</span>
</code></pre>
<p>This will iterate through every folder and subfolder in that specific location and compress all of the files to <code>.gzip</code> recursively (the <code>-r</code> flag). Keep in mind that it will <strong>replace</strong> all your files to <code>.gzip</code> in that directory.</p>
<p><strong>But</strong>, if you also want to keep the original files side by side during the batch compression process, you can use the command below.</p>
<pre><code class="lang-bash">gzip * -r -k
</code></pre>
<p>Here, the <code>-k</code> flag indicates the <code>--keep</code> option to keep the original files.</p>
<p><strong>If</strong> you want to use all your CPU cores in parallel, then follow the command below.</p>
<pre><code class="lang-bash">parallel gzip ::: *
</code></pre>
<p>You have to add the necessary suffixes to suit your needs obviously in this process.</p>
<p>That's it!</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>I hope you have gained some valuable insights from this article.</p>
<p>If you have enjoyed the procedures step-by-step, then don't forget to let me know on <a target="_blank" href="https://twitter.com/Fahim_FBA">Twitter/X</a> or <a target="_blank" href="https://www.linkedin.com/in/fahimfba/">LinkedIn</a>. I would appreciate it if you could endorse me for some relevant skillsets of mine on <a target="_blank" href="https://www.linkedin.com/in/fahimfba/">LinkedIn</a>.</p>
<p>You can follow me on <a target="_blank" href="https://github.com/FahimFBA">GitHub</a> as well if you are interested in open source. Make sure to check <a target="_blank" href="https://fahimbinamin.com/">my website</a> (<a target="_blank" href="https://fahimbinamin.com/">https://fahimbinamin.com/</a>) as well.</p>
<p>Thank you so much!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Minify CSS – CSS Minifying and Compression Explained ]]>
                </title>
                <description>
                    <![CDATA[ By Dillion Megida Minification is the process of reducing code size to reduce the size of your files. So how does this apply to CSS? Take a look at this code: h1 {   color: yellow; } p {   color: pink; } And the compressed version of ]]>
                </description>
                <link>https://www.freecodecamp.org/news/minify-css-css-minifying-and-compression-explained/</link>
                <guid isPermaLink="false">66d84f43d592ae8435de7172</guid>
                
                    <category>
                        <![CDATA[ compression ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ optimization ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 18 May 2022 15:20:23 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/05/pexels-magda-ehlers-2590535.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Dillion Megida</p>
<p>Minification is the process of reducing code size to reduce the size of your files. So how does this apply to CSS?</p>
<p>Take a look at this code:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">h1</span> {
  <span class="hljs-attribute">color</span>: yellow;
}

<span class="hljs-selector-tag">p</span> {
  <span class="hljs-attribute">color</span>: pink;
}
</code></pre>
<p>And the compressed version of it:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">h1</span> { <span class="hljs-attribute">color</span>: yellow; } <span class="hljs-selector-tag">p</span> { <span class="hljs-attribute">color</span>: pink; }
</code></pre>
<p>These two code blocks are the same thing, and they work the same way because they have the correct CSS syntax. But there are two differences between these code blocks:</p>
<ul>
<li>the first one is more readable and understandable compared to the second one with a one-liner</li>
<li>the first one results in bigger file size compared to the second one.</li>
</ul>
<p>On testing this on my computer, the first one has a size of <strong>46 bytes</strong> while the second is <strong>40 bytes</strong>. This difference may seem insignificant, but it becomes noticable when you consider the difference that a compressed version of a bigger codebase can make.</p>
<h2 id="heading-why-does-the-compressed-size-matter">Why does the compressed size matter?</h2>
<p>When a browser gets an HTML document from a server, it fetches the resources linked in the document. These resources include images, scripts, and also stylesheets.</p>
<p>The larger a CSS file is, the more resources (such as network bandwidth) it takes to download. Also, the longer it takes to download such files. This results in slower page load times and affects the overall user experience.</p>
<p>These expenses can be ignored for small CSS files, but as a program grows, compression becomes an essential factor in improving page load times.</p>
<h2 id="heading-all-the-browser-needs-is-valid-css-not-readable-or-formatted-css">All the browser needs is valid CSS, not readable or formatted CSS</h2>
<p>Compressed file sizes do not affect how the browser parses CSS. The browser does not need readable CSS to be able to interpret it on a web page. It only needs valid CSS (CSS code with a correct syntax – curly braces, semi-colons, and so on). </p>
<p>Therefore, the extra spaces, comments, and indentations do not matter to the browser. It only matters for development. </p>
<p>For deployed applications, you need a <strong>distributable</strong> version of CSS.</p>
<p>The <strong>distributable</strong> version isn't meant to be read by humans or used during development – but rather it's used for deployed applications as they only matter to the browser.</p>
<h2 id="heading-how-does-minified-css-work">How does minified CSS work?</h2>
<p>The goal of minification is to remove the parts of your CSS code that are irrelevant to the browser to interpret the CSS.</p>
<p>For example, this code:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">h1</span> {
  <span class="hljs-comment">/* a header */</span>
  <span class="hljs-attribute">color</span>: yellow;
}
</code></pre>
<p>Here's a graphical presentation of the parts of the code that matter and those that do not:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/05/image-85.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Code comments can help developers work together, remember why decisions are made, and understand the purpose of different parts of code. But the browser does not need that information.</p>
<p>Spaces and indentation improve the readability of code by humans, but the browser can read code without spaces.</p>
<p>The element selector, curly braces, and semi-colon are essential parts of the code as they follow the CSS syntax and help the browser interpret the code correctly.</p>
<p>CSS minification methods take these parts that the browser does not need from the code, which result in a lower-sized file, and makes it faster for the browser to download such files from the server.</p>
<p>The minified version of the above code is:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">h1</span>{<span class="hljs-attribute">color</span>:yellow;}
</code></pre>
<p>And everything still works perfectly on the browser.</p>
<h2 id="heading-how-to-minify-css">How to minify CSS</h2>
<p>Now you understand the relevance of compressed CSS files and how they work. So how do you minify your CSS files?</p>
<p>Of course, you cannot write minified CSS code during development because it makes code collaboration, reading, and understanding difficult. </p>
<p>Here are some tools you can use to minify your CSS.</p>
<h3 id="heading-clean-css">clean-css</h3>
<p><a target="_blank" href="https://www.npmjs.com/package/clean-css">clean-css</a> is an NPM library you can use to minify your CSS files either locally or from a remote server.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/05/image-88.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-dans-tools-css-minifier">Dan's Tools CSS Minifier</h3>
<p><a target="_blank" href="https://www.cleancss.com/css-minify/">Dan's Tools Minifier</a> is an online CSS minification tool for minifying CSS. You can paste the CSS in an input field, enter a URL where the CSS file lives, or paste the CSS file.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/05/image-86.png" alt="Image" width="600" height="400" loading="lazy">
<em>Screenshot from the Dan's Tools CSS Minifier page</em></p>
<h3 id="heading-toptal-css-minifier">Toptal CSS Minifier</h3>
<p><a target="_blank" href="https://www.toptal.com/developers/cssminifier/">Toptal CSS Minifier</a> provides a UI to add your CSS, and see the minified output. It also provides an API and plugins that makes the process automated.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/05/image-87.png" alt="Image" width="600" height="400" loading="lazy">
<em>Toptal CSS Minifier screenshot</em></p>
<p>There are more tools and configurations you can apply to make the process easier – these are just a few.</p>
<h2 id="heading-wrap-up">Wrap up</h2>
<p>Minification, generally, is a great approach for optimizing websites. Minifying CSS files increases page load times and requires fewer resources by the browser to download.</p>
<p>During development, comments, indentation, and other forms of formatting improve code readability and collaboration. But the browser does not need that. </p>
<p>CSS minification is the process of compressing CSS file sizes by taking out the irrelevant part of the files that the browser does not need to interpret on a webpage.</p>
<p>Fortunately, some tools make it easier, so you can enjoy the process of development and also get a distributed version at the end.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Linux tar Command – How to Compress Files in Linux ]]>
                </title>
                <description>
                    <![CDATA[ File compression is an essential utility across all platforms. It helps you reduce file size and share files efficiently. And compressed files are also easier to copy to remote servers.  You can also compress older and rarely used files and save them... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-compress-files-in-linux-with-tar-command/</link>
                <guid isPermaLink="false">66adea3ec537123a64ede800</guid>
                
                    <category>
                        <![CDATA[ compression ]]>
                    </category>
                
                    <category>
                        <![CDATA[ data compression ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Linux ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Zaira Hira ]]>
                </dc:creator>
                <pubDate>Wed, 06 Oct 2021 16:19:13 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/09/Linux-File-Tar-Compression.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>File compression is an essential utility across all platforms. It helps you reduce file size and share files efficiently. And compressed files are also easier to copy to remote servers. </p>
<p>You can also compress older and rarely used files and save them for future use which helps you conserve disk space.</p>
<p>In this post, we'll look at how to compress files with the <code>tar</code> command in Linux, along with some examples of <code>tar</code> in action.</p>
<h2 id="heading-what-is-the-tar-command">What is the tar command?</h2>
<p>We use the <code>tar</code> command to compress and expand files from the command line. The syntax is shown below:</p>
<pre><code class="lang-bash">tar [flags] destinationFileName sourceFileName
</code></pre>
<p> The <code>tar</code> command uses the following flags to customize the command input:</p>
<table>
<thead>
<tr>
<th>Flag</th>
<th>Explanation</th>
<th>Usage</th>
</tr>
</thead>
<tbody>
<tr>
<td>-c</td>
<td>Create a new archive.</td>
<td>We use this flag whenever we need to create a new archive.</td>
</tr>
<tr>
<td>-z</td>
<td>Use gzip compression.</td>
<td>When we specify this flag, it means that archive will be created using gzip compression.</td>
</tr>
<tr>
<td>-v</td>
<td>Provide verbose output.</td>
<td>Providing the -v flag shows details of the files compressed.</td>
</tr>
<tr>
<td>-f</td>
<td>Archive file name.</td>
<td>Archive file names are mapped using the -f flag.</td>
</tr>
<tr>
<td>-x</td>
<td>Extract from a compressed file.</td>
<td>We use this flag when files need to be extracted from an archive.</td>
</tr>
</tbody>
</table>

<h3 id="heading-how-to-create-an-archive">How to create an archive</h3>
<p>We have a list of the following files which we'll compress with <code>tar</code>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/10/image-1.png" alt="Image" width="600" height="400" loading="lazy">
<em>List of files to be compressed.</em></p>
<p>To compress them, we'll use <code>tar</code> like this:</p>
<pre><code class="lang-bash">tar -czvf logs_archive.tar.gz *
</code></pre>
<p>Let's break down this command and look into each flag.</p>
<p><code>-c</code> is creating and archive.</p>
<p><code>-z</code> is using gzip compression.</p>
<p><code>-v</code> is providing details of the files that have been archived.</p>
<p><code>-f</code> is creating an archive with the name 'logs_archive.tar.gz' as supplied in the command above.</p>
<p>In the results below, we can see that the archive has been created successfully.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/10/image-13.png" alt="Image" width="600" height="400" loading="lazy">
<em>Archive has been created with supplied command.</em></p>
<h3 id="heading-how-to-remove-files-after-compression">How to remove files after compression</h3>
<p>Let's say we don't want to keep the original files after creating an archive. For that, we can use the <code>--remove-files</code> flag.</p>
<pre><code class="lang-bash">tar -czvf logs_archive.tar.gz * --remove-files
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/10/image-3.png" alt="Image" width="600" height="400" loading="lazy">
<em>Files removed once archive has been created</em></p>
<p>Here, the <code>-czvf</code> flags are working as demonstrated before, but the original files are also removed. Once we list the files, we only see the archive.</p>
<h3 id="heading-how-to-view-the-contents-of-an-archive">How to view the contents of an archive</h3>
<p>You might need to view the contents of an archive without actually extracting it. You can do this with the <code>-t</code> flag.</p>
<pre><code class="lang-bash">tar -tvf logs_archive.tar.gz
</code></pre>
<p>In this command, <code>-t</code> flag specifies that we need to only view the contents of the archive. <code>-f</code> specifies the filename and <code>-v</code> displays the detailed contents.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/10/image-4.png" alt="Image" width="600" height="400" loading="lazy">
<em>Viewing contents of an archive.</em></p>
<h3 id="heading-how-to-extract-an-archive">How to extract an archive</h3>
<p>To extract files from an archive, you use the <code>-x</code> flag like this:</p>
<pre><code class="lang-bash">tar -xzvf logs_archive.tar.gz
</code></pre>
<p>Let's break down this command and look into each flag.</p>
<p><code>-x</code> is extracting and archive.</p>
<p><code>-z</code> specifies that the archive is gzip.</p>
<p><code>-v</code> is providing details of the files that have been archived.</p>
<p><code>-f</code> is extracting from the archive named 'logs_archive.tar.gz'.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/10/image-14.png" alt="Image" width="600" height="400" loading="lazy">
<em>Extracting an archive.</em></p>
<p>Here's a useful tip: commands that take a long time to execute can continue in the background with <code>&amp;</code>. </p>
<p>Adding files to an archive and extracting an archive can take a while. To keep the commands running in the background while you keep working, pair the command with <code>&amp;</code> like this:</p>
<pre><code class="lang-bash">tar -xzvf logs_archive.tar.gz &amp;
</code></pre>
<h3 id="heading-how-to-search-in-compressed-log-files">How to search in compressed log files</h3>
<p>You might still need to access certain files once they're archived. Luckily, there is a method you can use to search and view compressed log files without decompressing them and compromising disk space.</p>
<p>The command you can use to search in compressed files is <code>zgrep</code>:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/10/image-6.png" alt="Image" width="600" height="400" loading="lazy">
<em>Contents of a file 'audit.log' in an archive.</em></p>
<p>We can search for a string in an archive using the below command:</p>
<pre><code class="lang-bash"> zgrep -Hna <span class="hljs-string">'string-to-search'</span> compressedFile.tar.gz
</code></pre>
<p>Let's briefly look at the flags.</p>
<p><code>-H</code> lists the file name that contains the match.</p>
<p><code>-n</code> displays the line number that contains the matched string.</p>
<p><code>-a</code> treats all files as text files. </p>
<p>This is the result:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/10/image-15.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>File compression helps us save time and resources when sharing files. Servers are almost always rotating and archiving huge log files. </p>
<p>You can also schedule file compression via <code>cron</code> jobs to automate disk cleaning. I highly recommend that you take advantage of this utility.</p>
<p>Thanks for reading until the end. I would love to connect with you. You can find me <a target="_blank" href="https://twitter.com/hira_zaira">here</a> on Twitter. Do share your thoughts.</p>
<p>See you around.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Where do all the bytes come from? ]]>
                </title>
                <description>
                    <![CDATA[ By Colt McAnlis Great question Dion! I will answer it, and not just because you’re my new boss, but because it’s a good question. (but also because you’re my new boss.) I want to set something clear here though : we’re not really comparing Apples-to-... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/where-do-all-the-bytes-come-from-f51586690fd0/</link>
                <guid isPermaLink="false">66c3665640438b5931fe09ac</guid>
                
                    <category>
                        <![CDATA[ compression ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Game Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Image Compression ]]>
                    </category>
                
                    <category>
                        <![CDATA[ learning ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sat, 12 Dec 2015 19:39:33 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/0*qimWOyFboQoLx32R." medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Colt McAnlis</p>
<p>Great question Dion! I will answer it, and not just <em>because</em> you’re my new boss, but because it’s a good question. (but also because <a target="_blank" href="https://medium.com/ben-and-dion/heading-to-google-take-2-0-153841c6e1ae#.asvmj5s45">you’re my new boss</a>.)</p>
<p>I want to set something clear here though : we’re not really comparing Apples-to-Apples, so let’s define some technologies first.</p>
<h3 id="heading-how-mario-works">How Mario Works</h3>
<p>So let’s talk about how the original Super Mario game worked, from an asset perspective.</p>
<p>The original NES console was only designed to output images that were 256 wide by 240 high; meaning that the final image that needed to be displayed to the screen was 180kb in size.</p>
<p>Besides that, the NES only had 2kb of RAM. A cartridge itself could could hold 8k to 1mb of game data. So, there was no way to fit the entire game’s contents into main memory. Basically, a subset of the 1MB cartridge data must be loaded into the 2kb RAM and used to render the 180kb screen. How does one achieve that?</p>
<p><a target="_blank" href="https://en.wikipedia.org/wiki/Sprite_%28computer_graphics%29">SpriteSheets</a>.</p>
<p>Sprite sheets contain small tiles of graphics, that are re-used over and over again. For example, below is a remake of the original super mario sprite sheet:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*dwRaOKJJTu_i9TvZ." alt="Image" width="304" height="192" loading="lazy">
<em>It’s not the exact original sprite sheet, but you can get a sense of the small blocks of data that can be use over-and-over again.</em></p>
<p>Each small 16x16 pixel square represents a “tile” and artists would string these together to create the actual levels. The levels themselves, just became a giant 2D array of indexes into the sprite sheet. (I talk about this in a lot more detail in Lesson 3 of my <a target="_blank" href="https://www.udacity.com/course/html5-game-development--cs255">HTML5 Game Development course @ Udacity</a>, or in my book <a target="_blank" href="http://www.apress.com/9781430266976">HTML5 Game Development Insights</a>.) Tack on some <a target="_blank" href="https://en.wikipedia.org/wiki/Run-length_encoding">Run-Length-Encoding</a>, or some basic <a target="_blank" href="https://www.youtube.com/watch?v=Jqc418tQDkg">LZ77</a>, and you get a fairly compact format for levels.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*qimWOyFboQoLx32R." alt="Image" width="800" height="105" loading="lazy">
<em>Behold, that level that you parents could never complete.</em></p>
<p>So with concepts like tile-sheets and sprite-sheets, we can use a small set of images to create large scenes &amp; worlds. This is generally how most games work. Even 3D games will have a set of common textures that are applied multiple times and places throughout the game itself.</p>
<p>Now let’s talk about generic image compression.</p>
<h3 id="heading-how-images-are-compressed">How Images are compressed</h3>
<p>Here’s the “<em>not fair</em>” part of this comparison. Generic image compression algorithms have no domain knowledge about the pixels inside of them. JPG, PNG, WebP have all been designed for <em>photos</em> and not so much <em>game screens</em>. The result is that for a given 16x16 pixel block, these algorithms assume it’s unique among the image; Besides some color quantization, there’s no real logic added to determine if another 16x16 block could be an <em>exact duplicate</em> of the current one. This generally means there’s a lower-bound on how a given block of data is compressed.</p>
<p>For example, <a target="_blank" href="https://en.wikipedia.org/wiki/JPEG">JPG</a> breaks a given image into 8x8 pixel blocks, converts the RGB color space into the <a target="_blank" href="https://en.wikipedia.org/wiki/YCbCr">YCbCr</a> version, and then applies <a target="_blank" href="https://en.wikipedia.org/wiki/Discrete_cosine_transform">Discrete cosine transform</a> on them. It’s only <strong>after</strong> this step, does a lossless encoder come along, and see if it can match up common duplicate groups using DPCM, or RLE.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*xdUjGXy1FE9sgpqE." alt="Image" width="474" height="325" loading="lazy">
<em>A block-view of how JPG image compression works.</em></p>
<p>So, the only place where two blocks might get compacted into 1 block, is if their post-DCTd version is identical, and RLE can make stride recommendations. This doesn’t happen that often.</p>
<p><a target="_blank" href="https://www.youtube.com/watch?v=jHXzzHElFPk">Despite its other flaws</a>, PNG is much better in this regard. PNG compression is entirely lossless, (so your image quality is high, but your compression savings are low), and based on the DEFLATE codec, which pairs <a target="_blank" href="https://www.youtube.com/watch?v=Jqc418tQDkg">LZSS</a> along with <a target="_blank" href="https://www.youtube.com/watch?v=FdMoL3PzmSA">Arithmetic Compression</a>. The result is that long runs of similar pixels can end up being cut down to a much smaller size. This is why an image with a very uniform background will always be smaller as a PNG instead of a JPG.</p>
<h4 id="heading-the-below-image-is-a-59kb-png-file-while-the-jpg-image-is-106kb">The below image is a 5.9kb PNG file, while the JPG image is 106kb</h4>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*sUO8JCbLZc_i-524." alt="Image" width="800" height="387" loading="lazy">
<em>Since this image has lots of duplicate pixels (the blue sky background) compressors like PNG do a better job than their block-based JPG counterparts.</em></p>
<h3 id="heading-apples-vs-dragonfruit">Apples vs. Dragonfruit</h3>
<p>My point here is that it’s kinda unfair to compare game content to a single image on the internet.</p>
<p>From the game side, you start with a small set of re-usable tiles, and index them to build up your larger image, we can do this, because we know how the game is going to be made. On the other side, JPG/PNG/WebP just tries to compress the data it can find in local blocks, without any real desire to match repeated content. Image compression is clearly at a disadvantage here, since they don’t have prior knowledge of their data space, they can’t really make any expectations about it.</p>
<p>I mean, consider <a target="_blank" href="https://en.wikipedia.org/wiki/Demoscene">The Demo Scene</a> which is super big on this <a target="_blank" href="https://en.wikipedia.org/wiki/.kkrieger">sort of thing</a>. They can cram 30minutes of an entire 3d shooter into 64kb because they understand and know so much more about their data.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*KFIQVD2tHQ3nu9GBEu3RWg.jpeg" alt="Image" width="800" height="209" loading="lazy">
_A much better comparison. The .[kkrieger](https://en.wikipedia.org/wiki/.kkrieger" rel="noopener" target="<em>blank" title=") demo fit 30 minutes of a 3D shooter gameplay, with physics, sound, textures, and AI into 64kb of data. Seems like a massive amount of gain for just 24kb more than the original Mario.</em></p>
<p>It just goes to show, with the right amount of foreknowledge about your data, you can do great things with compression.</p>
<h3 id="heading-looking-forward">Looking forward.</h3>
<p>Obviously, we’ve grown up since the 256x240 displays of the NES days. <a target="_blank" href="https://www.google.com/nexus/5x/">The phone in my pocket</a> has 1,920 x 1,080 display pixels @ it’s 5.2” size, giving it ~423 pixels per inch density. To compare that in the same number of pixels that’s ~33 super-mario screens, or rather, 8MB of pixel data. I don’t think anyone’s surprised that screen resolutions are increasing, but it also comes with the need for <em>more data</em>.</p>
<p>This is something I’ve been <a target="_blank" href="https://www.youtube.com/watch?v=dmX2MpEBYhw">harping on for a while now</a>. While we get bigger displays, the content channels need to up their resolution outputs in order to still look good on our higher-density setups (otherwise, <a target="_blank" href="http://www.leemunroe.com/designing-for-high-resolution-retina-displays/">we get scaling blurryness..</a>). This of course, causes our video game packages to grow in size, our <a target="_blank" href="http://royal.pingdom.com/2011/11/21/web-pages-getting-bloated-here-is-why/">web-pages to grow in size</a>, and even our <a target="_blank" href="http://mashable.com/2014/01/03/youtube-4k-ces/">youtube streaming videos</a> to grow in size. Basically, we’re sending more data to smaller devices simply due to screen resolution. Which, for the next 2 billion folks in emerging markets, on 2G connections, is like the worst idea ever.</p>
<p>But I digress. That’s a different post.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
