|
1 | 1 | --- |
2 | 2 | id: convert-contents-of-rar-or-zip-to-different-formats-and-compress |
3 | 3 | url: conversion/net/convert-contents-of-rar-or-zip-document-to-different-formats-and-compress |
4 | | -title: Convert contents of RAR or ZIP to different formats and compress |
| 4 | +title: Working with Archive Files (ZIP, RAR, 7z, TAR) |
5 | 5 | weight: 12 |
6 | | -description: "Learn how to convert contents of RAR/ZIP archives to a different format based on content type using GroupDocs.Conversion for .NET." |
7 | | -keywords: Convert RAR, Convert ZIP |
| 6 | +description: "Learn three ways to work with archive files: extract and convert contents, repackage archive formats, or convert and re-compress using GroupDocs.Conversion for .NET." |
| 7 | +keywords: Convert RAR, Convert ZIP, Extract ZIP, Convert 7z, Archive conversion, ZIP to 7z, Extract and convert, Archive repackaging |
8 | 8 | productName: GroupDocs.Conversion for .NET |
9 | 9 | hideChildren: False |
| 10 | +toc: True |
10 | 11 | --- |
11 | 12 |
|
12 | | -GroupDocs.Conversion provides a flexible API to control the conversion of archives that contain other documents. |
| 13 | +GroupDocs.Conversion provides flexible options for working with archive files like ZIP, RAR, 7z, and TAR. This guide covers three common workflows for processing archive contents using the FluentConverter API. |
13 | 14 |
|
14 | | -The following code snippet shows how to convert each constituent |
15 | | - file of a RAR archive into a PDF format and then compress them to a single ZIP archive: |
| 15 | +## Prerequisites |
16 | 16 |
|
17 | | -With v24.10 and later: |
| 17 | +The workflows in this guide require the **FluentConverter API** (introduced in v22.1, reworked in v23.6). All examples use the v24.10+ syntax with `SaveContext`. |
| 18 | + |
| 19 | +## Three Ways to Work with Archives |
| 20 | + |
| 21 | +GroupDocs.Conversion supports three distinct workflows for archive files: |
| 22 | + |
| 23 | +| Workflow | What It Does | When to Use | Result | |
| 24 | +|----------|-------------|-------------|--------| |
| 25 | +| **[Workflow 1](#workflow-1-extract-and-convert-to-individual-files)** | Extract and convert each file separately | You need individual converted files, not an archive | Separate files (e.g., doc-1.pdf, doc-2.pdf) | |
| 26 | +| **[Workflow 2](#workflow-2-repackage-archive-format)** | Change archive format without modifying contents | Change compression format for compatibility or size | New archive format (e.g., ZIP → 7z) | |
| 27 | +| **[Workflow 3](#workflow-3-convert-contents-and-re-compress)** | Convert contents AND create new archive | Need converted files packaged in an archive | New archive with converted contents | |
| 28 | + |
| 29 | +## Workflow 1: Extract and Convert to Individual Files |
| 30 | + |
| 31 | +**Use Case:** You have a ZIP file containing multiple Word documents and need each one as a separate PDF file. |
| 32 | + |
| 33 | +This is the most common workflow. Files are extracted from the archive, converted to your target format, and saved as individual files—no re-compression. |
| 34 | + |
| 35 | +### Example: ZIP with DOCX Files → Separate PDFs |
18 | 36 |
|
19 | 37 | ```csharp |
20 | | -FluentConverter.Load("sample.rar") |
21 | | - .ConvertTo((SaveContext saveContext) => new MemoryStream()).WithOptions(new PdfConvertOptions()) |
22 | | - .Compress(new CompressionConvertOptions { Format = CompressionFileType.Zip }).OnCompressionCompleted( |
23 | | - compressedStream => |
24 | | - { |
25 | | - using (var fs = new FileStream("converted.zip", FileMode.Create)) |
26 | | - { |
27 | | - compressedStream.CopyTo(fs); |
28 | | - } |
29 | | - }) |
| 38 | +using GroupDocs.Conversion.Fluent; |
| 39 | +using GroupDocs.Conversion.Options.Convert; |
| 40 | +using System.IO; |
| 41 | + |
| 42 | +// ZIP contains: report.docx, summary.docx, analysis.docx |
| 43 | +FluentConverter.Load("documents.zip") |
| 44 | + .ConvertTo((SaveContext saveContext) => |
| 45 | + { |
| 46 | + // Create separate output file for each document |
| 47 | + string fileName = $"converted-document-{saveContext.PageNumber}.pdf"; |
| 48 | + return File.Create(Path.Combine(@"C:\output", fileName)); |
| 49 | + }) |
| 50 | + .WithOptions(new PdfConvertOptions()) |
30 | 51 | .Convert(); |
| 52 | + |
| 53 | +// Output: converted-document-1.pdf, converted-document-2.pdf, converted-document-3.pdf |
31 | 54 | ``` |
32 | 55 |
|
33 | | -Before v24.10: |
| 56 | +### What Happens |
| 57 | + |
| 58 | +1. **Load:** Opens the archive file |
| 59 | +2. **ConvertTo:** Defines where each converted file should be saved |
| 60 | +3. **WithOptions:** Specifies the target format (PDF in this example) |
| 61 | +4. **Convert:** Executes the conversion |
| 62 | + |
| 63 | +Note: There is no `.Compress()` call, so files are saved individually, not re-compressed into an archive. |
| 64 | + |
| 65 | +### Using Original Filenames |
| 66 | + |
| 67 | +You can preserve original filenames from the archive: |
34 | 68 |
|
35 | 69 | ```csharp |
36 | | -FluentConverter.Load("sample.rar") |
37 | | - .ConvertTo(() => new MemoryStream()).WithOptions(new PdfConvertOptions()) |
38 | | - .Compress(new CompressionConvertOptions { Format = CompressionFileType.Zip }).OnCompressionCompleted( |
39 | | - compressedStream => |
| 70 | +FluentConverter.Load("documents.zip") |
| 71 | + .ConvertTo((SaveContext saveContext) => |
| 72 | + { |
| 73 | + // Use original filename with new extension |
| 74 | + string originalName = saveContext.SourceFileName ?? $"document-{saveContext.PageNumber}"; |
| 75 | + string fileName = Path.ChangeExtension(originalName, ".pdf"); |
| 76 | + return File.Create(Path.Combine(@"C:\output", fileName)); |
| 77 | + }) |
| 78 | + .WithOptions(new PdfConvertOptions()) |
| 79 | + .Convert(); |
| 80 | + |
| 81 | +// Output: report.pdf, summary.pdf, analysis.pdf |
| 82 | +``` |
| 83 | + |
| 84 | +## Workflow 2: Repackage Archive Format |
| 85 | + |
| 86 | +**Use Case:** You have a ZIP file but need it as a 7z archive for better compression, without changing the files inside. |
| 87 | + |
| 88 | +This workflow changes the archive format while preserving the original file contents. No document conversion occurs. |
| 89 | + |
| 90 | +### Example: ZIP → 7z (No Content Conversion) |
| 91 | + |
| 92 | +```csharp |
| 93 | +using GroupDocs.Conversion.Fluent; |
| 94 | +using GroupDocs.Conversion.Options.Convert; |
| 95 | +using GroupDocs.Conversion.FileTypes; |
| 96 | +using System.IO; |
| 97 | + |
| 98 | +FluentConverter.Load("archive.zip") |
| 99 | + .ConvertTo((SaveContext saveContext) => new MemoryStream()) |
| 100 | + .Compress(new CompressionConvertOptions |
| 101 | + { |
| 102 | + Format = CompressionFileType.SevenZ |
| 103 | + }) |
| 104 | + .OnCompressionCompleted(compressedStream => |
| 105 | + { |
| 106 | + using (var fileStream = File.Create("archive.7z")) |
40 | 107 | { |
41 | | - using (var fs = new FileStream("converted.zip", FileMode.Create)) |
42 | | - { |
43 | | - compressedStream.CopyTo(fs); |
44 | | - } |
45 | | - }) |
| 108 | + compressedStream.CopyTo(fileStream); |
| 109 | + } |
| 110 | + }) |
46 | 111 | .Convert(); |
| 112 | + |
| 113 | +// Output: archive.7z containing the same files as the original ZIP |
47 | 114 | ``` |
48 | 115 |
|
| 116 | +### Why Repackage Archives? |
| 117 | + |
| 118 | +- **Better compression:** Convert ZIP to 7z for smaller file sizes |
| 119 | +- **Cross-platform compatibility:** Convert proprietary formats to open standards (e.g., RAR to ZIP) |
| 120 | +- **Standardization:** Consolidate different archive formats to one standard format |
| 121 | +- **Add security:** Repackage with password protection (see [CompressionConvertOptions]({{< ref "conversion/net/developer-guide/advanced-usage/converting/conversion-options-by-document-family/convert-to-compression-with-advanced-options.md" >}}) for details) |
| 122 | + |
| 123 | +### Supported Output Formats |
| 124 | + |
| 125 | +You can repackage archives to these formats: |
| 126 | + |
| 127 | +- **ZIP** - Most widely supported, good compression |
| 128 | +- **7z (SevenZ)** - Best compression ratio, open source |
| 129 | +- **TAR** - Standard on Unix/Linux systems |
| 130 | +- **Others:** TAR.GZ, TAR.BZ2, and more |
| 131 | + |
| 132 | +**Note:** RAR output is not supported due to licensing restrictions. RAR archives can be read as input but not created as output. |
| 133 | + |
| 134 | +## Workflow 3: Convert Contents and Re-compress |
49 | 135 |
|
50 | | -The following code snippet shows how to convert each constituent |
51 | | - file of a ZIP archive to a PDF format and then compress them as password-protected ZIP archive: |
| 136 | +**Use Case:** You have a RAR file containing Word documents and need them converted to PDFs, packaged in a new ZIP archive. |
52 | 137 |
|
53 | | -With v24.10 and later: |
| 138 | +This workflow combines content conversion with re-compression. Files are extracted, converted to a target format, and packaged into a new archive. |
| 139 | + |
| 140 | +### Example: RAR with DOCX → ZIP with PDFs |
54 | 141 |
|
55 | 142 | ```csharp |
56 | | -FluentConverter.Load("sample.zip") |
57 | | - .ConvertTo((SaveContext saveContext) => new MemoryStream()).WithOptions(new PdfConvertOptions()) |
58 | | - .Compress(new CompressionConvertOptions |
59 | | - { |
60 | | - Format = CompressionFileType.Zip, |
61 | | - Password = "123" |
62 | | - }).OnCompressionCompleted( |
63 | | - compressedStream => |
| 143 | +using GroupDocs.Conversion.Fluent; |
| 144 | +using GroupDocs.Conversion.Options.Convert; |
| 145 | +using GroupDocs.Conversion.FileTypes; |
| 146 | +using System.IO; |
| 147 | + |
| 148 | +FluentConverter.Load("documents.rar") |
| 149 | + .ConvertTo((SaveContext saveContext) => new MemoryStream()) |
| 150 | + .WithOptions(new PdfConvertOptions()) |
| 151 | + .Compress(new CompressionConvertOptions |
| 152 | + { |
| 153 | + Format = CompressionFileType.Zip |
| 154 | + }) |
| 155 | + .OnCompressionCompleted(compressedStream => |
| 156 | + { |
| 157 | + using (var fileStream = File.Create("converted-documents.zip")) |
64 | 158 | { |
65 | | - using (var fs = new FileStream("converted.zip", FileMode.Create)) |
66 | | - { |
67 | | - compressedStream.CopyTo(fs); |
68 | | - } |
69 | | - }) |
| 159 | + compressedStream.CopyTo(fileStream); |
| 160 | + } |
| 161 | + }) |
70 | 162 | .Convert(); |
| 163 | + |
| 164 | +// Output: converted-documents.zip containing PDFs |
71 | 165 | ``` |
72 | 166 |
|
73 | | -Before v24.10: |
| 167 | +### Password Protection Example |
| 168 | + |
| 169 | +Create a password-protected archive: |
74 | 170 |
|
75 | 171 | ```csharp |
76 | | -FluentConverter.Load("sample.zip") |
77 | | - .ConvertTo(() => new MemoryStream()).WithOptions(new PdfConvertOptions()) |
78 | | - .Compress(new CompressionConvertOptions |
79 | | - { |
| 172 | +FluentConverter.Load("sensitive-files.zip") |
| 173 | + .ConvertTo((SaveContext saveContext) => new MemoryStream()) |
| 174 | + .WithOptions(new PdfConvertOptions()) |
| 175 | + .Compress(new CompressionConvertOptions |
| 176 | + { |
80 | 177 | Format = CompressionFileType.Zip, |
81 | | - Password = "123" |
82 | | - }).OnCompressionCompleted( |
83 | | - compressedStream => |
| 178 | + Password = "SecurePassword123" |
| 179 | + }) |
| 180 | + .OnCompressionCompleted(compressedStream => |
| 181 | + { |
| 182 | + using (var fileStream = File.Create("protected-archive.zip")) |
84 | 183 | { |
85 | | - using (var fs = new FileStream("converted.zip", FileMode.Create)) |
86 | | - { |
87 | | - compressedStream.CopyTo(fs); |
88 | | - } |
89 | | - }) |
| 184 | + compressedStream.CopyTo(fileStream); |
| 185 | + } |
| 186 | + }) |
90 | 187 | .Convert(); |
91 | 188 | ``` |
| 189 | + |
| 190 | +For more advanced options including different compression formats, optimization settings, and best practices, see [Convert to Compression with advanced options]({{< ref "conversion/net/developer-guide/advanced-usage/converting/conversion-options-by-document-family/convert-to-compression-with-advanced-options.md" >}}). |
| 191 | + |
| 192 | +## Choosing the Right Workflow |
| 193 | + |
| 194 | +Use this decision guide to select the appropriate workflow: |
| 195 | + |
| 196 | +| Your Goal | Workflow | Key API Methods | |
| 197 | +|-----------|----------|-----------------| |
| 198 | +| Extract and convert each file separately | **Workflow 1** | `Load()` → `ConvertTo()` → `WithOptions()` → `Convert()` | |
| 199 | +| Change archive format only (no conversion) | **Workflow 2** | `Load()` → `ConvertTo()` → `Compress()` → `Convert()` | |
| 200 | +| Convert contents AND create new archive | **Workflow 3** | `Load()` → `ConvertTo()` → `WithOptions()` → `Compress()` → `Convert()` | |
| 201 | + |
| 202 | +**Quick Tips:** |
| 203 | + |
| 204 | +- **No `.Compress()`** = Individual files (Workflow 1) |
| 205 | +- **No `.WithOptions()`** = Archive format change only (Workflow 2) |
| 206 | +- **Both `.WithOptions()` and `.Compress()`** = Convert and re-compress (Workflow 3) |
| 207 | + |
| 208 | +## Supported Archive Formats |
| 209 | + |
| 210 | +### Input Formats (Read) |
| 211 | + |
| 212 | +GroupDocs.Conversion can read from these archive formats: |
| 213 | + |
| 214 | +- ZIP, RAR, 7z (7-Zip) |
| 215 | +- TAR, TAR.GZ, TAR.BZ2, TAR.XZ |
| 216 | +- CAB, LZ, CPIO, ISO |
| 217 | +- And more |
| 218 | + |
| 219 | +### Output Formats (Write) |
| 220 | + |
| 221 | +You can create these archive formats: |
| 222 | + |
| 223 | +- ZIP (most compatible) |
| 224 | +- 7z/SevenZ (best compression) |
| 225 | +- TAR and compressed TAR variants |
| 226 | +- Other open formats |
| 227 | + |
| 228 | +**Important:** RAR archives can only be read, not created (licensing restrictions). |
| 229 | + |
| 230 | +## Version Compatibility |
| 231 | + |
| 232 | +All examples in this guide use **v24.10+ syntax** with `SaveContext`: |
| 233 | + |
| 234 | +```csharp |
| 235 | +.ConvertTo((SaveContext saveContext) => ...) |
| 236 | +``` |
| 237 | + |
| 238 | +If you're using a version **before v24.10**, use the older syntax without `SaveContext`: |
| 239 | + |
| 240 | +```csharp |
| 241 | +.ConvertTo(() => ...) |
| 242 | +``` |
| 243 | + |
| 244 | +The rest of the code remains the same. |
| 245 | + |
| 246 | +## See Also |
| 247 | + |
| 248 | +- [Convert to Compression with advanced options]({{< ref "conversion/net/developer-guide/advanced-usage/converting/conversion-options-by-document-family/convert-to-compression-with-advanced-options.md" >}}) - Complete API reference for CompressionConvertOptions |
| 249 | +- [Common Conversion Options]({{< ref "conversion/net/developer-guide/advanced-usage/converting/common-conversion-options/_index.md" >}}) - Watermarks, page selection, and more |
| 250 | +- [FluentConverter API Reference](https://reference.groupdocs.com/conversion/net/groupdocs.conversion.fluent/fluentconverter/) - Complete API documentation |
0 commit comments