| Crates.io | file_icon_provider |
| lib.rs | file_icon_provider |
| version | 1.0.0 |
| created_at | 2024-11-25 18:49:45.172239+00 |
| updated_at | 2026-01-10 07:19:02.777769+00 |
| description | Cross-platform Rust library to retrieve file icons on Windows, MacOS and Linux. |
| homepage | |
| repository | https://github.com/IohannRabeson/file_icon_provider |
| max_upload_size | |
| id | 1460591 |
| size | 180,006 |
File Icon Provider is a cross-platform Rust library designed to simplify the retrieval of file icons on Windows, MacOS and Linux (Gnome).
Use the [get_file_icon] function to retrieve the icon for a specific file path.
//! Extract and save the system icon associated with any file.
//!
//! Usage: cargo run --example save_icon <source_file> <output_name>
//! Example: cargo run --example save_icon document.pdf icon.png
use file_icon_provider::get_file_icon;
use clap::Parser;
use image::{DynamicImage, RgbaImage};
use std::path::PathBuf;
#[derive(Parser)]
#[command(version, about = "Retrieve and save the icon associated with any file.", long_about = None)]
struct Cli {
/// The file we want to extract the icon.
file_path: PathBuf,
/// The file path of the extracted image.
output_path: PathBuf,
}
fn main() {
let cli = Cli::parse();
let icon = get_file_icon(cli.file_path, 32).expect("Failed to get icon");
let image = RgbaImage::from_raw(icon.width, icon.height, icon.pixels)
.map(DynamicImage::ImageRgba8)
.expect("Failed to convert Icon to Image");
match image.save_with_format(&cli.output_path, image::ImageFormat::Png) {
Err(error) => {
println!("Failed to save the image: {}", error);
}
_ => println!("Saved image: '{}'", cli.output_path.display()),
}
}
Examples are available in the examples directory.
Linux support is limited, and the library must be called from the main thread.
On Linux the tests will fail miserably when running cargo test because each test runs in a different thread and GTK API does not like that. The tests pass if you run them one by one.
It works on Github Action because I think their VMs are limited to one thread.
On Linux you need to install theses packages:
sudo apt install libegl-mesa0 libgtk-4-dev libgtk-3-dev libatk1.0-dev