yimi-rutool 0.2.2

A comprehensive Rust utility library inspired by Hutool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//! File compression and decompression utilities
//!
//! This module provides functionality for compressing and decompressing files
//! and directories using various compression formats.

use crate::error::{Error, Result};
use std::fs::{File, create_dir_all};
use std::io::{Read, Write};
use std::path::Path;

#[cfg(feature = "zip")]
use zip::{ZipWriter, ZipArchive, write::FileOptions, CompressionMethod};

/// Supported compression formats
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompressionFormat {
    /// ZIP format
    Zip,
    /// GZIP format (single file)
    Gzip,
    /// TAR format (uncompressed archive)
    Tar,
    /// TAR.GZ format (compressed archive)
    TarGz,
}

impl CompressionFormat {
    /// Get file extension for this format
    pub fn extension(self) -> &'static str {
        match self {
            Self::Zip => "zip",
            Self::Gzip => "gz",
            Self::Tar => "tar",
            Self::TarGz => "tar.gz",
        }
    }

    /// Detect format from file extension
    pub fn from_extension(ext: &str) -> Option<Self> {
        match ext.to_lowercase().as_str() {
            "zip" => Some(Self::Zip),
            "gz" | "gzip" => Some(Self::Gzip),
            "tar" => Some(Self::Tar),
            "tar.gz" | "tgz" => Some(Self::TarGz),
            _ => None,
        }
    }

    /// Detect format from file path
    pub fn from_path<P: AsRef<Path>>(path: P) -> Option<Self> {
        let path_str = path.as_ref().to_string_lossy().to_lowercase();
        
        if path_str.ends_with(".tar.gz") || path_str.ends_with(".tgz") {
            Some(Self::TarGz)
        } else if let Some(ext) = path.as_ref().extension() {
            Self::from_extension(&ext.to_string_lossy())
        } else {
            None
        }
    }
}

/// Compression level
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompressionLevel {
    /// No compression (fastest)
    None,
    /// Fastest compression
    Fastest,
    /// Balanced compression and speed
    Balanced,
    /// Best compression (slowest)
    Best,
}

impl CompressionLevel {
    /// Convert to ZIP compression level
    #[cfg(feature = "zip")]
    fn to_zip_level(self) -> i32 {
        match self {
            Self::None => 0,
            Self::Fastest => 1,
            Self::Balanced => 6,
            Self::Best => 9,
        }
    }
}

/// Compression statistics
#[derive(Debug, Clone)]
pub struct CompressionStats {
    /// Original size in bytes
    pub original_size: u64,
    /// Compressed size in bytes
    pub compressed_size: u64,
    /// Compression ratio (compressed_size / original_size)
    pub compression_ratio: f64,
    /// Space saved in bytes
    pub space_saved: u64,
    /// Space saved percentage
    pub space_saved_percentage: f64,
    /// Number of files processed
    pub file_count: usize,
}

impl CompressionStats {
    /// Create new compression statistics
    pub fn new(original_size: u64, compressed_size: u64, file_count: usize) -> Self {
        let compression_ratio = if original_size > 0 {
            if original_size == 0 { 0.0 } else { (compressed_size as f64) / (original_size as f64) }
        } else {
            0.0
        };
        
        let space_saved = original_size.saturating_sub(compressed_size);
        let space_saved_percentage = if original_size > 0 {
            if original_size == 0 { 0.0 } else { ((space_saved as f64) / (original_size as f64)) * 100.0 }
        } else {
            0.0
        };

        Self {
            original_size,
            compressed_size,
            compression_ratio,
            space_saved,
            space_saved_percentage,
            file_count,
        }
    }
}

impl std::fmt::Display for CompressionStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Compression Statistics:")?;
        writeln!(f, "  Files processed: {}", self.file_count)?;
        writeln!(f, "  Original size: {} bytes", self.original_size)?;
        writeln!(f, "  Compressed size: {} bytes", self.compressed_size)?;
        writeln!(f, "  Compression ratio: {:.2}", self.compression_ratio)?;
        writeln!(f, "  Space saved: {} bytes ({:.1}%)", self.space_saved, self.space_saved_percentage)?;
        Ok(())
    }
}

