0

I have zipping code in PowerShell and I need to create method to do the same in C# but I faced the issue that the zipped file I have without extension so I do not know if its .zip .tar or .tgz

The powershell code is as following :

Expand-Archive -Path $compressedFile.FullName -OutputPath $workDir -ShowProgress 

So Can you help me to implement it in C# ?

Mohammed Thabet
  • 21,387
  • 7
  • 27
  • 43

1 Answers1

2

For detecting compression formats, see : Detecting Compression Formats

You can determine that it is likely to be one of those formats by looking at the first few bytes. You should then test to see if it really is one of those, using an integrity check from the associated utility for that format, or by actually proceeding to decompress.

You can find the header formats in the descriptions:

Zip (.zip) format description, starts with 0x50, 0x4b, 0x03, 0x04 (unless empty — then the last two are 0x05, 0x06 or 0x06, 0x06) Gzip (.gz) format description, starts with 0x1f, 0x8b, 0x08 xz (.xz) format description, starts with 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00 Others:

zlib (.zz) format description, starts with (in bits) 0aaa1000 bbbccccc, where ccccc is chosen so that the first byte times 256 plus the second byte is a multiple of 31. compress (.Z) starts with 0x1f, 0x9d bzip2 (.bz2) starts with 0x42, 0x5a, 0x68

For unziping files in C#, see: Uncompressing Files in .NET

We have used SharpZipLib successfully on many projects. I know it's a third party tool, but source code is included and could provide some insight if you chose to reinvent the wheel here.

There are a few different answers on the above link that may provide a better path depending on your needs.

Community
  • 1
  • 1
Brosto
  • 4,445
  • 2
  • 34
  • 51
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Dylan Corriveau Aug 12 '15 at 17:02
  • 1
    Dylan - Fair point. Both links were from posts from StackOverflow that are several years old and are probably safe from becoming invalid. None the less, I'll update the answer with the answers from those previous posts. – Brosto Aug 12 '15 at 19:05