Easy File Compression and Decompression in C# - Make Your Files Smaller!
๐ Introduction: Why Do We Need File Compression?
Imagine you need to send a 1GB file over email, but the email service only allows attachments up to 25MB. What would you do? ๐
That’s where File Compression and Decompression in C# helps! It allows you to reduce file size, making it easier to store, transfer, and manage files efficiently. When needed, you can decompress it back to its original form. ๐
๐ What You Are Going to Learn in This Lesson
โ๏ธ What is File Compression and Decompression in C#
โ๏ธ How to compress files using C#
โ๏ธ How to decompress files back to normal
โ๏ธ Real-world examples with complete code and output
โ๏ธ Why itโs important and when to use it
๐๏ธ How File Compression and Decompression Works in C#
C# provides the GZipStream and DeflateStream classes inside the System.IO.Compression
namespace. These classes help you compress and decompress files efficiently.
Letโs dive into some practical examples!
Example 1: Compress a File Using GZipStream
Code: Compress a File
using System;
using System.IO;
using System.IO.Compression;
class Program
{
static void Main()
{
string sourceFile = "example.txt";
string compressedFile = "example.gz";
CompressFile(sourceFile, compressedFile);
Console.WriteLine("File compressed successfully!");
}
static void CompressFile(string source, string destination)
{
using (FileStream originalFileStream = new FileStream(source, FileMode.Open, FileAccess.Read))
using (FileStream compressedFileStream = new FileStream(destination, FileMode.Create))
using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
{
originalFileStream.CopyTo(compressionStream);
}
}
}
Output:
File compressed successfully!
Explanation:
โ FileStream
opens the file to be compressed.
โ GZipStream
compresses the data while copying it to the new file.
โ The new compressed file will be smaller in size, making storage and transfer easier!
Example 2: Decompress a GZip File
Code: Decompress a File
using System;
using System.IO;
using System.IO.Compression;
class Program
{
static void Main()
{
string compressedFile = "example.gz";
string decompressedFile = "example_decompressed.txt";
DecompressFile(compressedFile, decompressedFile);
Console.WriteLine("File decompressed successfully!");
}
static void DecompressFile(string source, string destination)
{
using (FileStream compressedFileStream = new FileStream(source, FileMode.Open))
using (FileStream decompressedFileStream = new FileStream(destination, FileMode.Create))
using (GZipStream decompressionStream = new GZipStream(compressedFileStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedFileStream);
}
}
}
Output:
File decompressed successfully!
Explanation:
โ Reads the compressed file using GZipStream
.
โ Decompresses it while copying the content to a new file.
โ You get back the original file in its full size!
Example 3: Compress a Folder (Multiple Files) Using ZipArchive
Sometimes, you may need to compress an entire folder instead of a single file. Letโs see how to do that.
Code: Zip a Folder
using System;
using System.IO;
using System.IO.Compression;
class Program
{
static void Main()
{
string sourceFolder = "MyFolder";
string zipFile = "MyFolder.zip";
ZipFile.CreateFromDirectory(sourceFolder, zipFile);
Console.WriteLine("Folder compressed successfully!");
}
}
Explanation:
โ Uses ZipFile.CreateFromDirectory
to compress an entire folder.
โ Creates a ZIP file containing all files from MyFolder
.
โ Super useful for backups, sharing multiple files, or archiving data!
Example 4: Extract a Zip File
Want to extract a ZIP file back to its original form? Hereโs how:
Code: Unzip a Folder
using System;
using System.IO;
using System.IO.Compression;
class Program
{
static void Main()
{
string zipFile = "MyFolder.zip";
string extractPath = "ExtractedFiles";
ZipFile.ExtractToDirectory(zipFile, extractPath);
Console.WriteLine("Files extracted successfully!");
}
}
Explanation:
โ Uses ZipFile.ExtractToDirectory
to unzip all files into a new folder.
โ This makes it easy to restore backups, retrieve archived data, or unzip shared files.
๐ฏ Real-World Scenario: Why Is This Important?
Imagine you run a cloud storage service like Google Drive or Dropbox. Users upload and download files all the time.
- Without compression, transferring large files would be slow and require more bandwidth.
- With compression, files are sent faster, take up less space, and save costs!
When needed, users can decompress the files back to normal. This makes cloud storage, backups, and file transfers much more efficient! ๐
โ Conclusion
We just saw how File Compression and Decompression in C# helps in:
โ Reducing file sizes for faster transfer.
โ Saving storage space by compressing multiple files into one.
โ Improving performance in cloud storage and data backups.
With GZipStream
, DeflateStream
, and ZipFile
, C# makes compression super easy! Try these programs and see how it helps in your projects. ๐
ย
โญ๏ธ Next What?
Now that you know how to compress and decompress files, letโs move to the next step! In the next lesson, youโll learn about Working with Temporary Files in C#โan important feature for handling temporary data. Stay tuned! ๐