0

I have an archive in the Zlib format. I am trying to decompress it using C#. The archive contains one file and a folder full of more files and folders. So far, I have been able to decompress the single, top-level file. However, the directory hasn't magically appeared and I don't even know where to start with it. So far, I have successfully decompressed the top-level file with DeflateStream. I have DotNetZip and am playing with it but I don't know how to create a folder full of files from a byte array returned by the decompression function found here.

sirdank
  • 3,351
  • 3
  • 25
  • 58
  • Is there a reason you can't use the `ZipArchive` class that comes with .NET 4.5? Post some code and we'll help! – Cory Nelson Apr 15 '14 at 23:28
  • I hadn't heard of it. However, I tried using the code from [MSDN](http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-3) and got an InvalidDataException with the message `End of Central Directory record could not be found`. Is that class suitable for decompressing ZLib? – sirdank Apr 15 '14 at 23:51
  • There's no such thing as a "ZLib archive". Do you mean zip? gz? tar.gz? – Cory Nelson Apr 15 '14 at 23:57
  • If you can convince me of that, it will have significant impact on my project. The file is a proprietary file format that is created in Delphi using the default ZLib compression. It has a ZLib header and skipping the header allows the top-level file to be decompressed using DeflateStream. That's as far as I've gotten. Everything else I try results in an error from the implementation. – sirdank Apr 16 '14 at 00:04
  • @CoryNelson There is no such thing as "ZipArchive" in .NET 4.5, DeflateStream and GZipStream only. – Kim Schneider Jul 21 '17 at 08:36
  • 1
    @KimSchneider [ZipArchive](https://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive%28v=vs.110%29.aspx) – Cory Nelson Jul 21 '17 at 18:06
  • @CoryNelson I missed the reference to System.IO.Compression :D – Kim Schneider Jul 28 '17 at 07:43

1 Answers1

0

This should work:

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
   foreach (ZipEntry e in zip)
   {
      e.Extract(TargetDirectory, true);  // overwrite existing files
   }
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • I can't even get that to compile. Is that code using the `System.IO.Compression namespace`, `Ionic.ZLib`, or `Ionic.Zip`? – sirdank Apr 15 '14 at 23:49
  • The ZipFile class is declared in the System.IO.Compression namespace. It requires the v4.5 of the framework. https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile(v=vs.110).aspx – Timothy Klenke Sep 01 '15 at 20:52