### Archive Files with Progress Monitoring Source: https://context7.com/saracen/fastzip/llms.txt Archive specific file types using a context with a timeout and monitor archiving progress. This example includes directories and .go files, sets a 5-minute timeout, and displays progress in bytes and entries per second. ```go package main import ( "context" "fmt" "os" "path/filepath" "time" "github.com/saracen/fastzip" ) func main() { archiveFile, _ := os.Create("project.zip") defer archiveFile.Close() sourceDir := "/path/to/project" archiver, _ := fastzip.NewArchiver(archiveFile, sourceDir, fastzip.WithArchiverConcurrency(4), ) defer archiver.Close() // Collect only specific file types files := make(map[string]os.FileInfo) filepath.Walk(sourceDir, func(pathname string, info os.FileInfo, err error) error { if err != nil { return err } // Include directories and .go files only if info.IsDir() || filepath.Ext(pathname) == ".go" { files[pathname] = info } return nil }) // Create context with timeout for archiving ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) defer cancel() // Archive with progress monitoring in separate goroutine go func() { for { bytes, entries := archiver.Written() fmt.Printf("Progress: %d bytes, %d entries\n", bytes, entries) time.Sleep(time.Second) } }() if err := archiver.Archive(ctx, files); err != nil { panic(err) } bytes, entries := archiver.Written() fmt.Printf("Completed: %d bytes, %d entries\n", bytes, entries) } ``` -------------------------------- ### Configure Archiver with Custom Options Source: https://context7.com/saracen/fastzip/llms.txt Customize Archiver behavior using options like concurrency, compression method, buffer size, and staging directory. This example sets concurrency to 8, uses the Store method (no compression), a 4MB buffer, and a custom staging directory. ```go package main import ( "context" "os" "path/filepath" "github.com/klauspost/compress/zip" "github.com/saracen/fastzip" ) func main() { archiveFile, err := os.Create("archive.zip") if err != nil { panic(err) } defer archiveFile.Close() sourceDir := "/path/to/source" // Create archiver with multiple options: // - Concurrency of 8 workers // - Store method (no compression) for pre-compressed content // - 4MB buffer per concurrent file // - Custom staging directory for temporary files archiver, err := fastzip.NewArchiver(archiveFile, sourceDir, fastzip.WithArchiverConcurrency(8), fastzip.WithArchiverMethod(zip.Store), fastzip.WithArchiverBufferSize(4*1024*1024), fastzip.WithStageDirectory("/tmp/staging"), ) if err != nil { panic(err) } defer archiver.Close() files := make(map[string]os.FileInfo) filepath.Walk(sourceDir, func(pathname string, info os.FileInfo, err error) error { files[pathname] = info return nil }) err = archiver.Archive(context.Background(), files) if err != nil { panic(err) } } ``` -------------------------------- ### NewArchiver with Options Source: https://context7.com/saracen/fastzip/llms.txt Initializes an Archiver with custom configuration options for performance and staging. ```APIDOC ## NewArchiver with Options ### Description Creates an Archiver with custom configuration options for concurrency, compression method, buffer size, and staging directory. ### Options - **WithArchiverConcurrency** (int) - Sets the number of concurrent workers. - **WithArchiverMethod** (uint16) - Sets the compression method (e.g., zip.Store). - **WithArchiverBufferSize** (int) - Sets the buffer size per concurrent file. - **WithStageDirectory** (string) - Sets a custom staging directory for temporary files. ``` -------------------------------- ### Archive Files and Directories with Fastzip Source: https://github.com/saracen/fastzip/blob/main/README.md Use this snippet to create a zip archive from a specified directory. Ensure the output file and the source directory are correctly set up. Buffers are recycled, and files are archived concurrently. ```go // Create archive file w, err := os.Create("archive.zip") if err != nil { panic(err) } defer w.Close() // Create new Archiver a, err := fastzip.NewArchiver(w, "~/fastzip-archiving") if err != nil { panic(err) } defer a.Close() // Register a non-default level compressor if required // a.RegisterCompressor(zip.Deflate, fastzip.FlateCompressor(1)) // Walk directory, adding the files we want to add files := make(map[string]os.FileInfo) if err = filepath.Walk("~/fastzip-archiving", func(pathname string, info os.FileInfo, err error) error { files[pathname] = info return nil }); err != nil { panic(err) } // Archive if err = a.Archive(context.Background(), files); err != nil { panic(err) } ``` -------------------------------- ### NewArchiver Source: https://context7.com/saracen/fastzip/llms.txt Initializes a new Archiver instance to write zip archives, enforcing a chroot directory for security. ```APIDOC ## NewArchiver ### Description Creates a new Archiver instance for building zip archives. The archiver writes to the provided io.Writer and restricts all file operations to the specified chroot directory. ### Parameters - **w** (io.Writer) - The destination for the zip archive. - **chroot** (string) - The directory to restrict all file operations to. ### Returns - **archiver** (*fastzip.Archiver) - The initialized archiver instance. - **err** (error) - Returns an error if the chroot path cannot be resolved. ``` -------------------------------- ### Run Benchmarks Source: https://github.com/saracen/fastzip/blob/main/README.md Execute benchmark tests for archiving and extraction using Go's testing package. The `-bench` flag specifies the benchmark functions to run, `-archivedir` indicates the directory to archive, and `-benchtime` sets the duration for each benchmark. ```bash $ go test -bench Benchmark* -archivedir go1.13 -benchtime=30s -timeout=20m ``` -------------------------------- ### Configure Extractor with Options Source: https://context7.com/saracen/fastzip/llms.txt Initializes an extractor with custom concurrency settings and a callback for handling chown errors during extraction. ```go package main import ( "context" "fmt" "os" "github.com/saracen/fastzip" ) func main() { // Create extractor with custom options extractor, err := fastzip.NewExtractor("archive.zip", "/path/to/extract", // Set concurrent extraction to 16 workers fastzip.WithExtractorConcurrency(16), // Handle ownership preservation errors (common when not running as root) fastzip.WithExtractorChownErrorHandler(func(name string, err error) error { // Log the error but continue extraction fmt.Fprintf(os.Stderr, "Warning: cannot set ownership for %s: %v\n", name, err) return nil // Return nil to continue, return error to abort }), ) if err != nil { panic(err) } defer extractor.Close() if err = extractor.Extract(context.Background()); err != nil { panic(err) } } ``` -------------------------------- ### Create New Archiver for Zip Archives Source: https://context7.com/saracen/fastzip/llms.txt Use NewArchiver to create an archiver instance for building zip archives. It writes to an io.Writer and restricts operations to a specified chroot directory. Ensure the chroot path is absolute. ```go package main import ( "context" "os" "path/filepath" "github.com/saracen/fastzip" ) func main() { // Create the archive output file archiveFile, err := os.Create("backup.zip") if err != nil { panic(err) } defer archiveFile.Close() // Create archiver with chroot directory sourceDir := "/path/to/source" archiver, err := fastzip.NewArchiver(archiveFile, sourceDir) if err != nil { panic(err) } defer archiver.Close() // Collect files to archive files := make(map[string]os.FileInfo) err = filepath.Walk(sourceDir, func(pathname string, info os.FileInfo, err error) error { if err != nil { return err } files[pathname] = info return nil }) if err != nil { panic(err) } // Archive files with context for cancellation support if err = archiver.Archive(context.Background(), files); err != nil { panic(err) } // Output: Creates backup.zip with all files from sourceDir } ``` -------------------------------- ### WithArchiverOffset Option Source: https://context7.com/saracen/fastzip/llms.txt Sets the offset for the beginning of the zip data, enabling the appending of zip data to existing files. ```APIDOC ## WithArchiverOffset ### Description Sets the offset of the beginning of the zip data. This is useful when appending zip data to an existing file, such as creating self-extracting archives. ### Parameters - **offset** (int64) - Required - The byte offset where the zip data should begin in the target file. ### Usage Example ```go archiver, err := fastzip.NewArchiver(existingFile, sourceDir, fastzip.WithArchiverOffset(offset)) ``` ``` -------------------------------- ### Extract Files from a Zip Archive with Fastzip Source: https://github.com/saracen/fastzip/blob/main/README.md This code snippet demonstrates how to extract the contents of a zip archive to a specified directory. Ensure the archive file exists and the extraction directory is accessible. ```go // Create new extractor e, err := fastzip.NewExtractor("archive.zip", "~/fastzip-extraction") if err != nil { panic(err) } defer e.Close() // Extract archive files if err = e.Extract(context.Background()); err != nil { panic(err) } ``` -------------------------------- ### FlateCompressor Configuration Source: https://context7.com/saracen/fastzip/llms.txt Configures a high-performance compressor using the klauspost/compress/flate library with a specified compression level. ```APIDOC ## FlateCompressor ### Description Returns a pooled high-performance compressor configured to a specified compression level. This is used to register a compressor with the archiver. ### Parameters - **level** (int) - Required - Compression level: -2 (Huffman only), -1 (Default), 0 (No compression), 1 (Best speed), 5 (Balanced), 9 (Best compression). ### Usage Example ```go archiver.RegisterCompressor(zip.Deflate, fastzip.FlateCompressor(1)) ``` ``` -------------------------------- ### Register a Custom Compressor Source: https://context7.com/saracen/fastzip/llms.txt Registers a custom compressor for a specified compression method ID. This allows using different compression levels or alternative compression implementations. ```go package main import ( "context" "os" "path/filepath" "github.com/klauspost/compress/zip" "github.com/saracen/fastzip" ) func main() { archiveFile, _ := os.Create("highcomp.zip") defer archiveFile.Close() sourceDir := "/path/to/source" archiver, _ := fastzip.NewArchiver(archiveFile, sourceDir) defer archiver.Close() // Register maximum compression (level 9) for better compression ratio archiver.RegisterCompressor(zip.Deflate, fastzip.FlateCompressor(9)) // Or use standard library compressor instead of klauspost // archiver.RegisterCompressor(zip.Deflate, fastzip.StdFlateCompressor(6)) files := make(map[string]os.FileInfo) filepath.Walk(sourceDir, func(pathname string, info os.FileInfo, err error) error { files[pathname] = info return nil }) archiver.Archive(context.Background(), files) } ``` -------------------------------- ### Extract Files from Zip Source: https://context7.com/saracen/fastzip/llms.txt Creates an extractor from a file path to extract contents into a chroot directory. The extractor must be closed after use to release file handles. ```go package main import ( "context" "fmt" "github.com/saracen/fastzip" ) func main() { // Create extractor from zip file extractor, err := fastzip.NewExtractor("archive.zip", "/path/to/extract") if err != nil { panic(err) } defer extractor.Close() // List files in archive before extraction for _, file := range extractor.Files() { fmt.Printf("File: %s, Size: %d, Mode: %s\n", file.Name, file.UncompressedSize64, file.Mode()) } // Extract all files if err = extractor.Extract(context.Background()); err != nil { panic(err) } bytes, entries := extractor.Written() fmt.Printf("Extracted: %d bytes, %d entries\n", bytes, entries) } ``` -------------------------------- ### Extract Archive with Progress Monitoring Source: https://context7.com/saracen/fastzip/llms.txt Performs concurrent extraction while tracking progress using a background goroutine and context timeout. ```go package main import ( "context" "fmt" "sync" "time" "github.com/saracen/fastzip" ) func main() { extractor, err := fastzip.NewExtractor("large-archive.zip", "/path/to/extract", fastzip.WithExtractorConcurrency(8), ) if err != nil { panic(err) } defer extractor.Close() // Calculate total uncompressed size for progress var totalSize int64 for _, file := range extractor.Files() { totalSize += int64(file.UncompressedSize64) } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) defer cancel() var wg sync.WaitGroup wg.Add(1) // Monitor extraction progress go func() { defer wg.Done() ticker := time.NewTicker(time.Second) defer ticker.Stop() for { select { case <-ctx.Done(): return case <-ticker.C: bytes, entries := extractor.Written() percent := float64(bytes) / float64(totalSize) * 100 fmt.Printf("Extracted: %.1f%% (%d/%d bytes), %d entries\n", percent, bytes, totalSize, entries) } } }() if err = extractor.Extract(ctx); err != nil { panic(err) } cancel() // Stop progress monitor wg.Wait() fmt.Println("Extraction complete!") } ``` -------------------------------- ### Benchmark Results - Archiving Source: https://github.com/saracen/fastzip/blob/main/README.md Benchmark results for archiving operations. Shows performance in operations per second (op/s), MB/s, bytes per operation (B/op), and allocations per operation (allocs/op) for different concurrency levels and compression methods. ```text goos: linux goarch: amd64 pkg: github.com/saracen/fastzip BenchmarkArchiveStore_1-24 39 788604969 ns/op 421.66 MB/s 9395405 B/op 266271 allocs/op BenchmarkArchiveStandardFlate_1-24 2 16154127468 ns/op 20.58 MB/s 12075824 B/op 257251 allocs/op BenchmarkArchiveStandardFlate_2-24 4 8686391074 ns/op 38.28 MB/s 15898644 B/op 260757 allocs/op BenchmarkArchiveStandardFlate_4-24 7 4391603068 ns/op 75.72 MB/s 19295604 B/op 260871 allocs/op BenchmarkArchiveStandardFlate_8-24 14 2291624196 ns/op 145.10 MB/s 21999205 B/op 260970 allocs/op BenchmarkArchiveStandardFlate_16-24 16 2105056696 ns/op 157.96 MB/s 29237232 B/op 261225 allocs/op BenchmarkArchiveNonStandardFlate_1-24 6 6011250439 ns/op 55.32 MB/s 11070960 B/op 257204 allocs/op BenchmarkArchiveNonStandardFlate_2-24 9 3629347294 ns/op 91.62 MB/s 18870130 B/op 262279 allocs/op BenchmarkArchiveNonStandardFlate_4-24 18 1766182097 ns/op 188.27 MB/s 22976928 B/op 262349 allocs/op BenchmarkArchiveNonStandardFlate_8-24 34 1002516188 ns/op 331.69 MB/s 29860872 B/op 262473 allocs/op BenchmarkArchiveNonStandardFlate_16-24 46 757112363 ns/op 439.20 MB/s 42036132 B/op 262714 allocs/op ``` -------------------------------- ### Create Archiver with Offset for Appending Zip Data Source: https://context7.com/saracen/fastzip/llms.txt Creates a new archiver with a specified offset, useful for appending zip data to existing files, such as creating self-extracting archives. The offset indicates the position where zip data should begin. ```go package main import ( "context" "os" "path/filepath" "github.com/saracen/fastzip" ) func main() { // Open existing file to append zip data existingFile, err := os.OpenFile("existing.exe", os.O_RDWR|os.O_APPEND, 0644) if err != nil { panic(err) } defer existingFile.Close() // Get current file size as offset for zip data stat, _ := existingFile.Stat() offset := stat.Size() sourceDir := "/path/to/source" // Create archiver with offset pointing to end of existing file archiver, err := fastzip.NewArchiver(existingFile, sourceDir, fastzip.WithArchiverOffset(offset), ) if err != nil { panic(err) } defer archiver.Close() files := make(map[string]os.FileInfo) filepath.Walk(sourceDir, func(pathname string, info os.FileInfo, err error) error { files[pathname] = info return nil }) archiver.Archive(context.Background(), files) // Result: Zip data appended after offset bytes in the file } ``` -------------------------------- ### Extract Source: https://context7.com/saracen/fastzip/llms.txt Extracts all files, symlinks, and directories from the archive to the target directory. ```APIDOC ## Extract ### Description Extracts all files, symlinks, and directories from the archive to the chroot directory. Files are extracted concurrently based on the configured concurrency level. Preserves file permissions, ownership, and modification times. ### Parameters - **ctx** (context.Context) - Required - Context for cancellation and timeout management. ``` -------------------------------- ### Inspect Archive Contents Source: https://context7.com/saracen/fastzip/llms.txt Iterates through archive files to inspect metadata, check for symlinks, and categorize files by extension before extraction. ```go package main import ( "context" "fmt" "os" "path/filepath" "strings" "github.com/saracen/fastzip" ) func main() { extractor, err := fastzip.NewExtractor("archive.zip", "/path/to/extract") if err != nil { panic(err) } defer extractor.Close() // Inspect and filter files before extraction fmt.Println("Archive contents:") for _, file := range extractor.Files() { sizeKB := float64(file.UncompressedSize64) / 1024 compressedKB := float64(file.CompressedSize64) / 1024 ratio := float64(file.CompressedSize64) / float64(file.UncompressedSize64) * 100 fmt.Printf(" %s\n", file.Name) fmt.Printf(" Size: %.2f KB -> %.2f KB (%.1f%%)\n", sizeKB, compressedKB, ratio) fmt.Printf(" Mode: %s, Modified: %s\n", file.Mode(), file.Modified) // Check for symlinks if file.Mode()&os.ModeSymlink != 0 { fmt.Printf(" Type: Symlink\n") } } // Count files by extension extensions := make(map[string]int) for _, file := range extractor.Files() { if !file.Mode().IsDir() { ext := strings.ToLower(filepath.Ext(file.Name)) if ext == "" { ext = "(no extension)" } extensions[ext]++ } } fmt.Println("\nFile types:") for ext, count := range extensions { fmt.Printf(" %s: %d files\n", ext, count) } extractor.Extract(context.Background()) } ``` -------------------------------- ### Benchmark Results - Extraction Source: https://github.com/saracen/fastzip/blob/main/README.md Benchmark results for extraction operations. Shows performance in operations per second (op/s), MB/s, bytes per operation (B/op), and allocations per operation (allocs/op) for different concurrency levels and compression methods. ```text BenchmarkExtractStore_1-24 20 1625582744 ns/op 202.66 MB/s 22900375 B/op 330528 allocs/op BenchmarkExtractStore_2-24 42 786644031 ns/op 418.80 MB/s 22307976 B/op 329272 allocs/op BenchmarkExtractStore_4-24 92 384075767 ns/op 857.76 MB/s 22247288 B/op 328667 allocs/op BenchmarkExtractStore_8-24 165 215884636 ns/op 1526.02 MB/s 22354996 B/op 328459 allocs/op BenchmarkExtractStore_16-24 226 157087517 ns/op 2097.20 MB/s 22258691 B/op 328393 allocs/op BenchmarkExtractStandardFlate_1-24 6 5501808448 ns/op 23.47 MB/s 86148462 B/op 495586 allocs/op BenchmarkExtractStandardFlate_2-24 13 2748387174 ns/op 46.99 MB/s 84232141 B/op 491343 allocs/op BenchmarkExtractStandardFlate_4-24 21 1511063035 ns/op 85.47 MB/s 84998750 B/op 490124 allocs/op BenchmarkExtractStandardFlate_8-24 32 995911009 ns/op 129.67 MB/s 86188957 B/op 489574 allocs/op BenchmarkExtractStandardFlate_16-24 46 652641882 ns/op 197.88 MB/s 88256113 B/op 489575 allocs/op BenchmarkExtractNonStandardFlate_1-24 7 4989810851 ns/op 25.88 MB/s 64552948 B/op 373541 allocs/op BenchmarkExtractNonStandardFlate_2-24 13 2478287953 ns/op 52.11 MB/s 63413947 B/op 373183 allocs/op BenchmarkExtractNonStandardFlate_4-24 26 1333552250 ns/op 96.84 MB/s 63546389 B/op 373925 allocs/op BenchmarkExtractNonStandardFlate_8-24 37 817039739 ns/op 158.06 MB/s 64354655 B/op 375357 allocs/op BenchmarkExtractNonStandardFlate_16-24 63 566984549 ns/op 227.77 MB/s 65444227 B/op 379664 allocs/op ``` -------------------------------- ### NewExtractor Source: https://context7.com/saracen/fastzip/llms.txt Creates a new Extractor from a zip file path to extract files to a specified directory. ```APIDOC ## NewExtractor ### Description Creates a new Extractor from a zip file path. Files are extracted to the specified chroot directory. The extractor must be closed after use. ### Parameters - **zipPath** (string) - Required - Path to the zip file. - **chroot** (string) - Required - Directory to extract files into. ``` -------------------------------- ### NewExtractor Source: https://context7.com/saracen/fastzip/llms.txt Creates a new Extractor instance with optional configuration for concurrency and error handling. ```APIDOC ## NewExtractor ### Description Creates an Extractor with custom configuration for concurrency and chown error handling. ### Parameters - **archivePath** (string) - Required - Path to the zip archive. - **outputPath** (string) - Required - Destination directory for extraction. - **options** (variadic) - Optional - Configuration options like WithExtractorConcurrency or WithExtractorChownErrorHandler. ``` -------------------------------- ### Register FlateCompressor for Fast Zip Compression Source: https://context7.com/saracen/fastzip/llms.txt Registers a high-performance flate compressor for zip archives. Use compression levels between -2 (fastest, minimal compression) and 9 (slowest, best compression). ```go package main import ( "context" "os" "path/filepath" "github.com/klauspost/compress/zip" "github.com/saracen/fastzip" ) func main() { archiveFile, _ := os.Create("archive.zip") defer archiveFile.Close() sourceDir := "/path/to/source" archiver, _ := fastzip.NewArchiver(archiveFile, sourceDir) defer archiver.Close() // Compression levels: // -2: Huffman only (fastest, minimal compression) // -1: Default compression (balanced) // 0: No compression (same as Store) // 1: Best speed // 5: Good balance of speed and compression // 9: Best compression (slowest) // Register fast compression for large archives archiver.RegisterCompressor(zip.Deflate, fastzip.FlateCompressor(1)) // Or maximum compression for smaller output // archiver.RegisterCompressor(zip.Deflate, fastzip.FlateCompressor(9)) files := make(map[string]os.FileInfo) filepath.Walk(sourceDir, func(pathname string, info os.FileInfo, err error) error { files[pathname] = info return nil }) archiver.Archive(context.Background(), files) } ``` -------------------------------- ### Monitor Archiving Progress Source: https://context7.com/saracen/fastzip/llms.txt Uses the Written method to track the number of bytes and entries processed during an archive operation. This method is safe to call concurrently while archiving is in progress. ```go package main import ( "context" "fmt" "os" "path/filepath" "sync" "time" "github.com/saracen/fastzip" ) func main() { archiveFile, _ := os.Create("large.zip") defer archiveFile.Close() sourceDir := "/path/to/large/directory" archiver, _ := fastzip.NewArchiver(archiveFile, sourceDir, fastzip.WithArchiverConcurrency(8), ) defer archiver.Close() files := make(map[string]os.FileInfo) var totalSize int64 filepath.Walk(sourceDir, func(pathname string, info os.FileInfo, err error) error { if err == nil && !info.IsDir() { totalSize += info.Size() } files[pathname] = info return nil }) var wg sync.WaitGroup wg.Add(1) // Monitor progress in background go func() { defer wg.Done() ticker := time.NewTicker(500 * time.Millisecond) defer ticker.Stop() for range ticker.C { bytes, entries := archiver.Written() percent := float64(bytes) / float64(totalSize) * 100 fmt.Printf("Written: %d bytes (%.1f%%), %d entries\n", bytes, percent, entries) if bytes >= totalSize { return } } }() archiver.Archive(context.Background(), files) wg.Wait() finalBytes, finalEntries := archiver.Written() fmt.Printf("Final: %d bytes compressed, %d entries\n", finalBytes, finalEntries) } ``` -------------------------------- ### Register Custom Decompressors Source: https://context7.com/saracen/fastzip/llms.txt Registers alternative decompression methods, such as standard library deflate or zstd, for specific compression IDs. ```go package main import ( "context" "github.com/klauspost/compress/zip" "github.com/saracen/fastzip" ) func main() { extractor, err := fastzip.NewExtractor("archive.zip", "/path/to/extract") if err != nil { panic(err) } defer extractor.Close() // Use standard library decompressor instead of klauspost extractor.RegisterDecompressor(zip.Deflate, fastzip.StdFlateDecompressor()) // Register zstd decompressor for zstd-compressed archives extractor.RegisterDecompressor(0x5D, fastzip.ZstdDecompressor()) // WinZip zstd method if err = extractor.Extract(context.Background()); err != nil { panic(err) } } ``` -------------------------------- ### Files Source: https://context7.com/saracen/fastzip/llms.txt Retrieves the list of files contained within the archive. ```APIDOC ## Files ### Description Returns the list of *zip.File entries contained in the archive. Useful for inspecting archive contents before extraction or for selective extraction. ### Response - **files** ([]*zip.File) - A slice of file entries present in the archive. ``` -------------------------------- ### Extract from Reader Source: https://context7.com/saracen/fastzip/llms.txt Creates an extractor from an io.ReaderAt, suitable for in-memory buffers or network streams. Closing the extractor is not required for this constructor. ```go package main import ( "bytes" "context" "fmt" "io" "net/http" "github.com/saracen/fastzip" ) func main() { // Example: Extract from HTTP response resp, err := http.Get("https://example.com/archive.zip") if err != nil { panic(err) } defer resp.Body.Close() // Read entire response into memory data, err := io.ReadAll(resp.Body) if err != nil { panic(err) } // Create reader from byte slice reader := bytes.NewReader(data) // Create extractor from reader extractor, err := fastzip.NewExtractorFromReader( reader, int64(len(data)), "/path/to/extract", fastzip.WithExtractorConcurrency(4), ) if err != nil { panic(err) } // No need to call Close() when using NewExtractorFromReader if err = extractor.Extract(context.Background()); err != nil { panic(err) } fmt.Printf("Extracted %d entries\n", len(extractor.Files())) } ``` -------------------------------- ### Archive Source: https://context7.com/saracen/fastzip/llms.txt Archives a collection of files into the zip file using the configured archiver. ```APIDOC ## Archive ### Description Archives all provided files, symlinks, and directories into the zip file. Files are compressed concurrently based on the configured concurrency level. ### Parameters - **ctx** (context.Context) - Context for cancellation support. - **files** (map[string]os.FileInfo) - A map of file paths to os.FileInfo objects to be archived. ### Returns - **err** (error) - Returns an error if the archiving process fails. ``` -------------------------------- ### Written (Archiver) Source: https://context7.com/saracen/fastzip/llms.txt Returns the number of bytes and entries written to the archive, useful for real-time progress monitoring. ```APIDOC ## Written ### Description Returns the number of bytes and entries written to the archive. This method is safe to call concurrently while archiving is in progress. ### Response - **bytes** (int64) - Number of bytes written. - **entries** (int64) - Number of entries written. ``` -------------------------------- ### NewExtractorFromReader Source: https://context7.com/saracen/fastzip/llms.txt Creates a new Extractor from an io.ReaderAt interface, suitable for in-memory buffers or network streams. ```APIDOC ## NewExtractorFromReader ### Description Creates a new Extractor from an io.ReaderAt interface with a known size. Useful for extracting from in-memory buffers or HTTP responses. ### Parameters - **reader** (io.ReaderAt) - Required - The source reader. - **size** (int64) - Required - Size of the source data. - **chroot** (string) - Required - Directory to extract files into. ``` -------------------------------- ### RegisterCompressor Source: https://context7.com/saracen/fastzip/llms.txt Registers a custom compressor for a specified compression method ID to allow for different compression levels or implementations. ```APIDOC ## RegisterCompressor ### Description Registers a custom compressor for a specified compression method ID. This allows using different compression levels or alternative compression implementations. ### Parameters - **method** (uint16) - Required - The compression method ID (e.g., zip.Deflate). - **compressor** (func) - Required - The compressor implementation (e.g., fastzip.FlateCompressor(9)). ``` -------------------------------- ### RegisterDecompressor Source: https://context7.com/saracen/fastzip/llms.txt Registers a custom decompressor for a specific compression method ID. ```APIDOC ## RegisterDecompressor ### Description Registers a custom decompressor for a specified compression method ID. Allows using alternative decompression implementations or supporting additional compression methods. ### Parameters - **method** (uint16) - Required - The compression method ID. - **decompressor** (zip.Decompressor) - Required - The decompressor implementation. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.