CachedLayer writeCompressed(Blob compressedLayerBlob) throws IOException {
    // Creates the layers directory if it doesn't exist.
    Files.createDirectories(cacheStorageFiles.getLayersDirectory());

    // Creates the temporary directory.
    Files.createDirectories(cacheStorageFiles.getTemporaryDirectory());
    try (TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider()) {
      Path temporaryLayerDirectory =
          tempDirectoryProvider.newDirectory(cacheStorageFiles.getTemporaryDirectory());

      // Writes the layer file to the temporary directory.
      WrittenLayer writtenLayer =
          writeCompressedLayerBlobToDirectory(compressedLayerBlob, temporaryLayerDirectory);

      // Moves the temporary directory to the final location.
      moveIfDoesNotExist(
          temporaryLayerDirectory, cacheStorageFiles.getLayerDirectory(writtenLayer.layerDigest));

      // Updates cachedLayer with the blob information.
      Path layerFile =
          cacheStorageFiles.getLayerFile(writtenLayer.layerDigest, writtenLayer.layerDiffId);
      return CachedLayer.builder()
          .setLayerDigest(writtenLayer.layerDigest)
          .setLayerDiffId(writtenLayer.layerDiffId)
          .setLayerSize(writtenLayer.layerSize)
          .setLayerBlob(Blobs.from(layerFile))
          .build();
    }
  }
@Test
  public void testWriteZstdCompressed() throws IOException {
    Blob uncompressedLayerBlob = Blobs.from("uncompressedLayerBlob");
    Blob compressedLayerBlob = compress(uncompressedLayerBlob, CompressorStreamFactory.ZSTANDARD);

    CachedLayer cachedLayer = cacheStorageWriter.writeCompressed(compressedLayerBlob);

    verifyCachedLayer(cachedLayer, uncompressedLayerBlob, compressedLayerBlob);
  }