diff --git a/src/emcee/backends/hdf.py b/src/emcee/backends/hdf.py
index 9503512..0f0c0de 100644
--- a/src/emcee/backends/hdf.py
+++ b/src/emcee/backends/hdf.py
@@ -22,9 +22,9 @@ except ImportError:
 def does_hdf5_support_longdouble():
     if h5py is None:
         return False
-    with NamedTemporaryFile(prefix="emcee-temporary-hdf5",
-                            suffix=".hdf5",
-                            delete=False) as f:
+    with NamedTemporaryFile(
+        prefix="emcee-temporary-hdf5", suffix=".hdf5", delete=False
+    ) as f:
         f.close()
 
         with h5py.File(f.name, "w") as hf:
@@ -53,12 +53,23 @@ class HDFBackend(Backend):
             ``RuntimeError`` if the file is opened with write access.
 
     """
-    def __init__(self, filename, name="mcmc", read_only=False, dtype=None):
+
+    def __init__(
+        self,
+        filename,
+        name="mcmc",
+        read_only=False,
+        dtype=None,
+        compression=None,
+        compression_opts=None,
+    ):
         if h5py is None:
             raise ImportError("you must install 'h5py' to use the HDFBackend")
         self.filename = filename
         self.name = name
         self.read_only = read_only
+        self.compression = compression
+        self.compression_opts = compression_opts
         if dtype is None:
             self.dtype_set = False
             self.dtype = np.float64
@@ -109,18 +120,27 @@ class HDFBackend(Backend):
             g.attrs["ndim"] = ndim
             g.attrs["has_blobs"] = False
             g.attrs["iteration"] = 0
-            g.create_dataset("accepted", data=np.zeros(nwalkers))
+            g.create_dataset(
+                "accepted",
+                data=np.zeros(nwalkers),
+                compression=self.compression,
+                compression_opts=self.compression_opts,
+            )
             g.create_dataset(
                 "chain",
                 (0, nwalkers, ndim),
                 maxshape=(None, nwalkers, ndim),
                 dtype=self.dtype,
+                compression=self.compression,
+                compression_opts=self.compression_opts,
             )
             g.create_dataset(
                 "log_prob",
                 (0, nwalkers),
                 maxshape=(None, nwalkers),
                 dtype=self.dtype,
+                compression=self.compression,
+                compression_opts=self.compression_opts,
             )
 
     def has_blobs(self):
@@ -206,6 +226,8 @@ class HDFBackend(Backend):
                         (ntot, nwalkers),
                         maxshape=(None, nwalkers),
                         dtype=dt,
+                        compression=self.compression,
+                        compression_opts=self.compression_opts,
                     )
                 else:
                     g["blobs"].resize(ntot, axis=0)
@@ -239,18 +261,25 @@ class HDFBackend(Backend):
 
 
 class TempHDFBackend(object):
-
-    def __init__(self, dtype=None):
+    def __init__(self, dtype=None, compression=None, compression_opts=None):
         self.dtype = dtype
         self.filename = None
+        self.compression = compression
+        self.compression_opts = compression_opts
 
     def __enter__(self):
-        f = NamedTemporaryFile(prefix="emcee-temporary-hdf5",
-                               suffix=".hdf5",
-                               delete=False)
+        f = NamedTemporaryFile(
+            prefix="emcee-temporary-hdf5", suffix=".hdf5", delete=False
+        )
         f.close()
         self.filename = f.name
-        return HDFBackend(f.name, "test", dtype=self.dtype)
+        return HDFBackend(
+            f.name,
+            "test",
+            dtype=self.dtype,
+            compression=self.compression,
+            compression_opts=self.compression_opts,
+        )
 
     def __exit__(self, exception_type, exception_value, traceback):
         os.remove(self.filename)

Enable HDF5 compression in the backend
**General information:**

- emcee version: 3.0.2
- platform: (Arch) Linux
- installation method (pip/conda/source/other?): AUR repository

**Problem description:**
It would be nice to be able to enable compression when using HDF5 files via the emcee backends. A quick skim of the documentation and `backends/hdf.py` file suggests this isn't currently possible. Perhaps the h5py `compression=` argument could be exposed in the `emcee.backends.HDFBackend` class?
diff --git a/src/emcee/tests/unit/test_backends.py b/src/emcee/tests/unit/test_backends.py
index 370008e..b7a01d3 100644
--- a/src/emcee/tests/unit/test_backends.py
+++ b/src/emcee/tests/unit/test_backends.py
@@ -1,8 +1,8 @@
 # -*- coding: utf-8 -*-
 
 import os
+from os.path import join
 from itertools import product
-from tempfile import NamedTemporaryFile
 
 import numpy as np
 import pytest
@@ -263,3 +263,14 @@ def test_longdouble_preserved(backend):
 
             assert s.log_prob.dtype == np.longdouble
             assert np.all(s.log_prob == lp)
+
+
+@pytest.mark.skipif(h5py is None, reason="HDF5 not available")
+def test_hdf5_compression():
+    with backends.TempHDFBackend(compression="gzip") as b:
+        run_sampler(b, blobs=True)
+        # re-open and read
+        b.get_chain()
+        b.get_blobs()
+        b.get_log_prob()
+        b.accepted

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest h5py numpy>=1.16.0 pandas>=1.0.0
apt-get update apt-get install -y gcc
3.9
requirements/base.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
-e git+https://github.com/dfm/emcee.git@049216c600d3829979baaa6c63078381a8e983b4#egg=emcee
exceptiongroup==1.2.2
h5py==3.13.0
iniconfig==2.1.0
numpy==2.0.2
packaging==24.2
pandas==2.2.3
pluggy==1.5.0
pytest==8.3.5
python-dateutil==2.9.0.post0
pytz==2025.2
six==1.17.0
tomli==2.2.1
tzdata==2025.2

name: emcee
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - emcee==3.0.3.dev26+g049216c
      - exceptiongroup==1.2.2
      - h5py==3.13.0
      - iniconfig==2.1.0
      - numpy==2.0.2
      - packaging==24.2
      - pandas==2.2.3
      - pluggy==1.5.0
      - pytest==8.3.5
      - python-dateutil==2.9.0.post0
      - pytz==2025.2
      - six==1.17.0
      - tomli==2.2.1
      - tzdata==2025.2
prefix: /opt/conda/envs/emcee

src/emcee/tests/unit/test_backends.py::test_hdf5_compression
src/emcee/tests/unit/test_backends.py::test_uninit_errors[Backend] src/emcee/tests/unit/test_backends.py::test_blob_usage_errors[Backend] src/emcee/tests/unit/test_backends.py::test_backend[TempHDFBackend-None-True] src/emcee/tests/unit/test_backends.py::test_backend[TempHDFBackend-None-False] src/emcee/tests/unit/test_backends.py::test_backend[TempHDFBackend-dtype2-True] src/emcee/tests/unit/test_backends.py::test_backend[TempHDFBackend-dtype3-False] src/emcee/tests/unit/test_backends.py::test_restart[TempHDFBackend-None] src/emcee/tests/unit/test_backends.py::test_restart[TempHDFBackend-dtype1]
src/emcee/tests/unit/test_backends.py::test_uninit src/emcee/tests/unit/test_backends.py::test_uninit_errors[TempHDFBackend] src/emcee/tests/unit/test_backends.py::test_blob_usage_errors[TempHDFBackend] src/emcee/tests/unit/test_backends.py::test_reload[TempHDFBackend-None] src/emcee/tests/unit/test_backends.py::test_reload[TempHDFBackend-dtype1] src/emcee/tests/unit/test_backends.py::test_multi_hdf5 src/emcee/tests/unit/test_backends.py::test_longdouble_preserved[Backend] src/emcee/tests/unit/test_backends.py::test_longdouble_preserved[TempHDFBackend]
src/emcee/backends/hdf.py
src/emcee/backends/hdf.py