Create BZIP2
BZIP2, short for 'Burrows-Wheeler Block Sort Text Compressor,' is a file compression utility used in Unix and Linux systems. It efficiently reduces file sizes using the BZIP2 compression algorithm, commonly resulting in smaller files with the '.bz2' extension. It is favored for its high compression efficiency and is often used for software distribution and data archiving.
BZIP2 is designed to compress single files and does not serve as a file archiver, which is a software utility used for combining multiple files and directories into a single archive file. BZIP2 is particularly efficient at compressing text data, and decompression is relatively fast.
Below is an example code snippet demonstrating how to use BZIP2 in a programming context. The resulting BZIP2 file must have both the file extension and '.bz2.' This is crucial for later decompressing the file back to a text file:
using IronZip;
using System;
class CreateBzip2Example
{
static void Main()
{
string inputPath = @"sample.txt";
string outputPath = @"sample.txt.bz2";
// Use IronBZip2Archive in a using block to ensure disposal
using (var archive = new IronBZip2Archive())
{
archive.Add(inputPath); // Add the file to compress
archive.SaveAs(outputPath); // Write the .bz2 compressed file
}
Console.WriteLine($"File '{inputPath}' has been compressed to '{outputPath}'");
}
}
using IronZip;
using System;
class CreateBzip2Example
{
static void Main()
{
string inputPath = @"sample.txt";
string outputPath = @"sample.txt.bz2";
// Use IronBZip2Archive in a using block to ensure disposal
using (var archive = new IronBZip2Archive())
{
archive.Add(inputPath); // Add the file to compress
archive.SaveAs(outputPath); // Write the .bz2 compressed file
}
Console.WriteLine($"File '{inputPath}' has been compressed to '{outputPath}'");
}
}
Imports IronZip
Imports System
Friend Class CreateBzip2Example
Shared Sub Main()
Dim inputPath As String = "sample.txt"
Dim outputPath As String = "sample.txt.bz2"
' Use IronBZip2Archive in a using block to ensure disposal
Using archive = New IronBZip2Archive()
archive.Add(inputPath) ' Add the file to compress
archive.SaveAs(outputPath) ' Write the .bz2 compressed file
End Using
Console.WriteLine($"File '{inputPath}' has been compressed to '{outputPath}'")
End Sub
End Class
Explanation of the Code
- Creating the compressor:
new IronBZip2Archive()
builds an empty BZIP2 archive instance. You can also useIronBZip2Archive.FromFile(...)
or.FromFiles(...)
for single or batch file initialization - Adding files:
archive.Add(inputPath)
adds the specified file (e.g.sample.txt
) into the archive. Note that BZIP2 compresses only one file; adding multiple files would instead wrap them in nested archives rather than a single.bz2
stream Wikipedia. Saving:
archive.SaveAs(outputPath)
writes out the compressed BZIP2 file. Make sure theoutputPath
includes the.bz2
extension. IronZIP ensures correct format output for later decompression- Resource management:
Enclosing in ausing
block guaranteesDispose()
is called automatically, releasing any underlying resources safely Iron Software.
Batch Compression
using IronZip;
using System;
class BatchBzip2Example
{
static void Main()
{
var files = new[] { "a.txt", "b.txt" };
using var archive = IronBZip2Archive.FromFiles(files);
archive.SaveAs("batch_output.bz2");
Console.WriteLine("Multiple files compressed into a single BZIP2 file");
}
}
using IronZip;
using System;
class BatchBzip2Example
{
static void Main()
{
var files = new[] { "a.txt", "b.txt" };
using var archive = IronBZip2Archive.FromFiles(files);
archive.SaveAs("batch_output.bz2");
Console.WriteLine("Multiple files compressed into a single BZIP2 file");
}
}
Imports IronZip
Imports System
Friend Class BatchBzip2Example
Shared Sub Main()
Dim files = { "a.txt", "b.txt" }
Dim archive = IronBZip2Archive.FromFiles(files)
archive.SaveAs("batch_output.bz2")
Console.WriteLine("Multiple files compressed into a single BZIP2 file")
End Sub
End Class