Reputation: 185
I tried DotNetZip、SharpZipLib even use cmd to command rar.exe
but it's does'nt work
and the code is I reference ICSharpCode.SharpZipLib.GZip、ICSharpCode.SharpZipLib.Tar to do
FileInfo tarFileInfo = new FileInfo(@"D:\*.tar.Z");
using (Stream sourceStream = new GZipInputStream(tarFileInfo.OpenRead()))
{
using (TarArchive tarArchive = TarArchive.CreateInputTarArchive(sourceStream, TarBuffer.DefaultBlockFactor))
{
tarArchive.ExtractContents(targetDirectory.FullName);
}
}
I'll get error message like this:
ICSharpCode.SharpZipLib.GZip.GZipException: 'Error GZIP header, second magic byte doesn't match
so how to fix it and success to unzip?
Upvotes: 0
Views: 945
Reputation: 131180
.z
uses LZW, a completely different algorithm than those used in .zip
or .gz
files. SharpZipLib supports LZW through the ICSharpCode.SharpZipLib.Lzw
namespace. The namespaces used for each algorithm are shown in the repo's landing page, in the Namespace Layout section.
You can check the LZW unit test file although the code is essentially the same as that used for other formats, eg:
using (var inStream = new LzwInputStream(File.OpenRead(@"D:\*.tar.Z")))
{
using (TarArchive tarArchive = TarArchive.CreateInputTarArchive(inStream, TarBuffer.DefaultBlockFactor))
{
tarArchive.ExtractContents(targetDirectory.FullName);
}
}
According to Wikipedia it's possible that the compressed file uses DEFLATE instead of LZW. In this case you'd have to use InflaterInputStream
instead of LzwInputStream
.
The TAR sample in SharpZipLib's repo uses InflaterInputStream, GZipInputStream or BZip2InputStream based on a switch
Upvotes: 2