/// Compression utility
pub struct CompressionUtil;

impl CompressionUtil {
    /// Compress a file or directory to ZIP format
    ///
    /// # Examples
    ///
    /// ```rust
    /// use yimi_rutool::extra::{CompressionUtil, CompressionLevel};
    ///
    /// // let stats = CompressionUtil::compress_zip(
    /// //     "source_directory",
    /// //     "output.zip",
    /// //     CompressionLevel::Balanced
    /// // ).unwrap();
    /// ```
    #[cfg(feature = "zip")]
    pub fn compress_zip<P: AsRef<Path>, Q: AsRef<Path>>(
        source: P,
        destination: Q,
        level: CompressionLevel,
    ) -> Result<CompressionStats> {
        let source_path = source.as_ref();
        let destination_path = destination.as_ref();

        if !source_path.exists() {
            return Err(Error::not_found(format!("Source path does not exist: {:?}", source_path)));
        }

        let file = File::create(destination_path)
            .map_err(|e| Error::validation(format!("Failed to create ZIP file: {e}")))?;
        
        let mut zip = ZipWriter::new(file);
        let options = FileOptions::<()>::default()
            .compression_method(CompressionMethod::Deflated)
            .compression_level(Some(level.to_zip_level() as i64));

        let mut original_size = 0u64;
        let mut file_count = 0usize;

        if source_path.is_file() {
            // Compress single file
            let file_name = source_path.file_name()
                .ok_or_else(|| Error::validation("Invalid file name".to_string()))?
                .to_string_lossy();
            
            zip.start_file(&file_name, options)
                .map_err(|e| Error::validation(format!("Failed to start ZIP file entry: {e}")))?;

            let mut source_file = File::open(source_path)
                .map_err(|e| Error::validation(format!("Failed to open source file: {e}")))?;
            
            let mut buffer = Vec::new();
            source_file.read_to_end(&mut buffer)
                .map_err(|e| Error::validation(format!("Failed to read source file: {e}")))?;
            
            original_size += buffer.len() as u64;
            file_count += 1;

            zip.write_all(&buffer)
                .map_err(|e| Error::validation(format!("Failed to write to ZIP: {e}")))?;
        } else if source_path.is_dir() {
            // Compress directory
            Self::compress_directory_to_zip(&mut zip, source_path, source_path, options, &mut original_size, &mut file_count)?;
        }

        let zip_file = zip.finish()
            .map_err(|e| Error::validation(format!("Failed to finish ZIP file: {e}")))?;
        
        let compressed_size = zip_file.metadata()
            .map_err(|e| Error::validation(format!("Failed to get ZIP file metadata: {e}")))?
            .len();

        Ok(CompressionStats::new(original_size, compressed_size, file_count))
    }

    /// Decompress a ZIP file
    #[cfg(feature = "zip")]
    pub fn decompress_zip<P: AsRef<Path>, Q: AsRef<Path>>(
        source: P,
        destination: Q,
    ) -> Result<CompressionStats> {
        let source_path = source.as_ref();
        let destination_path = destination.as_ref();

        if !source_path.exists() {
            return Err(Error::not_found(format!("ZIP file does not exist: {:?}", source_path)));
        }

        let file = File::open(source_path)
            .map_err(|e| Error::validation(format!("Failed to open ZIP file: {e}")))?;
        
        let mut archive = ZipArchive::new(file)
            .map_err(|e| Error::validation(format!("Failed to read ZIP archive: {e}")))?;

        let mut original_size = 0u64;
        let mut compressed_size = 0u64;
        let file_count = archive.len();

        for i in 0..archive.len() {
            let mut file = archive.by_index(i)
                .map_err(|e| Error::validation(format!("Failed to read ZIP entry {i}: {e}")))?;
            
            let outpath = destination_path.join(file.name());
            
            compressed_size += file.compressed_size();
            original_size += file.size();

            if file.name().ends_with('/') {
                // Directory
                create_dir_all(&outpath)
                    .map_err(|e| Error::validation(format!("Failed to create directory: {e}")))?;
            } else {
                // File
                if let Some(parent) = outpath.parent() {
                    create_dir_all(parent)
                        .map_err(|e| Error::validation(format!("Failed to create parent directory: {e}")))?;
                }
                
                let mut outfile = File::create(&outpath)
                    .map_err(|e| Error::validation(format!("Failed to create output file: {e}")))?;
                
                std::io::copy(&mut file, &mut outfile)
                    .map_err(|e| Error::validation(format!("Failed to extract file: {e}")))?;
            }
        }

        Ok(CompressionStats::new(original_size, compressed_size, file_count))
    }

