|
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: Extract and Convert Archive Contents |
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 how to extract files from archives (ZIP, RAR, 7z, TAR) and convert them to different formats using GroupDocs.Conversion for .NET." |
| 7 | +keywords: Extract ZIP, Convert ZIP contents, Extract RAR, Convert archive files, Extract and convert, Archive extraction |
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 can extract files from archives (ZIP, RAR, 7z, TAR) and convert each file to a different format. This guide covers two workflows for processing archive contents. |
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 | +**To convert archive formats** (ZIP to 7z, etc.) **without extracting contents**, see [Convert Archive Formats]({{< ref "conversion/net/developer-guide/advanced-usage/converting/conversion-options-by-document-family/convert-to-compression-with-advanced-options.md" >}}). |
16 | 16 |
|
17 | | -With v24.10 and later: |
| 17 | +## Two Content Extraction Workflows |
| 18 | + |
| 19 | +| Workflow | What It Does | Result | |
| 20 | +|----------|-------------|--------| |
| 21 | +| **[Workflow 1](#workflow-1-extract-and-convert-to-individual-files)** | Extract and convert each file separately | Individual converted files (e.g., doc-1.pdf, doc-2.pdf) | |
| 22 | +| **[Workflow 2](#workflow-2-convert-contents-and-re-compress)** | Convert contents AND create new archive | New archive with converted contents | |
| 23 | + |
| 24 | +## Workflow 1: Extract and Convert to Individual Files |
| 25 | + |
| 26 | +Extract files from an archive and convert each one separately (no re-compression). |
18 | 27 |
|
19 | 28 | ```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 | | - }) |
| 29 | +using GroupDocs.Conversion.Fluent; |
| 30 | +using GroupDocs.Conversion.Options.Convert; |
| 31 | +using GroupDocs.Conversion.Contracts; |
| 32 | +using System.IO; |
| 33 | + |
| 34 | +// ZIP contains: report.docx, summary.docx, analysis.docx |
| 35 | +FluentConverter.Load("documents.zip") |
| 36 | + .ConvertTo((SaveContext saveContext) => |
| 37 | + { |
| 38 | + // Create separate output file for each document |
| 39 | + string fileName = $"converted-document-{saveContext.ItemIndex}.pdf"; |
| 40 | + return File.Create(Path.Combine(@"C:\output", fileName)); |
| 41 | + }) |
| 42 | + .WithOptions(new PdfConvertOptions()) |
30 | 43 | .Convert(); |
| 44 | + |
| 45 | +// Output: converted-document-1.pdf, converted-document-2.pdf, converted-document-3.pdf |
31 | 46 | ``` |
32 | 47 |
|
33 | | -Before v24.10: |
| 48 | +**Using original filenames:** |
34 | 49 |
|
35 | 50 | ```csharp |
36 | | -FluentConverter.Load("sample.rar") |
37 | | - .ConvertTo(() => new MemoryStream()).WithOptions(new PdfConvertOptions()) |
38 | | - .Compress(new CompressionConvertOptions { Format = CompressionFileType.Zip }).OnCompressionCompleted( |
39 | | - compressedStream => |
40 | | - { |
41 | | - using (var fs = new FileStream("converted.zip", FileMode.Create)) |
42 | | - { |
43 | | - compressedStream.CopyTo(fs); |
44 | | - } |
45 | | - }) |
| 51 | +FluentConverter.Load("documents.zip") |
| 52 | + .ConvertTo((SaveContext saveContext) => |
| 53 | + { |
| 54 | + string originalName = saveContext.SourceFileName ?? $"document-{saveContext.ItemIndex}"; |
| 55 | + string fileName = Path.ChangeExtension(originalName, ".pdf"); |
| 56 | + return File.Create(Path.Combine(@"C:\output", fileName)); |
| 57 | + }) |
| 58 | + .WithOptions(new PdfConvertOptions()) |
46 | 59 | .Convert(); |
47 | | -``` |
48 | 60 |
|
| 61 | +// Output: report.pdf, summary.pdf, analysis.pdf |
| 62 | +``` |
49 | 63 |
|
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: |
| 64 | +## Workflow 2: Convert Contents and Re-compress |
52 | 65 |
|
53 | | -With v24.10 and later: |
| 66 | +Extract files, convert them, and package into a new archive: |
54 | 67 |
|
55 | 68 | ```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 => |
| 69 | +using GroupDocs.Conversion.Fluent; |
| 70 | +using GroupDocs.Conversion.Options.Convert; |
| 71 | +using GroupDocs.Conversion.Contracts; |
| 72 | +using GroupDocs.Conversion.FileTypes; |
| 73 | +using System.IO; |
| 74 | + |
| 75 | +FluentConverter.Load("documents.rar") |
| 76 | + .ConvertTo((SaveContext saveContext) => new MemoryStream()) |
| 77 | + .WithOptions(new PdfConvertOptions()) |
| 78 | + .Compress(new CompressionConvertOptions |
| 79 | + { |
| 80 | + Format = CompressionFileType.Zip |
| 81 | + }) |
| 82 | + .OnCompressionCompleted(compressedStream => |
| 83 | + { |
| 84 | + using (var fileStream = File.Create("converted-documents.zip")) |
64 | 85 | { |
65 | | - using (var fs = new FileStream("converted.zip", FileMode.Create)) |
66 | | - { |
67 | | - compressedStream.CopyTo(fs); |
68 | | - } |
69 | | - }) |
| 86 | + compressedStream.CopyTo(fileStream); |
| 87 | + } |
| 88 | + }) |
70 | 89 | .Convert(); |
| 90 | + |
| 91 | +// Output: converted-documents.zip containing PDFs |
71 | 92 | ``` |
72 | 93 |
|
73 | | -Before v24.10: |
| 94 | +**With password protection:** |
74 | 95 |
|
75 | 96 | ```csharp |
76 | | -FluentConverter.Load("sample.zip") |
77 | | - .ConvertTo(() => new MemoryStream()).WithOptions(new PdfConvertOptions()) |
78 | | - .Compress(new CompressionConvertOptions |
79 | | - { |
| 97 | +FluentConverter.Load("sensitive-files.zip") |
| 98 | + .ConvertTo((SaveContext saveContext) => new MemoryStream()) |
| 99 | + .WithOptions(new PdfConvertOptions()) |
| 100 | + .Compress(new CompressionConvertOptions |
| 101 | + { |
80 | 102 | Format = CompressionFileType.Zip, |
81 | | - Password = "123" |
82 | | - }).OnCompressionCompleted( |
83 | | - compressedStream => |
| 103 | + Password = "SecurePassword123" |
| 104 | + }) |
| 105 | + .OnCompressionCompleted(compressedStream => |
| 106 | + { |
| 107 | + using (var fileStream = File.Create("protected-archive.zip")) |
84 | 108 | { |
85 | | - using (var fs = new FileStream("converted.zip", FileMode.Create)) |
86 | | - { |
87 | | - compressedStream.CopyTo(fs); |
88 | | - } |
89 | | - }) |
| 109 | + compressedStream.CopyTo(fileStream); |
| 110 | + } |
| 111 | + }) |
90 | 112 | .Convert(); |
91 | 113 | ``` |
| 114 | + |
| 115 | +## Choosing the Right Workflow |
| 116 | + |
| 117 | +| Your Goal | Use | |
| 118 | +|-----------|-----| |
| 119 | +| Extract and convert each file separately | **Workflow 1** | |
| 120 | +| Convert contents AND create new archive | **Workflow 2** | |
| 121 | +| Change archive format only (no conversion) | See [Convert Archive Formats]({{< ref "conversion/net/developer-guide/advanced-usage/converting/conversion-options-by-document-family/convert-to-compression-with-advanced-options.md" >}}) | |
| 122 | + |
| 123 | +## Supported Archive Formats |
| 124 | + |
| 125 | +**Input (read):** ZIP, RAR, 7z, TAR, TAR.GZ, TAR.BZ2, TAR.XZ, CAB, LZ, CPIO, ISO |
| 126 | + |
| 127 | +**Output (write):** ZIP, 7z, TAR, TAR.GZ, TAR.BZ2, TAR.XZ (RAR output not supported due to licensing) |
| 128 | + |
| 129 | +## Version Compatibility |
| 130 | + |
| 131 | +All examples use **v24.10+ syntax** with `SaveContext`: |
| 132 | + |
| 133 | +```csharp |
| 134 | +.ConvertTo((SaveContext saveContext) => ...) |
| 135 | +``` |
| 136 | + |
| 137 | +For versions **before v24.10**, use: |
| 138 | + |
| 139 | +```csharp |
| 140 | +.ConvertTo(() => ...) |
| 141 | +``` |
| 142 | + |
| 143 | +## See Also |
| 144 | + |
| 145 | +- [Convert Archive Formats (ZIP, 7z, TAR, RAR)]({{< ref "conversion/net/developer-guide/advanced-usage/converting/conversion-options-by-document-family/convert-to-compression-with-advanced-options.md" >}}) - Change archive format without extracting contents |
| 146 | +- [Common Conversion Options]({{< ref "conversion/net/developer-guide/advanced-usage/converting/common-conversion-options/_index.md" >}}) |
| 147 | +- [FluentConverter API Reference](https://reference.groupdocs.com/conversion/net/groupdocs.conversion.fluent/fluentconverter/) |
0 commit comments