Multhreadown

一个功能强大的多线程下载管理器,支持断点续传、暂停恢复和速率限制。
特性
- 🚀 多线程下载:利用多线程并行下载文件的不同部分,显著提高下载速度
- ⏸️ 暂停与恢复:支持随时暂停下载并在之后恢复,无需重新开始
- 📊 进度跟踪:实时显示下载进度、速度和剩余时间
- 🔄 断点续传:自动从上次中断的位置继续下载
- 🚦 速率限制:可配置的下载速率限制,避免占用过多带宽
- 🛡️ 错误处理:健壮的错误处理和自动重试机制
安装
将以下内容添加到您的 Cargo.toml 文件中:
[dependencies]
multhreadown = "0.1.0"
快速开始
基本用法
use multhreadown::{Config, download_all_files, DefaultEventHandler};
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
let config = Config::new()
.with_threads(4)
.with_chunk_size(1024 * 1024)
.with_retry_attempts(3);
let urls = vec![
"https://example.com/file1.zip",
"https://example.com/file2.zip",
];
let output_dir = "/path/to/downloads";
let event_handler = DefaultEventHandler::new();
download_all_files(&urls, output_dir, &config, event_handler)?;
println!("所有文件下载完成!");
Ok(())
}
自定义事件处理
您可以实现自己的事件处理器来响应下载过程中的各种事件:
use multhreadown::{events::{DownloadEventHandler, DownloadEvent}, DownloadStats};
use std::sync::Arc;
struct MyEventHandler;
impl DownloadEventHandler for MyEventHandler {
fn on_event(&self, event: DownloadEvent) {
match event {
DownloadEvent::DownloadStarted(url) => {
println!("开始下载: {}", url);
}
DownloadEvent::DownloadCompleted(url) => {
println!("下载完成: {}", url);
}
DownloadEvent::ProgressUpdated(url, stats) => {
println!(
"{}: 已下载 {}/{} ({:.2}%), 速度: {}/s",
url,
stats.bytes_downloaded,
stats.total_bytes,
stats.progress * 100.0,
format_bytes(stats.download_speed as usize)
);
}
_ => {}
}
}
}
fn format_bytes(bytes: usize) -> String {
}
使用缓存管理器
use multhreadown::{CacheManager, DownloadCache};
let cache_manager = CacheManager::new("/path/to/cache/directory")?;
let download_cache = DownloadCache {
url: "https://example.com/file.zip".to_string(),
file_path: "/path/to/downloads/file.zip".to_string(),
total_size: 1024000,
downloaded_chunks: vec![(0, 512000)], };
cache_manager.save_download_state(&download_cache)?;
let cached_download = cache_manager.load_download_state("https://example.com/file.zip")?;
高级配置
use multhreadown::Config;
let config = Config::new()
.with_threads(8) .with_chunk_size(2 * 1024 * 1024) .with_retry_attempts(5) .with_retry_delay(std::time::Duration::from_secs(3)) .with_rate_limit(Some(1024 * 1024)) .with_connect_timeout(std::time::Duration::from_secs(30)) .with_user_agent("MyDownloader/1.0");
命令行界面
Multhreadown 也提供了命令行界面:
cargo install multhreadown
multhreadown download https://example.com/file.zip --output /path/to/downloads --threads 4
multhreadown --help
贡献
欢迎贡献!请随时提交问题或拉取请求。
许可证
本项目采用 MIT 。