简体   繁体   English

如何使用SharpZipLib提取任何档案?

[英]How do I extract any archives with SharpZipLib?

I'm using SharpZipLib to extract archives. 我正在使用SharpZipLib提取档案。 I managed to extract .zip archives: 我设法提取.zip存档:

FastZip fastZip = new FastZip();

fastZip.ExtractZip(file, directory, null);

and to extract .tar.gz: 并解压缩.tar.gz:

// Use a 4K buffer. Any larger is a waste.    
byte[] dataBuffer = new byte[4096];

using (Stream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
{
    using (GZipInputStream gzipStream = new GZipInputStream(fileStream))
    {
        // Change this to your needs
        string fnOut = Path.Combine(directory, Path.GetFileNameWithoutExtension(file));

        using (FileStream fsOut = File.Create(fnOut))
        {
            StreamUtils.Copy(gzipStream, fsOut, dataBuffer);
        }
    }
}

Is there also a way to extract any kind of archive where I don't need to know the type of archive upfront? 还有一种方法可以提取不需要我预先知道存档类型的任何存档吗? (eg SharpZipLib.ExtractAnyArchive(file, directory) ) (例如SharpZipLib.ExtractAnyArchive(file, directory)

SharpZipLib unfortunately is currently not able to auto-detect the format of an archive file/stream. 遗憾, SharpZipLib目前无法自动检测存档文件/流的格式。

You either have to implement the functionality by yourself in some form, or seek an alternative library that is able to auto-detect the format of an archive. 您要么必须自己以某种形式来实现该功能,要么寻找能够自动检测档案格式的替代库。 An example of such a library would be SharpCompress , however, as you already noted in the comments, different libraries can come with different kind of limitations and bugs that might affect the functionality of your software. 这样的库的一个示例是SharpCompress ,但是,正如您在注释中已经提到的那样,不同的库可能会有不同类型的限制和错误,可能会影响软件的功能。

If you decide to roll your own auto-detection functionality for SharpZipLib , you can choose different approaches, like 如果您决定为SharpZipLib推出自己的自动检测功能,则可以选择其他方法,例如

  • Try opening an (unknown) archive using the archive (reader/stream) classes for every archive format supported by SharpZipLib , until you find one which can open and process the archive file successfully. 尝试使用档案(阅读器/流)类为SharpZipLib支持的每种档案格式打开(未知)档案,直到找到可以成功打开和处理档案文件的档案。

  • Implement some format detection routine that scans an archive file/stream for 'magic' signature bytes identifying a particular archive format. 实现某种格式检测例程,该例程会在存档文件/流中扫描标识特定存档格式的“魔术”签名字节。 If the format of an archive file/stream has been thus identified, select and use the appropriate SharpZipLib classes for handling the detected archive format. 如果由此确定了存档文件/流的格式,请选择并使用适当的SharpZipLib类来处理检测到的存档格式。

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

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