简体   繁体   English

C#如何在不提取文件的情况下从bzip2(.bz2)文件获取文件/复制文件

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

I have a .bz2 compressed file, and i want to copy the inside file to another location, without decompressing it. 我有一个.bz2压缩文件,并且我想将内部文件复制到另一个位置而不进行解压缩。 I use .net 4.5 with C#. 我在C#中使用.net 4.5。

i tried like this, but this is for zip files (.zip): 我尝试过这样,但这是针对zip文件(.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. 或使用bzip2算法压缩文件,然后给文件扩展名.HDC。

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. 我认为我是这样解决的,至少我使用此方法的文件与从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);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM