_archive_write_set_format_from_context (struct archive *a,
					SaveData       *save_data)
{
	const char *mime_type;
	int         archive_filter;

	/* set format and filter from the mime type */

	mime_type = fr_archive_get_mime_type (LOAD_DATA (save_data)->archive);
	archive_filter = ARCHIVE_FILTER_NONE;

	if (_g_str_equal (mime_type, "application/x-bzip-compressed-tar")) {
		archive_write_set_format_pax_restricted (a);
		archive_filter = ARCHIVE_FILTER_BZIP2;
	}
	else if (_g_str_equal (mime_type, "application/x-tarz")) {
		archive_write_set_format_pax_restricted (a);
		archive_filter = ARCHIVE_FILTER_COMPRESS;
	}
	else if (_g_str_equal (mime_type, "application/x-compressed-tar")) {
		archive_write_set_format_pax_restricted (a);
		archive_filter = ARCHIVE_FILTER_GZIP;
	}
	else if (_g_str_equal (mime_type, "application/x-lrzip-compressed-tar")) {
		archive_write_set_format_pax_restricted (a);
		archive_filter = ARCHIVE_FILTER_LRZIP;
	}
	else if (_g_str_equal (mime_type, "application/x-lzip-compressed-tar")) {
		archive_write_set_format_pax_restricted (a);
		archive_filter = ARCHIVE_FILTER_LZIP;
	}
	else if (_g_str_equal (mime_type, "application/x-lzma-compressed-tar")) {
		archive_write_set_format_pax_restricted (a);
		archive_filter = ARCHIVE_FILTER_LZMA;
	}
	else if (_g_str_equal (mime_type, "application/x-tzo")) {
		archive_write_set_format_pax_restricted (a);
		archive_filter = ARCHIVE_FILTER_LZOP;
	}
	else if (_g_str_equal (mime_type, "application/x-xz-compressed-tar")) {
		archive_write_set_format_pax_restricted (a);
		archive_filter = ARCHIVE_FILTER_XZ;
	}
#if (ARCHIVE_VERSION_NUMBER >= 3003003)
	else if (_g_str_equal (mime_type, "application/x-zstd-compressed-tar")) {
		archive_write_set_format_pax_restricted (a);
		archive_filter = ARCHIVE_FILTER_ZSTD;
	}
#endif
	else if (_g_str_equal (mime_type, "application/x-tar")) {
		archive_write_add_filter_none (a);
		archive_write_set_format_pax_restricted (a);
	}
	else if (_g_str_equal (mime_type, "application/x-cd-image")) {
		archive_write_set_format_iso9660 (a);
	}
	else if (_g_str_equal (mime_type, "application/x-cpio")) {
		archive_write_set_format_cpio (a);
	}
	else if (_g_str_equal (mime_type, "application/x-xar")) {
		archive_write_set_format_xar (a);
	}
	else if (_g_str_equal (mime_type, "application/x-ar")) {
		archive_write_set_format_ar_svr4 (a);
	}
	else if (_g_str_equal (mime_type, "application/x-7z-compressed")) {
		archive_write_set_format_7zip (a);
	}
	else if (_g_str_equal (mime_type, "application/zip")
	    || _g_str_equal (mime_type, "application/x-cbz")) {
		archive_write_set_format_zip (a);
	}

	/* set the filter */

	if (archive_filter != ARCHIVE_FILTER_NONE) {
		char *compression_level = NULL;

		switch (archive_filter) {
		case ARCHIVE_FILTER_BZIP2:
			archive_write_add_filter_bzip2 (a);
			break;
		case ARCHIVE_FILTER_COMPRESS:
			archive_write_add_filter_compress (a);
			break;
		case ARCHIVE_FILTER_GZIP:
			archive_write_add_filter_gzip (a);
			break;
		case ARCHIVE_FILTER_LRZIP:
			archive_write_add_filter_lrzip (a);
			break;
		case ARCHIVE_FILTER_LZIP:
			archive_write_add_filter_lzip (a);
			break;
		case ARCHIVE_FILTER_LZMA:
			archive_write_add_filter_lzma (a);
			break;
		case ARCHIVE_FILTER_LZOP:
			archive_write_add_filter_lzop (a);
			break;
		case ARCHIVE_FILTER_XZ:
			archive_write_add_filter_xz (a);
			break;
#if (ARCHIVE_VERSION_NUMBER >= 3003003)
		case ARCHIVE_FILTER_ZSTD:
			archive_write_add_filter_zstd (a);
			break;
#endif
		default:
			break;
		}

		/* set the compression level */

		compression_level = NULL;
#if (ARCHIVE_VERSION_NUMBER >= 3003003)
		if (archive_filter == ARCHIVE_FILTER_ZSTD) {
			switch (save_data->compression) {
			case FR_COMPRESSION_VERY_FAST:
				compression_level = "1";
				break;
			case FR_COMPRESSION_FAST:
				compression_level = "2";
				break;
			case FR_COMPRESSION_NORMAL:
				compression_level = "3";
				break;
			case FR_COMPRESSION_MAXIMUM:
				compression_level = "22";
				break;
			}
		}
		else
#endif
		{
			switch (save_data->compression) {
			case FR_COMPRESSION_VERY_FAST:
				compression_level = "1";
				break;
			case FR_COMPRESSION_FAST:
				compression_level = "3";
				break;
			case FR_COMPRESSION_NORMAL:
				compression_level = "6";
				break;
			case FR_COMPRESSION_MAXIMUM:
				compression_level = "9";
				break;
			}
		}
		if (compression_level != NULL)
			archive_write_set_filter_option (a, NULL, "compression-level", compression_level);
	}
}