File compression is the process of reducing the size of one or more files. It shrinks big files into much smaller ones by removing unneeded data. The compressed file archive makes it easier to send and back up large files or groups of files. Moreover, such files make downloading faster and easier, besides, they allow more data to be stored on removable media. There are various compression formats. Below, we are going to describe how to convert the most popular compression file formats using GroupDocs.Conversion.
With GroupDocs.Conversion you can easily extract content from your archives. For example, a code snippet of extraction from a ZIP archive will look like this:
With v24.10 and later:
// Load the source ZIP fileusing(Converterconverter=newConverter("sample.zip")){converter.Convert(_=>null,(ConvertedContextconvertedContext)=>{// store extracted contentstringfileName=Path.Combine(outputFolder,convertedContext.SourceFileName);Directory.CreateDirectory(Path.GetDirectoryName(fileName)!);using(varfs=newFileStream(fileName,FileMode.Create)){convertedContext.ConvertedStream.CopyTo(fs);}});}
Before v24.10:
// Load the source ZIP fileusing(Converterconverter=newConverter("sample.zip")){converter.Convert(()=>newMemoryStream(),(stringsourceFileName,FileTypetargetFormat,StreamconvertedStream)=>{// store extracted contentstringfileName=Path.Combine(outputFolder,sourceFileName);Directory.CreateDirectory(Path.GetDirectoryName(fileName)!);using(varfs=newFileStream(fileName,FileMode.Create)){convertedStream.CopyTo(fs);}},(_,_)=>null);}
Put it simply - you just load a ZIP file into the Converter class, provide null convert options and a handler in which to store the result and GroupDocs.Conversion does all the rest.
Note
Refer to the API reference for more conversion options and customizations.
Extract and convert from a RAR
You also can convert the archive content during extraction to a desired format. Extraction and conversion to PDF format are also quite simple and natural.
The following code snippet shows how to convert the content of a RAR archive to PDF in C# using GroupDocs.Conversion.
With v24.10 and later:
// Load the source RAR fileusing(Converterconverter=newConverter("sample.rar")){// Set the convert option for PDF format. This will convert each file in the archive to PDFPdfConvertOptionsoptions=newPdfConvertOptions();inti=0;// Extract and convert to PDFconverter.Convert((SaveContextsaveContext)=>newFileStream(Path.Combine(outputFolder,$"converted-{++i}.{saveContext.TargetFormat.Extension}"),FileMode.Create),options);}
Before v24.10:
// Load the source RAR fileusing(Converterconverter=newConverter("sample.rar")){// Set the convert option for PDF format. This will convert each file in the archive to PDFPdfConvertOptionsoptions=newPdfConvertOptions();inti=0;// Extract and convert to PDFconverter.Convert(()=>newFileStream(Path.Combine(outputFolder,$"converted-{++i}.pdf"),FileMode.Create),options);}
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.