    /// Compress data to GZIP format
    #[cfg(feature = "flate2")]
    pub fn compress_gzip(data: &[u8]) -> Result<Vec<u8>> {
        use flate2::{Compression, write::GzEncoder};
        
        let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
        encoder.write_all(data)
            .map_err(|e| Error::validation(format!("Failed to compress with GZIP: {e}")))?;
        
        encoder.finish()
            .map_err(|e| Error::validation(format!("Failed to finish GZIP compression: {e}")))
    }

    /// Decompress GZIP data
    #[cfg(feature = "flate2")]
    pub fn decompress_gzip(data: &[u8]) -> Result<Vec<u8>> {
        use flate2::read::GzDecoder;
        
        let mut decoder = GzDecoder::new(data);
        let mut decompressed = Vec::new();
        decoder.read_to_end(&mut decompressed)
            .map_err(|e| Error::validation(format!("Failed to decompress GZIP: {}", e)))?;
        
        Ok(decompressed)
    }

    /// Compress a file with GZIP
    #[cfg(feature = "flate2")]
    pub fn compress_file_gzip<P: AsRef<Path>, Q: AsRef<Path>>(
        source: P,
        destination: Q,
    ) -> Result<CompressionStats> {
        use flate2::{Compression, write::GzEncoder};
        
        let source_path = source.as_ref();
        let destination_path = destination.as_ref();

        if !source_path.exists() {
            return Err(Error::not_found(format!("Source file does not exist: {:?}", source_path)));
        }

        let mut source_file = File::open(source_path)
            .map_err(|e| Error::validation(format!("Failed to open source file: {e}")))?;
        
        let destination_file = File::create(destination_path)
            .map_err(|e| Error::validation(format!("Failed to create destination file: {e}")))?;
        
        let mut encoder = GzEncoder::new(destination_file, Compression::default());
        
        let original_size = std::io::copy(&mut source_file, &mut encoder)
            .map_err(|e| Error::validation(format!("Failed to compress file: {}", e)))?;
        
        encoder.finish()
            .map_err(|e| Error::validation(format!("Failed to finish compression: {}", e)))?;

        let compressed_size = std::fs::metadata(destination_path)
            .map_err(|e| Error::validation(format!("Failed to get compressed file size: {}", e)))?
            .len();

        Ok(CompressionStats::new(original_size, compressed_size, 1))
    }

    /// Decompress a GZIP file
    #[cfg(feature = "flate2")]
    pub fn decompress_file_gzip<P: AsRef<Path>, Q: AsRef<Path>>(
        source: P,
        destination: Q,
    ) -> Result<CompressionStats> {
        use flate2::read::GzDecoder;
        
        let source_path = source.as_ref();
        let destination_path = destination.as_ref();

        if !source_path.exists() {
            return Err(Error::not_found(format!("GZIP file does not exist: {source_path:?}")));
        }

        let source_file = File::open(source_path)
            .map_err(|e| Error::validation(format!("Failed to open GZIP file: {e}")))?;
        
        let mut decoder = GzDecoder::new(source_file);
        
        let mut destination_file = File::create(destination_path)
            .map_err(|e| Error::validation(format!("Failed to create destination file: {e}")))?;
        
        let original_size = std::io::copy(&mut decoder, &mut destination_file)
            .map_err(|e| Error::validation(format!("Failed to decompress file: {e}")))?;

        let compressed_size = std::fs::metadata(source_path)
            .map_err(|e| Error::validation(format!("Failed to get source file size: {e}")))?
            .len();

        Ok(CompressionStats::new(original_size, compressed_size, 1))
    }

