C# How to get file/ copy file from a bzip2 (.bz2) file without extracting the file

870 views Asked by At

I have a .bz2 compressed file, and i want to copy the inside file to another location, without decompressing it. I use .net 4.5 with C#.

i tried like this, but this is for zip files (.zip):

using (var zip = ZipFile.Read(_targetPathComplete + "\\" + file[0].ToUpper() + "_" + file[1].ToUpper() + ".bz2"))
{
    Stream s = zip[file[0].ToUpper() + "_" + file[1].ToUpper()].OpenReader();
    // fiddle with stream here

    using (var fileStream = File.Create(_targetPathComplete + "\\" + file[0].ToUpper() + "_" + file[1].ToUpper() + ".HDC"))
    {
        s.Seek(0, SeekOrigin.Begin);
        s.CopyTo(fileStream);
    }
}

Or compress a file with bzip2 algorithm and give an extension .HDC to it.

1

There are 1 answers

0
Tommek On

I think i solved it like this, at least the file i have wit this method is the same as when i copy the file from winrar.

var fname = _targetPathComplete + "\\" + file[0].ToUpper() + "_" + file[1].ToUpper() + ".bz2";
using (var fs = File.OpenRead(fname))
{
    using (var decompressor = new Ionic.BZip2.BZip2InputStream(fs))
    {
        var outFname = _targetPathComplete + "\\" + file[0].ToUpper() + "_" + file[1].ToUpper() + ".HDC";
        using (var output = File.Create(outFname))
        {
            var buffer = new byte[2048];
            int n;
            while ((n = decompressor.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, n);
            }
        }
    }
}