Working with the File System

Compresssing Files using std::filesystem

Can I use std::filesystem to compress files?

Abstract art representing computer programming

std::filesystem does not provide built-in support for compressing files. It is designed primarily for file and directory manipulation, such as creating, deleting, moving, and copying files and directories. To compress files, you will need to use other libraries or APIs.

One popular library for compressing files in C++ is zlib. It provides functions to compress and decompress data using the DEFLATE compression algorithm. Here’s a basic example demonstrating how to use zlib to compress a file:

First, you need to include the zlib library in your project. You can download it from

#include <zlib.h>

#include <filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;

void compress_file(
  const fs::path &source,
  const fs::path &destination) {
  std::ifstream input(
    source, std::ios_base::binary);
  std::ofstream output(
    destination, std::ios_base::binary);

  if (!input || !output) {
    std::cerr << "Failed to open file\n";

  constexpr int buffer_size = 128 * 1024;
  char buffer[buffer_size];

  z_stream zlib_stream;
  zlib_stream.zalloc = Z_NULL;
  zlib_stream.zfree = Z_NULL;
  zlib_stream.opaque = Z_NULL;
  zlib_stream.avail_in = 0;
  zlib_stream.next_in = Z_NULL;

  deflateInit(&zlib_stream, Z_DEFAULT_COMPRESSION);

  int flush;
  do {, buffer_size);
    zlib_stream.avail_in = input.gcount();
    flush = input.eof() ? Z_FINISH : Z_NO_FLUSH;
    zlib_stream.next_in =
      reinterpret_cast<unsigned char *>(buffer);

    do {
      char out_buffer[buffer_size];
      zlib_stream.avail_out = buffer_size;
      zlib_stream.next_out = reinterpret_cast<
        unsigned char *>(out_buffer);
      deflate(&zlib_stream, flush);
        buffer_size - zlib_stream.avail_out
    } while (zlib_stream.avail_out == 0);
  } while (flush != Z_FINISH);


int main() {
  fs::path source{R"(c:\test\hello.txt)"};          
  fs::path destination{R"(c:\test\hello.txt.gz)"};  
  compress_file(source, destination);
  std::cout << "File compressed\n";
File compressed

In this example:

  • We define source as the path to the file we want to compress and destination as the path to the compressed file.
  • We open the input file in binary mode and the output file in binary mode.
  • We set up the z_stream structure for zlib and initialize it with deflateInit.
  • We read chunks of data from the input file, compress them using deflate, and write the compressed data to the output file.
  • We use Z_FINISH to indicate the end of the input file and finalize the compression with deflateEnd.

This example demonstrates basic file compression using zlib. For more advanced usage, including handling errors and different compression levels, refer to the zlib documentation.

Alternatively, you can use other compression libraries like Boost.Iostreams or libarchive for more comprehensive file compression and decompression capabilities.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved