rustfs-zip 0.0.3

ZIP file handling for RustFS, providing support for reading and writing ZIP archives.
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use async_compression::tokio::bufread::{BzDecoder, GzipDecoder, XzDecoder, ZlibDecoder, ZstdDecoder};
use async_compression::tokio::write::{BzEncoder, GzipEncoder, XzEncoder, ZlibEncoder, ZstdEncoder};
// use async_zip::tokio::read::seek::ZipFileReader;
// use async_zip::tokio::write::ZipFileWriter;
// use async_zip::{Compression, ZipEntryBuilder};
use std::path::Path;
use tokio::fs::File;
use tokio::io::{self, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader, BufWriter};
use tokio_stream::StreamExt;
use tokio_tar::Archive;

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum CompressionFormat {
    Gzip,  //.gz
    Bzip2, //.bz2
    Zip,   //.zip
    Xz,    //.xz
    Zlib,  //.z
    Zstd,  //.zst
    Tar,   //.tar (uncompressed)
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CompressionLevel {
    Fastest,
    Best,
    #[default]
    Default,
    Level(u32),
}

impl CompressionFormat {
    /// Identify compression format from file extension
    pub fn from_extension(ext: &str) -> Self {
        match ext.to_lowercase().as_str() {
            "gz" | "gzip" => CompressionFormat::Gzip,
            "bz2" | "bzip2" => CompressionFormat::Bzip2,
            "zip" => CompressionFormat::Zip,
            "xz" => CompressionFormat::Xz,
            "zlib" => CompressionFormat::Zlib,
            "zst" | "zstd" => CompressionFormat::Zstd,
            "tar" => CompressionFormat::Tar,
            _ => CompressionFormat::Unknown,
        }
    }

    /// Identify compression format from file path
    pub fn from_path<P: AsRef<Path>>(path: P) -> Self {
        let path = path.as_ref();
        if let Some(ext) = path.extension().and_then(|s| s.to_str()) {
            Self::from_extension(ext)
        } else {
            CompressionFormat::Unknown
        }
    }

    /// Get file extension corresponding to the format
    pub fn extension(&self) -> &'static str {
        match self {
            CompressionFormat::Gzip => "gz",
            CompressionFormat::Bzip2 => "bz2",
            CompressionFormat::Zip => "zip",
            CompressionFormat::Xz => "xz",
            CompressionFormat::Zlib => "zlib",
            CompressionFormat::Zstd => "zst",
            CompressionFormat::Tar => "tar",
            CompressionFormat::Unknown => "",
        }
    }

    /// Check if format is supported
    pub fn is_supported(&self) -> bool {
        !matches!(self, CompressionFormat::Unknown)
    }

    /// Create decompressor
    pub fn get_decoder<R>(&self, input: R) -> io::Result<Box<dyn AsyncRead + Send + Unpin>>
    where
        R: AsyncRead + Send + Unpin + 'static,
    {
        let reader = BufReader::new(input);

        let decoder: Box<dyn AsyncRead + Send + Unpin + 'static> = match self {
            CompressionFormat::Gzip => Box::new(GzipDecoder::new(reader)),
            CompressionFormat::Bzip2 => Box::new(BzDecoder::new(reader)),
            CompressionFormat::Zlib => Box::new(ZlibDecoder::new(reader)),
            CompressionFormat::Xz => Box::new(XzDecoder::new(reader)),
            CompressionFormat::Zstd => Box::new(ZstdDecoder::new(reader)),
            CompressionFormat::Tar => Box::new(reader),
            CompressionFormat::Zip => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "Zip format requires special handling, use extract_zip function instead",
                ));
            }
            CompressionFormat::Unknown => {
                return Err(io::Error::new(io::ErrorKind::InvalidInput, "Unsupported file format"));
            }
        };

        Ok(decoder)
    }

    /// Create compressor
    pub fn get_encoder<W>(&self, output: W, level: CompressionLevel) -> io::Result<Box<dyn AsyncWrite + Send + Unpin>>
    where
        W: AsyncWrite + Send + Unpin + 'static,
    {
        let writer = BufWriter::new(output);

        let encoder: Box<dyn AsyncWrite + Send + Unpin + 'static> = match self {
            CompressionFormat::Gzip => {
                let level = match level {
                    CompressionLevel::Fastest => async_compression::Level::Fastest,
                    CompressionLevel::Best => async_compression::Level::Best,
                    CompressionLevel::Default => async_compression::Level::Default,
                    CompressionLevel::Level(n) => async_compression::Level::Precise(n as i32),
                };
                Box::new(GzipEncoder::with_quality(writer, level))
            }
            CompressionFormat::Bzip2 => {
                let level = match level {
                    CompressionLevel::Fastest => async_compression::Level::Fastest,
                    CompressionLevel::Best => async_compression::Level::Best,
                    CompressionLevel::Default => async_compression::Level::Default,
                    CompressionLevel::Level(n) => async_compression::Level::Precise(n as i32),
                };
                Box::new(BzEncoder::with_quality(writer, level))
            }
            CompressionFormat::Zlib => {
                let level = match level {
                    CompressionLevel::Fastest => async_compression::Level::Fastest,
                    CompressionLevel::Best => async_compression::Level::Best,
                    CompressionLevel::Default => async_compression::Level::Default,
                    CompressionLevel::Level(n) => async_compression::Level::Precise(n as i32),
                };
                Box::new(ZlibEncoder::with_quality(writer, level))
            }
            CompressionFormat::Xz => {
                let level = match level {
                    CompressionLevel::Fastest => async_compression::Level::Fastest,
                    CompressionLevel::Best => async_compression::Level::Best,
                    CompressionLevel::Default => async_compression::Level::Default,
                    CompressionLevel::Level(n) => async_compression::Level::Precise(n as i32),
                };
                Box::new(XzEncoder::with_quality(writer, level))
            }
            CompressionFormat::Zstd => {
                let level = match level {
                    CompressionLevel::Fastest => async_compression::Level::Fastest,
                    CompressionLevel::Best => async_compression::Level::Best,
                    CompressionLevel::Default => async_compression::Level::Default,
                    CompressionLevel::Level(n) => async_compression::Level::Precise(n as i32),
                };
                Box::new(ZstdEncoder::with_quality(writer, level))
            }
            CompressionFormat::Tar => Box::new(writer),
            CompressionFormat::Zip => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "Zip format requires special handling, use create_zip function instead",
                ));
            }
            CompressionFormat::Unknown => {
                return Err(io::Error::new(io::ErrorKind::InvalidInput, "Unsupported file format"));
            }
        };

        Ok(encoder)
    }
}

