0

I'm using SharpZipLib to extract archives. I managed to extract .zip archives:

FastZip fastZip = new FastZip();

fastZip.ExtractZip(file, directory, null);

and to extract .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? (e.g. SharpZipLib.ExtractAnyArchive(file, directory))

Mathias
  • 1,819
  • 4
  • 22
  • 34
  • 1
    If you are required to use SharpZipLib, you are out of luck. You would then need to "manually" try opening every archive format type supported by SharpZipLib until you find one that does succeed in processing the file; or you would need to implement format detection by yourself. Otherwise, if you are not forced to use SharpZipLib specifically, you might look for an alternative library such as SharpCompress, which is able to auto-detect supported archive types (see here for some simple examples: https://github.com/adamhathcock/sharpcompress/wiki/API-Examples). –  Dec 03 '18 at 13:26
  • Actually I changed from SharpCompress to SharpZipLib beacause SharpCompress does not properly create tar archives. Anyway this is something I have to deal with. Thank you very much! If you post it as an answer I will accept it. – Mathias Dec 03 '18 at 13:52
  • Uh, sorry to hear that SharpCompress made problems for you. I have to admit that i don't use SharpCompress for quite some years anymore. I recall needing 7z support at one time, and SharpCompress' 7z support was rather lackluster at that time. Other libraries promising 7z support were even worse or outdated then, so i was forced to interface with the 7-Zip DLL directly. Since then, using the 7-Zip DLL has always covered my needs (it also supports multiple archive formats and is able to detect formats of archive files/streams). –  Dec 03 '18 at 14:13

1 Answers1

1

SharpZipLib unfortunately is currently not able to auto-detect the format of an archive file/stream.

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.

If you decide to roll your own auto-detection functionality for SharpZipLib, you can choose different approaches, like

  • 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.

  • 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.