    /// Recursively compress a directory to ZIP
    #[cfg(feature = "zip")]
    fn compress_directory_to_zip(
        zip: &mut ZipWriter<File>,
        base_path: &Path,
        current_path: &Path,
        options: FileOptions<()>,
        total_size: &mut u64,
        file_count: &mut usize,
    ) -> Result<()> {
        let entries = std::fs::read_dir(current_path)
            .map_err(|e| Error::validation(format!("Failed to read directory: {e}")))?;

        for entry in entries {
            let entry = entry
                .map_err(|e| Error::validation(format!("Failed to read directory entry: {e}")))?;
            let path = entry.path();
            
            let relative_path = path.strip_prefix(base_path)
                .map_err(|e| Error::validation(format!("Failed to create relative path: {e}")))?;
            
            let name = relative_path.to_string_lossy().replace('\\', "/");

            if path.is_file() {
                zip.start_file(&name, options)
                    .map_err(|e| Error::validation(format!("Failed to start ZIP file entry: {e}")))?;

                let mut file = File::open(&path)
                    .map_err(|e| Error::validation(format!("Failed to open file: {e}")))?;
                
                let mut buffer = Vec::new();
                file.read_to_end(&mut buffer)
                    .map_err(|e| Error::validation(format!("Failed to read file: {e}")))?;
                
                *total_size += buffer.len() as u64;
                *file_count += 1;

                zip.write_all(&buffer)
                    .map_err(|e| Error::validation(format!("Failed to write to ZIP: {e}")))?;
            } else if path.is_dir() {
                // Add directory entry
                zip.add_directory(format!("{name}/"), options)
                    .map_err(|e| Error::validation(format!("Failed to add directory to ZIP: {e}")))?;
                
                // Recursively process subdirectory
                Self::compress_directory_to_zip(zip, base_path, &path, options, total_size, file_count)?;
            }
        }

        Ok(())
    }

    /// Get information about a ZIP file without extracting it
    /// 
    /// # Errors
    /// 
    /// Returns `Error` if:
    /// - ZIP file cannot be opened
    /// - ZIP archive cannot be read
    /// - Entry information cannot be accessed
    #[cfg(feature = "zip")]
    pub fn zip_info<P: AsRef<Path>>(path: P) -> Result<ZipInfo> {
        let file = File::open(&path)
            .map_err(|e| Error::validation(format!("Failed to open ZIP file: {e}")))?;
        
        let mut archive = ZipArchive::new(file)
            .map_err(|e| Error::validation(format!("Failed to read ZIP archive: {e}")))?;

        let mut files = Vec::new();
        let mut total_compressed_size = 0u64;
        let mut total_uncompressed_size = 0u64;

        for i in 0..archive.len() {
            let file = archive.by_index(i)
                .map_err(|e| Error::validation(format!("Failed to read ZIP entry {i}: {e}")))?;
            
            let info = ZipFileInfo {
                name: file.name().to_string(),
                compressed_size: file.compressed_size(),
                uncompressed_size: file.size(),
                is_directory: file.name().ends_with('/'),
                compression_method: format!("{:?}", file.compression()),
            };

            total_compressed_size += file.compressed_size();
            total_uncompressed_size += file.size();
            files.push(info);
        }

        Ok(ZipInfo {
            file_count: archive.len(),
            total_compressed_size,
            total_uncompressed_size,
            compression_ratio: if total_uncompressed_size > 0 {
                if total_uncompressed_size == 0 { 0.0 } else { (total_compressed_size as f64) / (total_uncompressed_size as f64) }
            } else {
                0.0
            },
            files,
        })
    }

