0

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.

Tommek
  • 11
  • 4
  • Since it's a compressed file, it seems highly unlikely to extract anything from it without decrompessing first. Maybe [this](http://stackoverflow.com/questions/14774401/how-to-list-the-first-or-last-10-lines-from-a-file-without-decompressing-it-in-l) helps. – Melvin Jun 09 '15 at 08:39
  • if you open manually with winrar and copy the file from there, it is not decompressed as well or is it? – Tommek Jun 09 '15 at 08:41

1 Answers1

0

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);
            }
        }
    }
}
Tommek
  • 11
  • 4