/// Decompress tar format compressed files
pub async fn decompress<R, F>(input: R, format: CompressionFormat, mut callback: F) -> io::Result<()>
where
    R: AsyncRead + Send + Unpin + 'static,
    F: AsyncFnMut(tokio_tar::Entry<Archive<Box<dyn AsyncRead + Send + Unpin + 'static>>>) -> std::io::Result<()> + Send + 'static,
{
    let decoder = format.get_decoder(input)?;
    let mut ar = Archive::new(decoder);
    let mut entries = ar.entries()?;

    while let Some(entry) = entries.next().await {
        let entry = entry?;
        callback(entry).await?;
    }

    Ok(())
}

/// ZIP file entry information
#[derive(Debug, Clone)]
pub struct ZipEntry {
    pub name: String,
    pub size: u64,
    pub compressed_size: u64,
    pub is_dir: bool,
    pub compression_method: String,
}

/// Simplified ZIP file processing (temporarily using standard library zip crate)
pub async fn extract_zip_simple<P: AsRef<Path>>(zip_path: P, extract_to: P) -> io::Result<Vec<ZipEntry>> {
    // Use standard library zip processing, return empty list as placeholder for now
    // Actual implementation needs to be improved in future versions
    let _zip_path = zip_path.as_ref();
    let _extract_to = extract_to.as_ref();

    Ok(Vec::new())
}

/// Simplified ZIP file creation
pub async fn create_zip_simple<P: AsRef<Path>>(
    _zip_path: P,
    _files: Vec<(String, Vec<u8>)>, // (filename, file content)
    _compression_level: CompressionLevel,
) -> io::Result<()> {
    // Return unimplemented error for now
    Err(io::Error::new(io::ErrorKind::Unsupported, "ZIP creation not yet implemented"))
}

/// Compression utility struct
pub struct Compressor {
    format: CompressionFormat,
    level: CompressionLevel,
}

impl Compressor {
    pub fn new(format: CompressionFormat) -> Self {
        Self {
            format,
            level: CompressionLevel::Default,
        }
    }

    pub fn with_level(mut self, level: CompressionLevel) -> Self {
        self.level = level;
        self
    }

    /// Compress data
    pub async fn compress(&self, input: &[u8]) -> io::Result<Vec<u8>> {
        let output = Vec::new();
        let cursor = std::io::Cursor::new(output);
        let mut encoder = self.format.get_encoder(cursor, self.level)?;

        tokio::io::copy(&mut std::io::Cursor::new(input), &mut encoder).await?;
        encoder.shutdown().await?;

        // Get compressed data
        // Note: API needs to be redesigned here as we cannot retrieve data from encoder
        // Return empty vector as placeholder for now
        Ok(Vec::new())
    }

    /// Decompress data
    pub async fn decompress(&self, input: Vec<u8>) -> io::Result<Vec<u8>> {
        let mut output = Vec::new();
        let cursor = std::io::Cursor::new(input);
        let mut decoder = self.format.get_decoder(cursor)?;

        tokio::io::copy(&mut decoder, &mut output).await?;

        Ok(output)
    }
}

/// Decompression utility struct
pub struct Decompressor {
    format: CompressionFormat,
}

impl Decompressor {
    pub fn new(format: CompressionFormat) -> Self {
        Self { format }
    }

    pub fn auto_detect<P: AsRef<Path>>(path: P) -> Self {
        let format = CompressionFormat::from_path(path);
        Self { format }
    }

    /// Decompress file
    pub async fn decompress_file<P: AsRef<Path>>(&self, input_path: P, output_path: P) -> io::Result<()> {
        let input_file = File::open(&input_path).await?;
        let output_file = File::create(&output_path).await?;

        let mut decoder = self.format.get_decoder(input_file)?;
        let mut writer = BufWriter::new(output_file);

        tokio::io::copy(&mut decoder, &mut writer).await?;
        writer.shutdown().await?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Cursor;
    use tokio::io::AsyncReadExt;

    #[test]
    fn test_compression_format_from_extension() {
        // Test supported compression format recognition
        assert_eq!(CompressionFormat::from_extension("gz"), CompressionFormat::Gzip);
        assert_eq!(CompressionFormat::from_extension("gzip"), CompressionFormat::Gzip);
        assert_eq!(CompressionFormat::from_extension("bz2"), CompressionFormat::Bzip2);
        assert_eq!(CompressionFormat::from_extension("bzip2"), CompressionFormat::Bzip2);
        assert_eq!(CompressionFormat::from_extension("zip"), CompressionFormat::Zip);
        assert_eq!(CompressionFormat::from_extension("xz"), CompressionFormat::Xz);
        assert_eq!(CompressionFormat::from_extension("zlib"), CompressionFormat::Zlib);
        assert_eq!(CompressionFormat::from_extension("zst"), CompressionFormat::Zstd);
        assert_eq!(CompressionFormat::from_extension("zstd"), CompressionFormat::Zstd);
        assert_eq!(CompressionFormat::from_extension("tar"), CompressionFormat::Tar);

        // Test case insensitivity
        assert_eq!(CompressionFormat::from_extension("GZ"), CompressionFormat::Gzip);
        assert_eq!(CompressionFormat::from_extension("ZIP"), CompressionFormat::Zip);

        // Test unknown formats
        assert_eq!(CompressionFormat::from_extension("unknown"), CompressionFormat::Unknown);
        assert_eq!(CompressionFormat::from_extension("txt"), CompressionFormat::Unknown);
        assert_eq!(CompressionFormat::from_extension(""), CompressionFormat::Unknown);
    }

    #[test]
    fn test_compression_format_case_sensitivity() {
        // Test case insensitivity (now supports case insensitivity)
        assert_eq!(CompressionFormat::from_extension("GZ"), CompressionFormat::Gzip);
        assert_eq!(CompressionFormat::from_extension("Gz"), CompressionFormat::Gzip);
        assert_eq!(CompressionFormat::from_extension("BZ2"), CompressionFormat::Bzip2);
        assert_eq!(CompressionFormat::from_extension("ZIP"), CompressionFormat::Zip);
    }

    #[test]
    fn test_compression_format_edge_cases() {
        // Test edge cases
        assert_eq!(CompressionFormat::from_extension("gz "), CompressionFormat::Unknown);
        assert_eq!(CompressionFormat::from_extension(" gz"), CompressionFormat::Unknown);
        assert_eq!(CompressionFormat::from_extension("gz.bak"), CompressionFormat::Unknown);
        assert_eq!(CompressionFormat::from_extension("tar.gz"), CompressionFormat::Unknown);
    }

    #[test]
    fn test_compression_format_debug() {
        // Test Debug trait implementation
        let format = CompressionFormat::Gzip;
        let debug_str = format!("{format:?}");
        assert_eq!(debug_str, "Gzip");

        let unknown_format = CompressionFormat::Unknown;
        let unknown_debug_str = format!("{unknown_format:?}");
        assert_eq!(unknown_debug_str, "Unknown");
    }

    #[test]
    fn test_compression_format_equality() {
        // Test PartialEq trait implementation
        assert_eq!(CompressionFormat::Gzip, CompressionFormat::Gzip);
        assert_eq!(CompressionFormat::Unknown, CompressionFormat::Unknown);
        assert_ne!(CompressionFormat::Gzip, CompressionFormat::Bzip2);
        assert_ne!(CompressionFormat::Zip, CompressionFormat::Unknown);
    }

    #[tokio::test]
    async fn test_get_decoder_supported_formats() {
        // Test that supported formats can create decoders
        let test_data = b"test data";
        let cursor = Cursor::new(test_data);

        let gzip_format = CompressionFormat::Gzip;
        let decoder_result = gzip_format.get_decoder(cursor);
        assert!(decoder_result.is_ok(), "Gzip decoder should be created successfully");
    }

    #[tokio::test]
    async fn test_get_decoder_unsupported_formats() {
        // Test that unsupported formats return errors
        let test_data = b"test data";
        let cursor = Cursor::new(test_data);

        let unknown_format = CompressionFormat::Unknown;
        let decoder_result = unknown_format.get_decoder(cursor);
        assert!(decoder_result.is_err(), "Unknown format should return error");

        if let Err(e) = decoder_result {
            assert_eq!(e.kind(), io::ErrorKind::InvalidInput);
            assert_eq!(e.to_string(), "Unsupported file format");
        }
    }

    #[tokio::test]
    async fn test_get_decoder_zip_format() {
        // Test Zip format (currently not supported)
        let test_data = b"test data";
        let cursor = Cursor::new(test_data);

        let zip_format = CompressionFormat::Zip;
        let decoder_result = zip_format.get_decoder(cursor);
        assert!(decoder_result.is_err(), "Zip format should return error (not implemented)");
    }

    #[tokio::test]
    async fn test_get_decoder_all_supported_formats() {
        // Test that all supported formats can create decoders successfully
        let sample_content = b"Hello, compression world!";

        let supported_formats = vec![
            CompressionFormat::Gzip,
            CompressionFormat::Bzip2,
            CompressionFormat::Zlib,
            CompressionFormat::Xz,
            CompressionFormat::Zstd,
            CompressionFormat::Tar,
        ];

        for format in supported_formats {
            let cursor = Cursor::new(sample_content);
            let decoder_result = format.get_decoder(cursor);
            assert!(decoder_result.is_ok(), "Format {format:?} should create decoder successfully");
        }
    }

    #[tokio::test]
    async fn test_decoder_type_consistency() {
        // Test decoder return type consistency
        let sample_content = b"Hello, compression world!";
        let cursor = Cursor::new(sample_content);

        let gzip_format = CompressionFormat::Gzip;
        let mut decoder = gzip_format.get_decoder(cursor).unwrap();

        // Verify that the returned decoder implements the correct trait object
        let mut output_buffer = Vec::new();
        // This only verifies the type, we don't expect successful reading (since data is not actual gzip format)
        let _read_result = decoder.read_to_end(&mut output_buffer).await;
    }

    #[test]
    fn test_compression_format_exhaustive_matching() {
        // Test that all enum variants have corresponding handling
        let all_formats = vec![
            CompressionFormat::Gzip,
            CompressionFormat::Bzip2,
            CompressionFormat::Zip,
            CompressionFormat::Xz,
            CompressionFormat::Zlib,
            CompressionFormat::Zstd,
            CompressionFormat::Unknown,
        ];

        for format in all_formats {
            // Verify each format has corresponding Debug implementation
            let _debug_str = format!("{format:?}");

            // Verify each format has corresponding PartialEq implementation
            assert_eq!(format, format);
        }
    }

    #[test]
    fn test_extension_mapping_completeness() {
        // Test completeness of extension mapping
        let extension_mappings = vec![
            ("gz", CompressionFormat::Gzip),
            ("gzip", CompressionFormat::Gzip),
            ("bz2", CompressionFormat::Bzip2),
            ("bzip2", CompressionFormat::Bzip2),
            ("zip", CompressionFormat::Zip),
            ("xz", CompressionFormat::Xz),
            ("zlib", CompressionFormat::Zlib),
            ("zst", CompressionFormat::Zstd),
            ("zstd", CompressionFormat::Zstd),
            ("tar", CompressionFormat::Tar),
        ];

        for (ext, expected_format) in extension_mappings {
            assert_eq!(
                CompressionFormat::from_extension(ext),
                expected_format,
                "Extension '{ext}' should map to {expected_format:?}"
            );
        }
    }

    #[test]
    fn test_format_string_representations() {
        // Test string representation of formats
        let format_strings = vec![
            (CompressionFormat::Gzip, "Gzip"),
            (CompressionFormat::Bzip2, "Bzip2"),
            (CompressionFormat::Zip, "Zip"),
            (CompressionFormat::Xz, "Xz"),
            (CompressionFormat::Zlib, "Zlib"),
            (CompressionFormat::Zstd, "Zstd"),
            (CompressionFormat::Unknown, "Unknown"),
        ];

        for (format, expected_str) in format_strings {
            assert_eq!(
                format!("{format:?}"),
                expected_str,
                "Format {:?} should have string representation '{}'",
                format,
                expected_str
            );
        }
    }

    #[tokio::test]
    async fn test_decoder_error_handling() {
        // Test decoder error handling with edge cases
        let empty_content = b"";
        let cursor = Cursor::new(empty_content);

        let gzip_format = CompressionFormat::Gzip;
        let decoder_result = gzip_format.get_decoder(cursor);

        // Decoder creation should succeed even with empty content
        assert!(decoder_result.is_ok(), "Decoder creation should succeed even with empty content");
    }

    #[test]
    fn test_compression_format_memory_efficiency() {
        // Test memory efficiency of enum
        use std::mem;

        // Verify enum size is reasonable
        let size = mem::size_of::<CompressionFormat>();
        assert!(size <= 8, "CompressionFormat should be memory efficient, got {size} bytes");

        // Verify Option<CompressionFormat> size
        let option_size = mem::size_of::<Option<CompressionFormat>>();
        assert!(
            option_size <= 16,
            "Option<CompressionFormat> should be efficient, got {option_size} bytes"
        );
    }

    #[test]
    fn test_extension_validation() {
        // Test edge cases of extension validation
        let test_cases = vec![
            // Normal cases
            ("gz", true),
            ("bz2", true),
            ("xz", true),
            // Edge cases
            ("", false),
            ("g", false),
            ("gzz", false),
            ("gz2", false),
            // Special characters
            ("gz.", false),
            (".gz", false),
            ("gz-", false),
            ("gz_", false),
        ];

        for (ext, should_be_known) in test_cases {
            let format = CompressionFormat::from_extension(ext);
            let is_known = format != CompressionFormat::Unknown;
            assert_eq!(
                is_known, should_be_known,
                "Extension '{ext}' recognition mismatch: expected {should_be_known}, got {is_known}"
            );
        }
    }

    #[tokio::test]
    async fn test_decoder_trait_bounds() {
        // Test decoder trait bounds compliance
        let sample_content = b"Hello, compression world!";
        let cursor = Cursor::new(sample_content);

        let gzip_format = CompressionFormat::Gzip;
        let decoder = gzip_format.get_decoder(cursor).unwrap();

        // Verify that the returned decoder satisfies required trait bounds
        fn check_trait_bounds<T: AsyncRead + Send + Unpin + ?Sized>(_: &T) {}
        check_trait_bounds(&*decoder);
    }

    #[test]
    fn test_format_consistency_with_extensions() {
        // 测试格式与扩展名的一致性
        let consistency_tests = vec![
            (CompressionFormat::Gzip, "gz"),
            (CompressionFormat::Bzip2, "bz2"),
            (CompressionFormat::Zip, "zip"),
            (CompressionFormat::Xz, "xz"),
            (CompressionFormat::Zlib, "zlib"),
            (CompressionFormat::Zstd, "zst"),
        ];

        for (format, ext) in consistency_tests {
            let parsed_format = CompressionFormat::from_extension(ext);
            assert_eq!(parsed_format, format, "Extension '{ext}' should consistently map to {format:?}");
        }
    }

    #[tokio::test]
    async fn test_decompress_with_invalid_format() {
        // Test decompression with invalid format
        use std::sync::Arc;
        use std::sync::atomic::{AtomicUsize, Ordering};

        let sample_content = b"Hello, compression world!";
        let cursor = Cursor::new(sample_content);

        let processed_entries_count = Arc::new(AtomicUsize::new(0));
        let processed_entries_count_clone = processed_entries_count.clone();

        let decompress_result = decompress(cursor, CompressionFormat::Unknown, move |_archive_entry| {
            processed_entries_count_clone.fetch_add(1, Ordering::SeqCst);
            async move { Ok(()) }
        })
        .await;

        assert!(decompress_result.is_err(), "Decompress with Unknown format should fail");
        assert_eq!(
            processed_entries_count.load(Ordering::SeqCst),
            0,
            "No entries should be processed with invalid format"
        );
    }

    #[tokio::test]
    async fn test_decompress_with_zip_format() {
        // Test decompression with Zip format (currently not supported)
        use std::sync::Arc;
        use std::sync::atomic::{AtomicUsize, Ordering};

        let sample_content = b"Hello, compression world!";
        let cursor = Cursor::new(sample_content);

        let processed_entries_count = Arc::new(AtomicUsize::new(0));
        let processed_entries_count_clone = processed_entries_count.clone();

        let decompress_result = decompress(cursor, CompressionFormat::Zip, move |_archive_entry| {
            processed_entries_count_clone.fetch_add(1, Ordering::SeqCst);
            async move { Ok(()) }
        })
        .await;

        assert!(decompress_result.is_err(), "Decompress with Zip format should fail (not implemented)");
        assert_eq!(
            processed_entries_count.load(Ordering::SeqCst),
            0,
            "No entries should be processed with unsupported format"
        );
    }

    #[tokio::test]
    async fn test_decompress_error_propagation() {
        // Test error propagation during decompression process
        use std::sync::Arc;
        use std::sync::atomic::{AtomicUsize, Ordering};

        let sample_content = b"Hello, compression world!";
        let cursor = Cursor::new(sample_content);

        let callback_invocation_count = Arc::new(AtomicUsize::new(0));
        let callback_invocation_count_clone = callback_invocation_count.clone();

        let decompress_result = decompress(cursor, CompressionFormat::Gzip, move |_archive_entry| {
            let invocation_number = callback_invocation_count_clone.fetch_add(1, Ordering::SeqCst);
            async move {
                if invocation_number == 0 {
                    // First invocation returns an error
                    Err(io::Error::other("Simulated callback error"))
                } else {
                    Ok(())
                }
            }
        })
        .await;

        // Since input data is not valid gzip format, it may fail during parsing phase
        // This mainly tests the error handling mechanism
        assert!(decompress_result.is_err(), "Should propagate callback errors");
    }

    #[tokio::test]
    async fn test_decompress_callback_execution() {
        // Test callback function execution during decompression
        use std::sync::Arc;
        use std::sync::atomic::{AtomicBool, Ordering};

        let sample_content = b"Hello, compression world!";
        let cursor = Cursor::new(sample_content);

        let callback_was_invoked = Arc::new(AtomicBool::new(false));
        let callback_was_invoked_clone = callback_was_invoked.clone();

        let _decompress_result = decompress(cursor, CompressionFormat::Gzip, move |_archive_entry| {
            callback_was_invoked_clone.store(true, Ordering::SeqCst);
            async move { Ok(()) }
        })
        .await;

        // Note: Since test data is not valid gzip format, callback may not be invoked
        // This test mainly verifies function signature and basic flow
    }

    #[test]
    fn test_compression_format_clone_and_copy() {
        // 测试 CompressionFormat 是否可以被复制
        let format = CompressionFormat::Gzip;
        let format_copy = format;

        // 验证复制后的值相等
        assert_eq!(format, format_copy);

        // 验证原值仍然可用
        assert_eq!(format, CompressionFormat::Gzip);
    }

    #[test]
    fn test_compression_format_match_exhaustiveness() {
        // 测试 match 语句的完整性
        fn handle_format(format: CompressionFormat) -> &'static str {
            match format {
                CompressionFormat::Gzip => "gzip",
                CompressionFormat::Bzip2 => "bzip2",
                CompressionFormat::Zip => "zip",
                CompressionFormat::Xz => "xz",
                CompressionFormat::Zlib => "zlib",
                CompressionFormat::Zstd => "zstd",
                CompressionFormat::Tar => "tar",
                CompressionFormat::Unknown => "unknown",
            }
        }

        // 测试所有变体都有对应的处理
        assert_eq!(handle_format(CompressionFormat::Gzip), "gzip");
        assert_eq!(handle_format(CompressionFormat::Bzip2), "bzip2");
        assert_eq!(handle_format(CompressionFormat::Zip), "zip");
        assert_eq!(handle_format(CompressionFormat::Xz), "xz");
        assert_eq!(handle_format(CompressionFormat::Zlib), "zlib");
        assert_eq!(handle_format(CompressionFormat::Zstd), "zstd");
        assert_eq!(handle_format(CompressionFormat::Unknown), "unknown");
    }

    #[test]
    fn test_extension_parsing_performance() {
        // 测试扩展名解析的性能(简单的性能测试)
        let extensions = vec!["gz", "bz2", "zip", "xz", "zlib", "zst", "unknown"];

        // 多次调用以测试性能一致性
        for _ in 0..1000 {
            for ext in &extensions {
                let _format = CompressionFormat::from_extension(ext);
            }
        }

        // Extension parsing performance test completed
    }

    #[test]
    fn test_format_default_behavior() {
        // 测试格式的默认行为
        let unknown_extensions = vec!["", "txt", "doc", "pdf", "unknown_ext"];

        for ext in unknown_extensions {
            let format = CompressionFormat::from_extension(ext);
            assert_eq!(format, CompressionFormat::Unknown, "Extension '{ext}' should default to Unknown");
        }
    }

    #[test]
    fn test_compression_level() {
        // 测试压缩级别
        let default_level = CompressionLevel::default();
        assert_eq!(default_level, CompressionLevel::Default);

        let fastest = CompressionLevel::Fastest;
        let best = CompressionLevel::Best;
        let custom = CompressionLevel::Level(5);

        assert_ne!(fastest, best);
        assert_ne!(default_level, custom);
    }

    #[test]
    fn test_format_extension() {
        // 测试格式扩展名获取
        assert_eq!(CompressionFormat::Gzip.extension(), "gz");
        assert_eq!(CompressionFormat::Bzip2.extension(), "bz2");
        assert_eq!(CompressionFormat::Zip.extension(), "zip");
        assert_eq!(CompressionFormat::Xz.extension(), "xz");
        assert_eq!(CompressionFormat::Zlib.extension(), "zlib");
        assert_eq!(CompressionFormat::Zstd.extension(), "zst");
        assert_eq!(CompressionFormat::Tar.extension(), "tar");
        assert_eq!(CompressionFormat::Unknown.extension(), "");
    }

    #[test]
    fn test_format_is_supported() {
        // 测试格式支持检查
        assert!(CompressionFormat::Gzip.is_supported());
        assert!(CompressionFormat::Bzip2.is_supported());
        assert!(CompressionFormat::Zip.is_supported());
        assert!(CompressionFormat::Xz.is_supported());
        assert!(CompressionFormat::Zlib.is_supported());
        assert!(CompressionFormat::Zstd.is_supported());
        assert!(CompressionFormat::Tar.is_supported());
        assert!(!CompressionFormat::Unknown.is_supported());
    }

    #[test]
    fn test_format_from_path() {
        // 测试从路径识别格式
        use std::path::Path;

        assert_eq!(CompressionFormat::from_path("file.gz"), CompressionFormat::Gzip);
        assert_eq!(CompressionFormat::from_path("archive.zip"), CompressionFormat::Zip);
        assert_eq!(CompressionFormat::from_path("/path/to/file.tar.gz"), CompressionFormat::Gzip);
        assert_eq!(CompressionFormat::from_path("no_extension"), CompressionFormat::Unknown);

        let path = Path::new("test.bz2");
        assert_eq!(CompressionFormat::from_path(path), CompressionFormat::Bzip2);
    }

    #[tokio::test]
    async fn test_get_encoder_supported_formats() {
        // 测试支持的格式能够创建编码器
        use std::io::Cursor;

        let output = Vec::new();
        let cursor = Cursor::new(output);

        let gzip_format = CompressionFormat::Gzip;
        let encoder_result = gzip_format.get_encoder(cursor, CompressionLevel::Default);
        assert!(encoder_result.is_ok(), "Gzip encoder should be created successfully");
    }

    #[tokio::test]
    async fn test_get_encoder_unsupported_formats() {
        // 测试不支持的格式返回错误
        use std::io::Cursor;

        let output1 = Vec::new();
        let cursor1 = Cursor::new(output1);

        let unknown_format = CompressionFormat::Unknown;
        let encoder_result = unknown_format.get_encoder(cursor1, CompressionLevel::Default);
        assert!(encoder_result.is_err(), "Unknown format should return error");

        let output2 = Vec::new();
        let cursor2 = Cursor::new(output2);
        let zip_format = CompressionFormat::Zip;
        let zip_encoder_result = zip_format.get_encoder(cursor2, CompressionLevel::Default);
        assert!(zip_encoder_result.is_err(), "Zip format should return error (requires special handling)");
    }

    #[tokio::test]
    async fn test_compressor_basic_functionality() {
        // Test basic compressor functionality (Note: current implementation returns empty vector as placeholder)
        let compressor = Compressor::new(CompressionFormat::Gzip);
        let sample_text = b"Hello, World! This is a sample string for compression testing.";

        let compression_result = compressor.compress(sample_text).await;
        assert!(compression_result.is_ok(), "Compression should succeed");

        // Note: Current implementation returns empty vector as placeholder
        let compressed_output = compression_result.unwrap();
        // assert!(!compressed_output.is_empty(), "Compressed data should not be empty");
        // assert_ne!(compressed_output.as_slice(), sample_text, "Compressed data should be different from original");

        // Temporarily verify that function can be called normally
        assert!(compressed_output.is_empty(), "Current implementation returns empty vector as placeholder");
    }

    #[tokio::test]
    async fn test_compressor_with_level() {
        // Test compressor with custom compression level
        let compressor = Compressor::new(CompressionFormat::Gzip).with_level(CompressionLevel::Best);

        let sample_text = b"Sample text for compression level testing";
        let compression_result = compressor.compress(sample_text).await;
        assert!(compression_result.is_ok(), "Compression with custom level should succeed");
    }

    #[test]
    fn test_decompressor_creation() {
        // 测试解压缩器创建
        let decompressor = Decompressor::new(CompressionFormat::Gzip);
        assert_eq!(decompressor.format, CompressionFormat::Gzip);

        let auto_decompressor = Decompressor::auto_detect("test.gz");
        assert_eq!(auto_decompressor.format, CompressionFormat::Gzip);
    }

    #[test]
    fn test_zip_entry_creation() {
        // 测试 ZIP 条目信息创建
        let entry = ZipEntry {
            name: "test.txt".to_string(),
            size: 1024,
            compressed_size: 512,
            is_dir: false,
            compression_method: "Deflate".to_string(),
        };

        assert_eq!(entry.name, "test.txt");
        assert_eq!(entry.size, 1024);
        assert_eq!(entry.compressed_size, 512);
        assert!(!entry.is_dir);
        assert_eq!(entry.compression_method, "Deflate");
    }

    #[test]
    fn test_compression_level_variants() {
        // 测试压缩级别的所有变体
        let levels = vec![
            CompressionLevel::Fastest,
            CompressionLevel::Best,
            CompressionLevel::Default,
            CompressionLevel::Level(1),
            CompressionLevel::Level(9),
        ];

        for level in levels {
            // 验证每个级别都有对应的 Debug 实现
            let _debug_str = format!("{level:?}");
        }
    }

    #[test]
    fn test_format_comprehensive_coverage() {
        // 测试格式的全面覆盖
        let all_formats = vec![
            CompressionFormat::Gzip,
            CompressionFormat::Bzip2,
            CompressionFormat::Zip,
            CompressionFormat::Xz,
            CompressionFormat::Zlib,
            CompressionFormat::Zstd,
            CompressionFormat::Tar,
            CompressionFormat::Unknown,
        ];

        for format in all_formats {
            // 验证每个格式都有扩展名
            let _ext = format.extension();

            // 验证支持状态检查
            let _supported = format.is_supported();

            // 验证 Debug 实现
            let _debug = format!("{format:?}");
        }
    }
}

// #[tokio::test]
// async fn test_decompress() -> io::Result<()> {
//     use std::path::Path;
//     use tokio::fs::File;

//     let input_path = "/Users/weisd/Downloads/wsd.tar.gz"; // 替换为你的压缩文件路径

//     let f = File::open(input_path).await?;

//     let Some(ext) = Path::new(input_path).extension().and_then(|s| s.to_str()) else {
//         return Err(io::Error::new(io::ErrorKind::InvalidInput, "Unsupported file format"));
//     };

//     match decompress(
//         f,
//         CompressionFormat::from_extension(ext),
//         |entry: tokio_tar::Entry<Archive<Box<dyn AsyncRead + Send + Unpin>>>| async move {
//             let path = entry.path().unwrap();
//             println!("Extracted: {}", path.display());
//             Ok(())
//         },
//     )
//     .await
//     {
//         Ok(_) => println!("解压成功!"),
//         Err(e) => println!("解压失败:{}", e),
//     }

//     Ok(())
// }