    /// List files in a ZIP archive
    #[cfg(feature = "zip")]
    pub fn list_zip<P: AsRef<Path>>(path: P) -> Result<Vec<String>> {
        let file = File::open(&path)
            .map_err(|e| Error::validation(format!("Failed to open ZIP file: {e}")))?;
        
        let mut archive = ZipArchive::new(file)
            .map_err(|e| Error::validation(format!("Failed to read ZIP archive: {e}")))?;

        let mut files = Vec::new();
        for i in 0..archive.len() {
            let file = archive.by_index(i)
                .map_err(|e| Error::validation(format!("Failed to read ZIP entry {i}: {e}")))?;
            files.push(file.name().to_string());
        }

        Ok(files)
    }

    /// Extract a specific file from a ZIP archive
    #[cfg(feature = "zip")]
    pub fn extract_file_from_zip<P: AsRef<Path>, Q: AsRef<Path>>(
        zip_path: P,
        file_name: &str,
        destination: Q,
    ) -> Result<()> {
        let file = File::open(&zip_path)
            .map_err(|e| Error::validation(format!("Failed to open ZIP file: {e}")))?;
        
        let mut archive = ZipArchive::new(file)
            .map_err(|e| Error::validation(format!("Failed to read ZIP archive: {e}")))?;

        let mut zip_file = archive.by_name(file_name)
            .map_err(|e| Error::not_found(format!("File '{file_name}' not found in ZIP: {e}")))?;

        let mut output_file = File::create(destination)
            .map_err(|e| Error::validation(format!("Failed to create output file: {e}")))?;

        std::io::copy(&mut zip_file, &mut output_file)
            .map_err(|e| Error::validation(format!("Failed to extract file: {e}")))?;

        Ok(())
    }

    /// Calculate compression ratio for a file
    pub fn calculate_compression_ratio(original_size: u64, compressed_size: u64) -> f64 {
        if original_size > 0 {
            if original_size == 0 { 0.0 } else { (compressed_size as f64) / (original_size as f64) }
        } else {
            0.0
        }
    }

    /// Estimate compression ratio for different formats
    pub fn estimate_compression_ratio(file_type: &str) -> f64 {
        match file_type.to_lowercase().as_str() {
            "txt" | "csv" | "json" | "xml" | "html" | "css" | "js" => 0.2, // Text files compress very well
            "log" => 0.1, // Log files often have repetitive content
            "sql" => 0.3,
            "pdf" => 0.9, // PDFs are already compressed
            "jpg" | "jpeg" | "png" | "gif" | "webp" => 0.95, // Images are already compressed
            "mp3" | "mp4" | "avi" | "mkv" => 0.98, // Media files are already compressed
            "zip" | "rar" | "7z" | "gz" | "bz2" => 0.99, // Already compressed archives
            "exe" | "dll" | "so" => 0.7, // Binary files
            "doc" | "docx" | "xls" | "xlsx" | "ppt" | "pptx" => 0.6, // Office documents
            _ => 0.5, // Default estimate
        }
    }
}

/// Information about a ZIP archive
#[derive(Debug, Clone)]
pub struct ZipInfo {
    /// Number of files in the archive
    pub file_count: usize,
    /// Total compressed size
    pub total_compressed_size: u64,
    /// Total uncompressed size
    pub total_uncompressed_size: u64,
    /// Overall compression ratio
    pub compression_ratio: f64,
    /// List of files in the archive
    pub files: Vec<ZipFileInfo>,
}

/// Information about a file in a ZIP archive
#[derive(Debug, Clone)]
pub struct ZipFileInfo {
    /// File name
    pub name: String,
    /// Compressed size
    pub compressed_size: u64,
    /// Uncompressed size
    pub uncompressed_size: u64,
    /// Whether this is a directory
    pub is_directory: bool,
    /// Compression method used
    pub compression_method: String,
}

