Skip to main content

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/// Format creator for ZIP archives.
474pub struct ZipCreator;
475
476impl crate::formats::traits::FormatCreator for ZipCreator {
477    fn create(
478        &self,
479        output: &std::path::Path,
480        sources: &[&std::path::Path],
481        config: &CreationConfig,
482        progress: &mut dyn ProgressCallback,
483    ) -> crate::Result<crate::creation::CreationReport> {
484        create_zip_with_progress(output, sources, config, progress)
485    }
486
487    fn format_name(&self) -> &'static str {
488        "zip"
489    }
490}
491
492#[cfg(test)]
493#[allow(clippy::unwrap_used)] // Allow unwrap in tests for brevity
494mod tests {
495    use super::*;
496    use std::fs;
497    use tempfile::TempDir;
498
499    #[test]
500    fn test_create_zip_single_file() {
501        let temp = TempDir::new().unwrap();
502        let output = temp.path().join("output.zip");
503
504        // Create source file
505        let source_dir = TempDir::new().unwrap();
506        fs::write(source_dir.path().join("test.txt"), "Hello ZIP").unwrap();
507
508        let config = CreationConfig::default()
509            .with_exclude_patterns(vec![])
510            .with_include_hidden(true);
511
512        let report = create_zip(&output, &[source_dir.path().join("test.txt")], &config).unwrap();
513
514        assert_eq!(report.files_added, 1);
515        assert!(report.bytes_written > 0);
516        assert!(output.exists());
517    }
518
519    #[test]
520    fn test_create_zip_directory() {
521        let temp = TempDir::new().unwrap();
522        let output = temp.path().join("output.zip");
523
524        // Create source directory with multiple files
525        let source_dir = TempDir::new().unwrap();
526        fs::write(source_dir.path().join("file1.txt"), "content1").unwrap();
527        fs::write(source_dir.path().join("file2.txt"), "content2").unwrap();
528        fs::create_dir(source_dir.path().join("subdir")).unwrap();
529        fs::write(source_dir.path().join("subdir/file3.txt"), "content3").unwrap();
530
531        let config = CreationConfig::default()
532            .with_exclude_patterns(vec![])
533            .with_include_hidden(true);
534
535        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
536
537        // Should have exactly 3 files: file1.txt, file2.txt, subdir/file3.txt
538        assert_eq!(report.files_added, 3);
539        // Should have exactly 2 directories: root and subdir
540        assert_eq!(report.directories_added, 2);
541        assert!(output.exists());
542    }
543
544    #[test]
545    fn test_create_zip_compression() {
546        let temp = TempDir::new().unwrap();
547        let output = temp.path().join("output.zip");
548
549        // Create source file with repetitive content (compresses well)
550        let source_dir = TempDir::new().unwrap();
551        fs::write(source_dir.path().join("test.txt"), "a".repeat(1000)).unwrap();
552
553        let config = CreationConfig::default()
554            .with_exclude_patterns(vec![])
555            .with_compression_level(9);
556
557        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
558
559        assert_eq!(report.files_added, 1);
560        assert!(output.exists());
561
562        // Verify it's a valid ZIP file (basic check)
563        let data = fs::read(&output).unwrap();
564        assert_eq!(&data[0..4], b"PK\x03\x04"); // ZIP local file header magic
565    }
566
567    #[test]
568    fn test_create_zip_compression_levels() {
569        let temp = TempDir::new().unwrap();
570
571        // Create source with repetitive data (compresses well)
572        let source_dir = TempDir::new().unwrap();
573        fs::write(source_dir.path().join("test.txt"), "a".repeat(10000)).unwrap();
574
575        // Test different compression levels (1-9 are valid)
576        for level in [1, 6, 9] {
577            let output = temp.path().join(format!("output_{level}.zip"));
578            let config = CreationConfig::default()
579                .with_exclude_patterns(vec![])
580                .with_compression_level(level);
581
582            let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
583            assert_eq!(report.files_added, 1);
584            assert!(output.exists());
585        }
586    }
587
588    #[test]
589    fn test_create_zip_explicit_directories() {
590        let temp = TempDir::new().unwrap();
591        let output = temp.path().join("output.zip");
592
593        // Create source directory structure
594        let source_dir = TempDir::new().unwrap();
595        fs::create_dir(source_dir.path().join("dir1")).unwrap();
596        fs::create_dir(source_dir.path().join("dir1/dir2")).unwrap();
597        fs::write(source_dir.path().join("dir1/dir2/file.txt"), "content").unwrap();
598
599        let config = CreationConfig::default()
600            .with_exclude_patterns(vec![])
601            .with_include_hidden(true);
602
603        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
604
605        assert!(report.directories_added >= 2); // dir1 and dir1/dir2
606        assert!(output.exists());
607
608        // Verify directories have trailing slash by reading archive
609        let file = File::open(&output).unwrap();
610        let mut archive = zip::ZipArchive::new(file).unwrap();
611
612        let mut dir_entries = 0;
613        for i in 0..archive.len() {
614            let entry = archive.by_index(i).unwrap();
615            if entry.is_dir() {
616                dir_entries += 1;
617                assert!(
618                    entry.name().unwrap().ends_with('/'),
619                    "Directory entry should end with /"
620                );
621            }
622        }
623        assert!(dir_entries >= 2, "Expected at least 2 directory entries");
624    }
625
626    #[cfg(unix)]
627    #[test]
628    fn test_create_zip_preserves_permissions() {
629        use std::os::unix::fs::PermissionsExt;
630
631        let temp = TempDir::new().unwrap();
632        let output = temp.path().join("output.zip");
633
634        // Create source file with specific permissions
635        let source_dir = TempDir::new().unwrap();
636        let file_path = source_dir.path().join("test.txt");
637        fs::write(&file_path, "content").unwrap();
638        fs::set_permissions(&file_path, fs::Permissions::from_mode(0o755)).unwrap();
639
640        let config = CreationConfig::default()
641            .with_exclude_patterns(vec![])
642            .with_preserve_permissions(true);
643
644        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
645        assert_eq!(report.files_added, 1);
646
647        // Verify permissions in archive
648        let file = File::open(&output).unwrap();
649        let mut archive = zip::ZipArchive::new(file).unwrap();
650
651        for i in 0..archive.len() {
652            let entry = archive.by_index(i).unwrap();
653            if entry.name().unwrap().contains("test.txt")
654                && let Some(mode) = entry.unix_mode()
655            {
656                assert_eq!(mode & 0o777, 0o755, "Permissions should be preserved");
657            }
658        }
659    }
660
661    #[test]
662    fn test_create_zip_report_statistics() {
663        let temp = TempDir::new().unwrap();
664        let output = temp.path().join("output.zip");
665
666        // Create source directory with known structure
667        let source_dir = TempDir::new().unwrap();
668        fs::write(source_dir.path().join("file1.txt"), "content1").unwrap();
669        fs::write(source_dir.path().join("file2.txt"), "content2").unwrap();
670        fs::create_dir(source_dir.path().join("subdir")).unwrap();
671        fs::write(source_dir.path().join("subdir/file3.txt"), "content3").unwrap();
672
673        let config = CreationConfig::default()
674            .with_exclude_patterns(vec![])
675            .with_include_hidden(true);
676
677        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
678
679        assert_eq!(report.files_added, 3);
680        assert!(report.directories_added >= 1);
681        assert_eq!(report.files_skipped, 0);
682        assert!(!report.has_warnings());
683        assert!(report.duration.as_nanos() > 0);
684    }
685
686    #[test]
687    fn test_create_zip_roundtrip() {
688        let temp = TempDir::new().unwrap();
689        let output = temp.path().join("output.zip");
690
691        // Create source directory
692        let source_dir = TempDir::new().unwrap();
693        fs::write(source_dir.path().join("file1.txt"), "content1").unwrap();
694        fs::create_dir(source_dir.path().join("subdir")).unwrap();
695        fs::write(source_dir.path().join("subdir/file2.txt"), "content2").unwrap();
696
697        let config = CreationConfig::default()
698            .with_exclude_patterns(vec![])
699            .with_include_hidden(true);
700
701        // Create archive
702        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
703        assert!(report.files_added >= 2);
704
705        // Extract and verify using zip crate
706        let file = File::open(&output).unwrap();
707        let mut archive = zip::ZipArchive::new(file).unwrap();
708
709        let extract_dir = TempDir::new().unwrap();
710
711        for i in 0..archive.len() {
712            let mut entry = archive.by_index(i).unwrap();
713            let outpath = extract_dir.path().join(entry.name().unwrap().as_ref());
714
715            if entry.is_dir() {
716                fs::create_dir_all(&outpath).unwrap();
717            } else {
718                if let Some(parent) = outpath.parent() {
719                    fs::create_dir_all(parent).unwrap();
720                }
721                let mut outfile = File::create(&outpath).unwrap();
722                std::io::copy(&mut entry, &mut outfile).unwrap();
723            }
724        }
725
726        // Verify extracted files match originals
727        let extracted1 = fs::read_to_string(extract_dir.path().join("file1.txt")).unwrap();
728        assert_eq!(extracted1, "content1");
729
730        let extracted2 = fs::read_to_string(extract_dir.path().join("subdir/file2.txt")).unwrap();
731        assert_eq!(extracted2, "content2");
732    }
733
734    #[test]
735    fn test_create_zip_forward_slashes() {
736        let temp = TempDir::new().unwrap();
737        let output = temp.path().join("output.zip");
738
739        // Create source directory structure
740        let source_dir = TempDir::new().unwrap();
741        fs::create_dir(source_dir.path().join("dir1")).unwrap();
742        fs::write(source_dir.path().join("dir1/file.txt"), "content").unwrap();
743
744        let config = CreationConfig::default()
745            .with_exclude_patterns(vec![])
746            .with_include_hidden(true);
747
748        create_zip(&output, &[source_dir.path()], &config).unwrap();
749
750        // Verify paths use forward slashes
751        let file = File::open(&output).unwrap();
752        let mut archive = zip::ZipArchive::new(file).unwrap();
753
754        for i in 0..archive.len() {
755            let entry = archive.by_index(i).unwrap();
756            let name = entry.name().unwrap();
757            // ZIP paths should never contain backslashes
758            assert!(
759                !name.contains('\\'),
760                "ZIP path should use forward slashes: {name}"
761            );
762            // Subdirectory paths should use forward slash
763            if name.contains("dir1") && name.contains("file") {
764                assert!(name.contains("dir1/file"), "Expected forward slash in path");
765            }
766        }
767    }
768
769    #[test]
770    fn test_create_zip_source_not_found() {
771        let temp = TempDir::new().unwrap();
772        let output = temp.path().join("output.zip");
773
774        let config = CreationConfig::default();
775        let result = create_zip(&output, &[Path::new("/nonexistent/path")], &config);
776
777        assert!(result.is_err());
778        assert!(matches!(
779            result.unwrap_err(),
780            ExtractionError::SourceNotFound { .. }
781        ));
782    }
783
784    #[test]
785    fn test_normalize_zip_path() {
786        // Basic path
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        // Single file
792        let path = Path::new("file.txt");
793        let normalized = normalize_zip_path(path).unwrap();
794        assert_eq!(normalized, "file.txt");
795
796        // Nested directories
797        let path = Path::new("a/b/c/file.txt");
798        let normalized = normalize_zip_path(path).unwrap();
799        assert_eq!(normalized, "a/b/c/file.txt");
800    }
801
802    #[cfg(windows)]
803    #[test]
804    fn test_normalize_zip_path_windows() {
805        // Windows path with backslashes
806        let path = Path::new("dir\\file.txt");
807        let normalized = normalize_zip_path(path).unwrap();
808        assert_eq!(normalized, "dir/file.txt");
809
810        // Nested with backslashes
811        let path = Path::new("a\\b\\c\\file.txt");
812        let normalized = normalize_zip_path(path).unwrap();
813        assert_eq!(normalized, "a/b/c/file.txt");
814    }
815
816    #[test]
817    fn test_create_zip_max_file_size() {
818        let temp = TempDir::new().unwrap();
819        let output = temp.path().join("output.zip");
820
821        // Create files with different sizes
822        let source_dir = TempDir::new().unwrap();
823        fs::write(source_dir.path().join("small.txt"), "tiny").unwrap(); // 4 bytes
824        fs::write(source_dir.path().join("large.txt"), "a".repeat(1000)).unwrap(); // 1000 bytes
825
826        // Set max file size to 100 bytes
827        let config = CreationConfig::default()
828            .with_exclude_patterns(vec![])
829            .with_max_file_size(Some(100));
830
831        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
832
833        // Walker filters out large.txt, so only small.txt is added
834        // No files are skipped at the ZIP level (walker already filtered)
835        assert_eq!(report.files_added, 1);
836        assert_eq!(report.files_skipped, 0);
837    }
838
839    #[cfg(unix)]
840    #[test]
841    fn test_create_zip_skips_symlinks() {
842        let temp = TempDir::new().unwrap();
843        let output = temp.path().join("output.zip");
844
845        // Create source with symlink
846        let source_dir = TempDir::new().unwrap();
847        fs::write(source_dir.path().join("target.txt"), "content").unwrap();
848        std::os::unix::fs::symlink(
849            source_dir.path().join("target.txt"),
850            source_dir.path().join("link.txt"),
851        )
852        .unwrap();
853
854        // Don't follow symlinks (default)
855        let config = CreationConfig::default()
856            .with_exclude_patterns(vec![])
857            .with_include_hidden(true);
858
859        let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
860
861        // Should add target.txt, skip link.txt
862        assert_eq!(report.files_added, 1);
863        assert_eq!(report.files_skipped, 1);
864        assert!(report.has_warnings());
865
866        let warning = &report.warnings[0];
867        assert!(warning.contains("Skipped symlink"));
868    }
869
870    #[test]
871    fn test_create_zip_with_progress_callback() {
872        #[derive(Debug, Default, Clone)]
873        struct TestProgress {
874            entries_started: Vec<String>,
875            entries_completed: Vec<String>,
876            bytes_written: u64,
877            completed: bool,
878        }
879
880        impl ProgressCallback for TestProgress {
881            fn on_entry_start(&mut self, path: &Path, _total: usize, _current: usize) {
882                self.entries_started
883                    .push(path.to_string_lossy().to_string());
884            }
885
886            fn on_bytes_written(&mut self, bytes: u64) {
887                self.bytes_written += bytes;
888            }
889
890            fn on_entry_complete(&mut self, path: &Path) {
891                self.entries_completed
892                    .push(path.to_string_lossy().to_string());
893            }
894
895            fn on_complete(&mut self) {
896                self.completed = true;
897            }
898        }
899
900        let temp = TempDir::new().unwrap();
901        let output = temp.path().join("output.zip");
902
903        // Create source directory with multiple files
904        let source_dir = TempDir::new().unwrap();
905        fs::write(source_dir.path().join("file1.txt"), "content1").unwrap();
906        fs::write(source_dir.path().join("file2.txt"), "content2").unwrap();
907        fs::create_dir(source_dir.path().join("subdir")).unwrap();
908        fs::write(source_dir.path().join("subdir/file3.txt"), "content3").unwrap();
909
910        let config = CreationConfig::default()
911            .with_exclude_patterns(vec![])
912            .with_include_hidden(true);
913
914        let mut progress = TestProgress::default();
915
916        let report =
917            create_zip_with_progress(&output, &[source_dir.path()], &config, &mut progress)
918                .unwrap();
919
920        // Verify report
921        assert_eq!(report.files_added, 3);
922        assert!(report.directories_added >= 1);
923
924        // Verify callbacks were invoked
925        assert!(
926            progress.entries_started.len() >= 3,
927            "Expected at least 3 entry starts, got {}",
928            progress.entries_started.len()
929        );
930        assert!(
931            progress.entries_completed.len() >= 3,
932            "Expected at least 3 entry completions, got {}",
933            progress.entries_completed.len()
934        );
935        assert!(
936            progress.bytes_written > 0,
937            "Expected bytes written > 0, got {}",
938            progress.bytes_written
939        );
940        assert!(progress.completed, "Expected on_complete to be called");
941
942        // Verify specific entries
943        let has_file1 = progress
944            .entries_started
945            .iter()
946            .any(|p| p.contains("file1.txt"));
947        let has_file2 = progress
948            .entries_started
949            .iter()
950            .any(|p| p.contains("file2.txt"));
951        let has_file3 = progress
952            .entries_started
953            .iter()
954            .any(|p| p.contains("file3.txt"));
955
956        assert!(has_file1, "Expected file1.txt in progress callbacks");
957        assert!(has_file2, "Expected file2.txt in progress callbacks");
958        assert!(has_file3, "Expected file3.txt in progress callbacks");
959    }
960}