exarch_core/creation/
zip.rs

1//! ZIP archive creation.
2//!
3//! This module provides functions for creating ZIP archives with configurable
4//! compression levels and security options.
5
6use crate::ExtractionError;
7use crate::ProgressCallback;
8use crate::Result;
9use crate::creation::config::CreationConfig;
10use crate::creation::filters;
11use crate::creation::report::CreationReport;
12use crate::creation::walker::EntryType;
13use crate::creation::walker::FilteredWalker;
14use crate::creation::walker::collect_entries;
15use std::fs::File;
16use std::io::Read;
17use std::io::Seek;
18use std::io::Write;
19use std::path::Path;
20use zip::CompressionMethod;
21use zip::ZipWriter;
22use zip::write::SimpleFileOptions;
23
24/// Creates a ZIP archive.
25///
26/// # Examples
27///
28/// ```no_run
29/// use exarch_core::creation::CreationConfig;
30/// use exarch_core::creation::zip::create_zip;
31/// use std::path::Path;
32///
33/// let config = CreationConfig::default();
34/// let report = create_zip(Path::new("output.zip"), &[Path::new("src")], &config)?;
35/// println!("Added {} files", report.files_added);
36/// # Ok::<(), exarch_core::ExtractionError>(())
37/// ```
38///
39/// # Errors
40///
41/// Returns an error if:
42/// - Source path does not exist
43/// - Output file cannot be created
44/// - I/O error during archive creation
45#[allow(dead_code)] // Will be used by CLI
46pub fn create_zip<P: AsRef<Path>, Q: AsRef<Path>>(
47    output: P,
48    sources: &[Q],
49    config: &CreationConfig,
50) -> Result<CreationReport> {
51    let file = File::create(output.as_ref())?;
52    create_zip_internal(file, sources, config)
53}
54
55/// Creates a ZIP archive with progress reporting.
56///
57/// This function provides real-time progress updates during archive creation
58/// through callback functions. Useful for displaying progress bars or logging
59/// in interactive applications.
60///
61/// # Parameters
62///
63/// - `output`: Path where the ZIP archive will be created
64/// - `sources`: Slice of source paths to include in the archive
65/// - `config`: Configuration controlling filtering, permissions, compression,
66///   and archiving behavior
67/// - `progress`: Mutable reference to a progress callback implementation
68///
69/// # Progress Callbacks
70///
71/// The `progress` callback receives four types of events:
72///
73/// 1. `on_entry_start`: Called before processing each file/directory
74/// 2. `on_bytes_written`: Called for each chunk of data written (typically
75///    every 64 KB)
76/// 3. `on_entry_complete`: Called after successfully processing an entry
77/// 4. `on_complete`: Called once when the entire archive is finished
78///
79/// Note: Callbacks are invoked frequently during large file processing. For
80/// better performance with very large files, consider batching updates.
81///
82/// # Examples
83///
84/// ```no_run
85/// use exarch_core::ProgressCallback;
86/// use exarch_core::creation::CreationConfig;
87/// use exarch_core::creation::zip::create_zip_with_progress;
88/// use std::path::Path;
89///
90/// struct SimpleProgress;
91///
92/// impl ProgressCallback for SimpleProgress {
93///     fn on_entry_start(&mut self, path: &Path, total: usize, current: usize) {
94///         println!("[{}/{}] Processing: {}", current, total, path.display());
95///     }
96///
97///     fn on_bytes_written(&mut self, bytes: u64) {
98///         // Called frequently - consider rate limiting
99///     }
100///
101///     fn on_entry_complete(&mut self, path: &Path) {
102///         println!("Completed: {}", path.display());
103///     }
104///
105///     fn on_complete(&mut self) {
106///         println!("Archive creation complete!");
107///     }
108/// }
109///
110/// let config = CreationConfig::default();
111/// let mut progress = SimpleProgress;
112/// let report = create_zip_with_progress(
113///     Path::new("output.zip"),
114///     &[Path::new("src")],
115///     &config,
116///     &mut progress,
117/// )?;
118/// # Ok::<(), exarch_core::ExtractionError>(())
119/// ```
120///
121/// # Errors
122///
123/// Returns an error if:
124/// - Source path does not exist
125/// - Output file cannot be created
126/// - I/O error during archive creation
127/// - File metadata cannot be read
128#[allow(dead_code)] // Will be used by CLI
129pub fn create_zip_with_progress<P: AsRef<Path>, Q: AsRef<Path>>(
130    output: P,
131    sources: &[Q],
132    config: &CreationConfig,
133    progress: &mut dyn ProgressCallback,
134) -> Result<CreationReport> {
135    let file = File::create(output.as_ref())?;
136    create_zip_internal_with_progress(file, sources, config, progress)
137}
138
139/// Internal function that creates ZIP with any writer and progress reporting.
140fn create_zip_internal_with_progress<W: Write + Seek, P: AsRef<Path>>(
141    writer: W,
142    sources: &[P],
143    config: &CreationConfig,
144    progress: &mut dyn ProgressCallback,
145) -> Result<CreationReport> {
146    let mut zip = ZipWriter::new(writer);
147    let mut report = CreationReport::default();
148    let start = std::time::Instant::now();
149
150    // Configure ZIP file options with compression level
151    let options = if config.compression_level == Some(0) {
152        SimpleFileOptions::default().compression_method(CompressionMethod::Stored)
153    } else {
154        let level = config.compression_level.unwrap_or(6);
155        SimpleFileOptions::default()
156            .compression_method(CompressionMethod::Deflated)
157            .compression_level(Some(i64::from(level)))
158    };
159
160    // Single-pass collection of entries (avoids double directory traversal)
161    let entries = collect_entries(sources, config)?;
162    let total_entries = entries.len();
163
164    // Reusable buffer for file copying (fixes HIGH #2)
165    let mut buffer = vec![0u8; 64 * 1024]; // 64 KB
166
167    for (idx, entry) in entries.iter().enumerate() {
168        let current_entry = idx + 1;
169
170        match &entry.entry_type {
171            EntryType::File => {
172                progress.on_entry_start(&entry.archive_path, total_entries, current_entry);
173                add_file_to_zip_with_progress_and_buffer(
174                    &mut zip,
175                    &entry.path,
176                    &entry.archive_path,
177                    config,
178                    &mut report,
179                    &options,
180                    progress,
181                    &mut buffer,
182                )?;
183                progress.on_entry_complete(&entry.archive_path);
184            }
185            EntryType::Directory => {
186                progress.on_entry_start(&entry.archive_path, total_entries, current_entry);
187                // Skip root directory entry (empty path becomes "/" which is invalid)
188                if !entry.archive_path.as_os_str().is_empty() {
189                    let dir_path = format!("{}/", normalize_zip_path(&entry.archive_path)?);
190                    zip.add_directory(&dir_path, options).map_err(|e| {
191                        std::io::Error::other(format!("failed to add directory: {e}"))
192                    })?;
193                    report.directories_added += 1;
194                }
195                progress.on_entry_complete(&entry.archive_path);
196            }
197            EntryType::Symlink { .. } => {
198                progress.on_entry_start(&entry.archive_path, total_entries, current_entry);
199                if !config.follow_symlinks {
200                    report.files_skipped += 1;
201                    report.add_warning(format!("Skipped symlink: {}", entry.path.display()));
202                }
203                progress.on_entry_complete(&entry.archive_path);
204            }
205        }
206    }
207
208    // Finish writing ZIP
209    zip.finish()
210        .map_err(|e| std::io::Error::other(format!("failed to finish ZIP archive: {e}")))?;
211
212    report.duration = start.elapsed();
213
214    progress.on_complete();
215
216    Ok(report)
217}
218
219/// Internal function that creates ZIP with any writer.
220///
221/// Handles the core logic of walking sources and adding entries to the archive.
222fn create_zip_internal<W: Write + Seek, P: AsRef<Path>>(
223    writer: W,
224    sources: &[P],
225    config: &CreationConfig,
226) -> Result<CreationReport> {
227    let mut zip = ZipWriter::new(writer);
228    let mut report = CreationReport::default();
229    let start = std::time::Instant::now();
230
231    // Configure ZIP file options with compression level
232    let options = if config.compression_level == Some(0) {
233        SimpleFileOptions::default().compression_method(CompressionMethod::Stored)
234    } else {
235        // Convert compression level (1-9) to zip crate level
236        let level = config.compression_level.unwrap_or(6);
237        SimpleFileOptions::default()
238            .compression_method(CompressionMethod::Deflated)
239            .compression_level(Some(i64::from(level)))
240    };
241
242    for source in sources {
243        let path = source.as_ref();
244
245        // Validate source exists
246        if !path.exists() {
247            return Err(ExtractionError::SourceNotFound {
248                path: path.to_path_buf(),
249            });
250        }
251
252        // Walk directory or add single file
253        if path.is_dir() {
254            add_directory_to_zip(&mut zip, path, config, &mut report, &options)?;
255        } else {
256            // For single files, use filename as archive path
257            let archive_path =
258                filters::compute_archive_path(path, path.parent().unwrap_or(path), config)?;
259            add_file_to_zip(&mut zip, path, &archive_path, config, &mut report, &options)?;
260        }
261    }
262
263    // Finish writing ZIP
264    zip.finish()
265        .map_err(|e| std::io::Error::other(format!("failed to finish ZIP archive: {e}")))?;
266
267    report.duration = start.elapsed();
268
269    Ok(report)
270}
271
272/// Adds a directory tree to the ZIP archive using the walker.
273fn add_directory_to_zip<W: Write + Seek>(
274    zip: &mut ZipWriter<W>,
275    dir: &Path,
276    config: &CreationConfig,
277    report: &mut CreationReport,
278    options: &SimpleFileOptions,
279) -> Result<()> {
280    let walker = FilteredWalker::new(dir, config);
281
282    for entry in walker.walk() {
283        let entry = entry?;
284
285        match entry.entry_type {
286            EntryType::File => {
287                add_file_to_zip(
288                    zip,
289                    &entry.path,
290                    &entry.archive_path,
291                    config,
292                    report,
293                    options,
294                )?;
295            }
296            EntryType::Directory => {
297                // ZIP requires explicit directory entries with trailing /
298                let dir_path = format!("{}/", normalize_zip_path(&entry.archive_path)?);
299                zip.add_directory(&dir_path, *options)
300                    .map_err(|e| std::io::Error::other(format!("failed to add directory: {e}")))?;
301                report.directories_added += 1;
302            }
303            EntryType::Symlink { .. } => {
304                if !config.follow_symlinks {
305                    // ZIP doesn't have native symlink support like TAR
306                    // Skip symlinks when not following them
307                    report.files_skipped += 1;
308                    report.add_warning(format!("Skipped symlink: {}", entry.path.display()));
309                }
310            }
311        }
312    }
313
314    Ok(())
315}
316
317/// Adds a single file to the ZIP archive.
318fn add_file_to_zip<W: Write + Seek>(
319    zip: &mut ZipWriter<W>,
320    file_path: &Path,
321    archive_path: &Path,
322    config: &CreationConfig,
323    report: &mut CreationReport,
324    options: &SimpleFileOptions,
325) -> Result<()> {
326    let mut file = File::open(file_path)?;
327    let metadata = file.metadata()?;
328    let size = metadata.len();
329
330    // Check file size limit
331    if let Some(max_size) = config.max_file_size
332        && size > max_size
333    {
334        report.files_skipped += 1;
335        report.add_warning(format!(
336            "Skipped file (too large): {} ({} bytes)",
337            file_path.display(),
338            size
339        ));
340        return Ok(());
341    }
342
343    // Configure options with permissions if needed
344    let file_options = if config.preserve_permissions {
345        #[cfg(unix)]
346        {
347            use std::os::unix::fs::PermissionsExt;
348            options.unix_permissions(metadata.permissions().mode())
349        }
350        #[cfg(not(unix))]
351        {
352            *options
353        }
354    } else {
355        *options
356    };
357
358    // Convert archive_path to ZIP format (forward slashes)
359    let archive_name = normalize_zip_path(archive_path)?;
360
361    zip.start_file(&archive_name, file_options)
362        .map_err(|e| std::io::Error::other(format!("failed to start file in ZIP: {e}")))?;
363
364    // Copy file contents with 64KB buffer for better throughput
365    let mut buffer = vec![0u8; 64 * 1024]; // 64 KB
366    let mut bytes_written = 0u64;
367    loop {
368        let bytes_read = file.read(&mut buffer)?;
369        if bytes_read == 0 {
370            break;
371        }
372        zip.write_all(&buffer[..bytes_read])?;
373        bytes_written += bytes_read as u64;
374    }
375
376    report.files_added += 1;
377    report.bytes_written += bytes_written;
378
379    Ok(())
380}
381
382/// Adds a single file to the ZIP archive with progress reporting and reusable
383/// buffer.
384#[allow(clippy::too_many_arguments)]
385fn add_file_to_zip_with_progress_and_buffer<W: Write + Seek>(
386    zip: &mut ZipWriter<W>,
387    file_path: &Path,
388    archive_path: &Path,
389    config: &CreationConfig,
390    report: &mut CreationReport,
391    options: &SimpleFileOptions,
392    progress: &mut dyn ProgressCallback,
393    buffer: &mut [u8],
394) -> Result<()> {
395    let mut file = File::open(file_path)?;
396    let metadata = file.metadata()?;
397    let size = metadata.len();
398
399    // Check file size limit
400    if let Some(max_size) = config.max_file_size
401        && size > max_size
402    {
403        report.files_skipped += 1;
404        report.add_warning(format!(
405            "Skipped file (too large): {} ({} bytes)",
406            file_path.display(),
407            size
408        ));
409        return Ok(());
410    }
411
412    // Configure options with permissions if needed
413    let file_options = if config.preserve_permissions {
414        #[cfg(unix)]
415        {
416            use std::os::unix::fs::PermissionsExt;
417            options.unix_permissions(metadata.permissions().mode())
418        }
419        #[cfg(not(unix))]
420        {
421            *options
422        }
423    } else {
424        *options
425    };
426
427    let archive_name = normalize_zip_path(archive_path)?;
428
429    zip.start_file(&archive_name, file_options)
430        .map_err(|e| std::io::Error::other(format!("failed to start file in ZIP: {e}")))?;
431
432    // Copy file contents with progress tracking and reusable buffer
433    let mut bytes_written = 0u64;
434    loop {
435        let bytes_read = file.read(buffer)?;
436        if bytes_read == 0 {
437            break;
438        }
439        zip.write_all(&buffer[..bytes_read])?;
440        bytes_written += bytes_read as u64;
441        progress.on_bytes_written(bytes_read as u64);
442    }
443
444    report.files_added += 1;
445    report.bytes_written += bytes_written;
446
447    Ok(())
448}
449
450/// Normalizes a path for ZIP archive format.
451///
452/// ZIP format requires forward slashes (/) as path separators, regardless
453/// of platform. This function converts platform-specific paths to ZIP format.
454fn normalize_zip_path(path: &Path) -> Result<String> {
455    // Convert to string
456    let path_str = path.to_str().ok_or_else(|| {
457        ExtractionError::Io(std::io::Error::other(format!(
458            "path is not valid UTF-8: {}",
459            path.display()
460        )))
461    })?;
462
463    // Replace backslashes with forward slashes (Windows)
464    #[cfg(windows)]
465    let normalized = path_str.replace('\\', "/");
466
467    #[cfg(not(windows))]
468    let normalized = path_str.to_string();
469
470    Ok(normalized)
471}
472
473#[cfg(test)]
474#[allow(clippy::unwrap_used)] // Allow unwrap in tests for brevity
475mod tests {
476    use super::*;
477    use std::fs;
478    use tempfile::TempDir;
479
480    #[test]
481    fn test_create_zip_single_file() {
482        let temp = TempDir::new().unwrap();
483        let output = temp.path().join("output.zip");
484
485        // Create source file
486        let source_dir = TempDir::new().unwrap();
487        fs::write(source_dir.path().join("test.txt"), "Hello ZIP").unwrap();
488
489        let config = CreationConfig::default()
490            .with_exclude_patterns(vec![])
491            .with_include_hidden(true);
492
493        let report = create_zip(&output, &[source_dir.path().join("test.txt")], &config).unwrap();
494
495        assert_eq!(report.files_added, 1);
496        assert!(report.bytes_written > 0);
497        assert!(output.exists());
498    }
499
500    #[test]
501    fn test_create_zip_directory() {
502        let temp = TempDir::new().unwrap();
503        let output = temp.path().join("output.zip");
504
505        // Create source directory with multiple files
506        let source_dir = TempDir::new().unwrap();
507        fs::write(source_dir.path().join("file1.txt"), "content1").unwrap();
508        fs::write(source_dir.path().join("file2.txt"), "content2").unwrap();
509        fs::create_dir(source_dir.path().join("subdir")).unwrap();
510        fs::write(source_dir.path().join("subdir/file3.txt"), "content3").unwrap();
511
512        let config = CreationConfig::default()
513            .with_exclude_patterns(vec![])
514            .with_include_hidden(true);
515
516        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
517
518        // Should have exactly 3 files: file1.txt, file2.txt, subdir/file3.txt
519        assert_eq!(report.files_added, 3);
520        // Should have exactly 2 directories: root and subdir
521        assert_eq!(report.directories_added, 2);
522        assert!(output.exists());
523    }
524
525    #[test]
526    fn test_create_zip_compression() {
527        let temp = TempDir::new().unwrap();
528        let output = temp.path().join("output.zip");
529
530        // Create source file with repetitive content (compresses well)
531        let source_dir = TempDir::new().unwrap();
532        fs::write(source_dir.path().join("test.txt"), "a".repeat(1000)).unwrap();
533
534        let config = CreationConfig::default()
535            .with_exclude_patterns(vec![])
536            .with_compression_level(9);
537
538        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
539
540        assert_eq!(report.files_added, 1);
541        assert!(output.exists());
542
543        // Verify it's a valid ZIP file (basic check)
544        let data = fs::read(&output).unwrap();
545        assert_eq!(&data[0..4], b"PK\x03\x04"); // ZIP local file header magic
546    }
547
548    #[test]
549    fn test_create_zip_compression_levels() {
550        let temp = TempDir::new().unwrap();
551
552        // Create source with repetitive data (compresses well)
553        let source_dir = TempDir::new().unwrap();
554        fs::write(source_dir.path().join("test.txt"), "a".repeat(10000)).unwrap();
555
556        // Test different compression levels (1-9 are valid)
557        for level in [1, 6, 9] {
558            let output = temp.path().join(format!("output_{level}.zip"));
559            let config = CreationConfig::default()
560                .with_exclude_patterns(vec![])
561                .with_compression_level(level);
562
563            let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
564            assert_eq!(report.files_added, 1);
565            assert!(output.exists());
566        }
567    }
568
569    #[test]
570    fn test_create_zip_explicit_directories() {
571        let temp = TempDir::new().unwrap();
572        let output = temp.path().join("output.zip");
573
574        // Create source directory structure
575        let source_dir = TempDir::new().unwrap();
576        fs::create_dir(source_dir.path().join("dir1")).unwrap();
577        fs::create_dir(source_dir.path().join("dir1/dir2")).unwrap();
578        fs::write(source_dir.path().join("dir1/dir2/file.txt"), "content").unwrap();
579
580        let config = CreationConfig::default()
581            .with_exclude_patterns(vec![])
582            .with_include_hidden(true);
583
584        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
585
586        assert!(report.directories_added >= 2); // dir1 and dir1/dir2
587        assert!(output.exists());
588
589        // Verify directories have trailing slash by reading archive
590        let file = File::open(&output).unwrap();
591        let mut archive = zip::ZipArchive::new(file).unwrap();
592
593        let mut dir_entries = 0;
594        for i in 0..archive.len() {
595            let entry = archive.by_index(i).unwrap();
596            if entry.is_dir() {
597                dir_entries += 1;
598                assert!(
599                    entry.name().ends_with('/'),
600                    "Directory entry should end with /"
601                );
602            }
603        }
604        assert!(dir_entries >= 2, "Expected at least 2 directory entries");
605    }
606
607    #[cfg(unix)]
608    #[test]
609    fn test_create_zip_preserves_permissions() {
610        use std::os::unix::fs::PermissionsExt;
611
612        let temp = TempDir::new().unwrap();
613        let output = temp.path().join("output.zip");
614
615        // Create source file with specific permissions
616        let source_dir = TempDir::new().unwrap();
617        let file_path = source_dir.path().join("test.txt");
618        fs::write(&file_path, "content").unwrap();
619        fs::set_permissions(&file_path, fs::Permissions::from_mode(0o755)).unwrap();
620
621        let config = CreationConfig::default()
622            .with_exclude_patterns(vec![])
623            .with_preserve_permissions(true);
624
625        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
626        assert_eq!(report.files_added, 1);
627
628        // Verify permissions in archive
629        let file = File::open(&output).unwrap();
630        let mut archive = zip::ZipArchive::new(file).unwrap();
631
632        for i in 0..archive.len() {
633            let entry = archive.by_index(i).unwrap();
634            if entry.name().contains("test.txt")
635                && let Some(mode) = entry.unix_mode()
636            {
637                assert_eq!(mode & 0o777, 0o755, "Permissions should be preserved");
638            }
639        }
640    }
641
642    #[test]
643    fn test_create_zip_report_statistics() {
644        let temp = TempDir::new().unwrap();
645        let output = temp.path().join("output.zip");
646
647        // Create source directory with known structure
648        let source_dir = TempDir::new().unwrap();
649        fs::write(source_dir.path().join("file1.txt"), "content1").unwrap();
650        fs::write(source_dir.path().join("file2.txt"), "content2").unwrap();
651        fs::create_dir(source_dir.path().join("subdir")).unwrap();
652        fs::write(source_dir.path().join("subdir/file3.txt"), "content3").unwrap();
653
654        let config = CreationConfig::default()
655            .with_exclude_patterns(vec![])
656            .with_include_hidden(true);
657
658        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
659
660        assert_eq!(report.files_added, 3);
661        assert!(report.directories_added >= 1);
662        assert_eq!(report.files_skipped, 0);
663        assert!(!report.has_warnings());
664        assert!(report.duration.as_nanos() > 0);
665    }
666
667    #[test]
668    fn test_create_zip_roundtrip() {
669        let temp = TempDir::new().unwrap();
670        let output = temp.path().join("output.zip");
671
672        // Create source directory
673        let source_dir = TempDir::new().unwrap();
674        fs::write(source_dir.path().join("file1.txt"), "content1").unwrap();
675        fs::create_dir(source_dir.path().join("subdir")).unwrap();
676        fs::write(source_dir.path().join("subdir/file2.txt"), "content2").unwrap();
677
678        let config = CreationConfig::default()
679            .with_exclude_patterns(vec![])
680            .with_include_hidden(true);
681
682        // Create archive
683        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
684        assert!(report.files_added >= 2);
685
686        // Extract and verify using zip crate
687        let file = File::open(&output).unwrap();
688        let mut archive = zip::ZipArchive::new(file).unwrap();
689
690        let extract_dir = TempDir::new().unwrap();
691
692        for i in 0..archive.len() {
693            let mut entry = archive.by_index(i).unwrap();
694            let outpath = extract_dir.path().join(entry.name());
695
696            if entry.is_dir() {
697                fs::create_dir_all(&outpath).unwrap();
698            } else {
699                if let Some(parent) = outpath.parent() {
700                    fs::create_dir_all(parent).unwrap();
701                }
702                let mut outfile = File::create(&outpath).unwrap();
703                std::io::copy(&mut entry, &mut outfile).unwrap();
704            }
705        }
706
707        // Verify extracted files match originals
708        let extracted1 = fs::read_to_string(extract_dir.path().join("file1.txt")).unwrap();
709        assert_eq!(extracted1, "content1");
710
711        let extracted2 = fs::read_to_string(extract_dir.path().join("subdir/file2.txt")).unwrap();
712        assert_eq!(extracted2, "content2");
713    }
714
715    #[test]
716    fn test_create_zip_forward_slashes() {
717        let temp = TempDir::new().unwrap();
718        let output = temp.path().join("output.zip");
719
720        // Create source directory structure
721        let source_dir = TempDir::new().unwrap();
722        fs::create_dir(source_dir.path().join("dir1")).unwrap();
723        fs::write(source_dir.path().join("dir1/file.txt"), "content").unwrap();
724
725        let config = CreationConfig::default()
726            .with_exclude_patterns(vec![])
727            .with_include_hidden(true);
728
729        create_zip(&output, &[source_dir.path()], &config).unwrap();
730
731        // Verify paths use forward slashes
732        let file = File::open(&output).unwrap();
733        let mut archive = zip::ZipArchive::new(file).unwrap();
734
735        for i in 0..archive.len() {
736            let entry = archive.by_index(i).unwrap();
737            let name = entry.name();
738            // ZIP paths should never contain backslashes
739            assert!(
740                !name.contains('\\'),
741                "ZIP path should use forward slashes: {name}"
742            );
743            // Subdirectory paths should use forward slash
744            if name.contains("dir1") && name.contains("file") {
745                assert!(name.contains("dir1/file"), "Expected forward slash in path");
746            }
747        }
748    }
749
750    #[test]
751    fn test_create_zip_source_not_found() {
752        let temp = TempDir::new().unwrap();
753        let output = temp.path().join("output.zip");
754
755        let config = CreationConfig::default();
756        let result = create_zip(&output, &[Path::new("/nonexistent/path")], &config);
757
758        assert!(result.is_err());
759        assert!(matches!(
760            result.unwrap_err(),
761            ExtractionError::SourceNotFound { .. }
762        ));
763    }
764
765    #[test]
766    fn test_normalize_zip_path() {
767        // Basic path
768        let path = Path::new("dir/file.txt");
769        let normalized = normalize_zip_path(path).unwrap();
770        assert_eq!(normalized, "dir/file.txt");
771
772        // Single file
773        let path = Path::new("file.txt");
774        let normalized = normalize_zip_path(path).unwrap();
775        assert_eq!(normalized, "file.txt");
776
777        // Nested directories
778        let path = Path::new("a/b/c/file.txt");
779        let normalized = normalize_zip_path(path).unwrap();
780        assert_eq!(normalized, "a/b/c/file.txt");
781    }
782
783    #[cfg(windows)]
784    #[test]
785    fn test_normalize_zip_path_windows() {
786        // Windows path with backslashes
787        let path = Path::new("dir\\file.txt");
788        let normalized = normalize_zip_path(path).unwrap();
789        assert_eq!(normalized, "dir/file.txt");
790
791        // Nested with backslashes
792        let path = Path::new("a\\b\\c\\file.txt");
793        let normalized = normalize_zip_path(path).unwrap();
794        assert_eq!(normalized, "a/b/c/file.txt");
795    }
796
797    #[test]
798    fn test_create_zip_max_file_size() {
799        let temp = TempDir::new().unwrap();
800        let output = temp.path().join("output.zip");
801
802        // Create files with different sizes
803        let source_dir = TempDir::new().unwrap();
804        fs::write(source_dir.path().join("small.txt"), "tiny").unwrap(); // 4 bytes
805        fs::write(source_dir.path().join("large.txt"), "a".repeat(1000)).unwrap(); // 1000 bytes
806
807        // Set max file size to 100 bytes
808        let config = CreationConfig::default()
809            .with_exclude_patterns(vec![])
810            .with_max_file_size(Some(100));
811
812        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
813
814        // Walker filters out large.txt, so only small.txt is added
815        // No files are skipped at the ZIP level (walker already filtered)
816        assert_eq!(report.files_added, 1);
817        assert_eq!(report.files_skipped, 0);
818    }
819
820    #[cfg(unix)]
821    #[test]
822    fn test_create_zip_skips_symlinks() {
823        let temp = TempDir::new().unwrap();
824        let output = temp.path().join("output.zip");
825
826        // Create source with symlink
827        let source_dir = TempDir::new().unwrap();
828        fs::write(source_dir.path().join("target.txt"), "content").unwrap();
829        std::os::unix::fs::symlink(
830            source_dir.path().join("target.txt"),
831            source_dir.path().join("link.txt"),
832        )
833        .unwrap();
834
835        // Don't follow symlinks (default)
836        let config = CreationConfig::default()
837            .with_exclude_patterns(vec![])
838            .with_include_hidden(true);
839
840        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
841
842        // Should add target.txt, skip link.txt
843        assert_eq!(report.files_added, 1);
844        assert_eq!(report.files_skipped, 1);
845        assert!(report.has_warnings());
846
847        let warning = &report.warnings[0];
848        assert!(warning.contains("Skipped symlink"));
849    }
850
851    #[test]
852    fn test_create_zip_with_progress_callback() {
853        #[derive(Debug, Default, Clone)]
854        struct TestProgress {
855            entries_started: Vec<String>,
856            entries_completed: Vec<String>,
857            bytes_written: u64,
858            completed: bool,
859        }
860
861        impl ProgressCallback for TestProgress {
862            fn on_entry_start(&mut self, path: &Path, _total: usize, _current: usize) {
863                self.entries_started
864                    .push(path.to_string_lossy().to_string());
865            }
866
867            fn on_bytes_written(&mut self, bytes: u64) {
868                self.bytes_written += bytes;
869            }
870
871            fn on_entry_complete(&mut self, path: &Path) {
872                self.entries_completed
873                    .push(path.to_string_lossy().to_string());
874            }
875
876            fn on_complete(&mut self) {
877                self.completed = true;
878            }
879        }
880
881        let temp = TempDir::new().unwrap();
882        let output = temp.path().join("output.zip");
883
884        // Create source directory with multiple files
885        let source_dir = TempDir::new().unwrap();
886        fs::write(source_dir.path().join("file1.txt"), "content1").unwrap();
887        fs::write(source_dir.path().join("file2.txt"), "content2").unwrap();
888        fs::create_dir(source_dir.path().join("subdir")).unwrap();
889        fs::write(source_dir.path().join("subdir/file3.txt"), "content3").unwrap();
890
891        let config = CreationConfig::default()
892            .with_exclude_patterns(vec![])
893            .with_include_hidden(true);
894
895        let mut progress = TestProgress::default();
896
897        let report =
898            create_zip_with_progress(&output, &[source_dir.path()], &config, &mut progress)
899                .unwrap();
900
901        // Verify report
902        assert_eq!(report.files_added, 3);
903        assert!(report.directories_added >= 1);
904
905        // Verify callbacks were invoked
906        assert!(
907            progress.entries_started.len() >= 3,
908            "Expected at least 3 entry starts, got {}",
909            progress.entries_started.len()
910        );
911        assert!(
912            progress.entries_completed.len() >= 3,
913            "Expected at least 3 entry completions, got {}",
914            progress.entries_completed.len()
915        );
916        assert!(
917            progress.bytes_written > 0,
918            "Expected bytes written > 0, got {}",
919            progress.bytes_written
920        );
921        assert!(progress.completed, "Expected on_complete to be called");
922
923        // Verify specific entries
924        let has_file1 = progress
925            .entries_started
926            .iter()
927            .any(|p| p.contains("file1.txt"));
928        let has_file2 = progress
929            .entries_started
930            .iter()
931            .any(|p| p.contains("file2.txt"));
932        let has_file3 = progress
933            .entries_started
934            .iter()
935            .any(|p| p.contains("file3.txt"));
936
937        assert!(has_file1, "Expected file1.txt in progress callbacks");
938        assert!(has_file2, "Expected file2.txt in progress callbacks");
939        assert!(has_file3, "Expected file3.txt in progress callbacks");
940    }
941}