impl std::fmt::Display for ZipInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "ZIP Archive Information:")?;
        writeln!(f, "  Files: {}", self.file_count)?;
        writeln!(f, "  Compressed size: {} bytes", self.total_compressed_size)?;
        writeln!(f, "  Uncompressed size: {} bytes", self.total_uncompressed_size)?;
        writeln!(f, "  Compression ratio: {:.2}", self.compression_ratio)?;
        writeln!(f, "  Space saved: {:.1}%", (1.0 - self.compression_ratio) * 100.0)?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_compression_format_extension() {
        assert_eq!(CompressionFormat::Zip.extension(), "zip");
        assert_eq!(CompressionFormat::Gzip.extension(), "gz");
        assert_eq!(CompressionFormat::Tar.extension(), "tar");
        assert_eq!(CompressionFormat::TarGz.extension(), "tar.gz");
    }

    #[test]
    fn test_compression_format_from_extension() {
        assert_eq!(CompressionFormat::from_extension("zip"), Some(CompressionFormat::Zip));
        assert_eq!(CompressionFormat::from_extension("gz"), Some(CompressionFormat::Gzip));
        assert_eq!(CompressionFormat::from_extension("tar"), Some(CompressionFormat::Tar));
        assert_eq!(CompressionFormat::from_extension("unknown"), None);
    }

    #[test]
    fn test_compression_format_from_path() {
        assert_eq!(CompressionFormat::from_path("test.zip"), Some(CompressionFormat::Zip));
        assert_eq!(CompressionFormat::from_path("test.tar.gz"), Some(CompressionFormat::TarGz));
        assert_eq!(CompressionFormat::from_path("test.tgz"), Some(CompressionFormat::TarGz));
        assert_eq!(CompressionFormat::from_path("test.txt"), None);
    }

    #[test]
    fn test_compression_stats() {
        let stats = CompressionStats::new(1000, 600, 5);
        assert_eq!(stats.original_size, 1000);
        assert_eq!(stats.compressed_size, 600);
        assert_eq!(stats.compression_ratio, 0.6);
        assert_eq!(stats.space_saved, 400);
        assert_eq!(stats.space_saved_percentage, 40.0);
        assert_eq!(stats.file_count, 5);
    }

    #[test]
    fn test_compression_stats_display() {
        let stats = CompressionStats::new(1000, 600, 3);
        let display = stats.to_string();
        assert!(display.contains("Files processed: 3"));
        assert!(display.contains("Original size: 1000 bytes"));
        assert!(display.contains("Compressed size: 600 bytes"));
        assert!(display.contains("40.0%"));
    }

    #[test]
    fn test_calculate_compression_ratio() {
        assert_eq!(CompressionUtil::calculate_compression_ratio(1000, 600), 0.6);
        assert_eq!(CompressionUtil::calculate_compression_ratio(0, 100), 0.0);
        assert_eq!(CompressionUtil::calculate_compression_ratio(100, 0), 0.0);
    }

    #[test]
    fn test_estimate_compression_ratio() {
        assert!(CompressionUtil::estimate_compression_ratio("txt") < 0.5);
        assert!(CompressionUtil::estimate_compression_ratio("json") < 0.5);
        assert!(CompressionUtil::estimate_compression_ratio("jpg") > 0.9);
        assert!(CompressionUtil::estimate_compression_ratio("mp3") > 0.9);
        assert!(CompressionUtil::estimate_compression_ratio("zip") > 0.9);
        assert_eq!(CompressionUtil::estimate_compression_ratio("unknown"), 0.5);
    }

    #[cfg(feature = "flate2")]
    #[test]
    fn test_gzip_compression() {
        let data = b"Hello, World! This is a test string for compression.".repeat(100);
        
        let compressed = CompressionUtil::compress_gzip(&data).unwrap();
        assert!(compressed.len() < data.len());
        
        let decompressed = CompressionUtil::decompress_gzip(&compressed).unwrap();
        assert_eq!(decompressed, data);
    }

    // Note: ZIP tests would require creating temporary files and directories
    // These are more complex integration tests that would be better suited
    // for a separate test module with proper setup and teardown
}