C# SevenZipSharp无法打开某些tar档案

C# SevenZipSharp无法打开某些tar档案,c#,archive,7zip,C#,Archive,7zip,我使用SevenZipSharp打包到7z档案,并从各种档案中解包。多年来,它一直运作良好 今天我有一个.tgz归档文件在第二阶段解包失败: 从.tgz提取.tar有效,但解压缩.tar失败。受影响的只是这一个归档文件。所有其他的。tgz工作得很好。.tar本身没有问题,因为使用7-zip软件解包也可以工作。经过大量测试,我和一位同事找到了原因: 我们必须调试SevenZipSharp DLL以找到其中的故障。DLL通过读取前16个字节并将其与签名列表进行比较来检测归档文件的类型。这对于大多数类

我使用SevenZipSharp打包到7z档案,并从各种档案中解包。多年来,它一直运作良好

今天我有一个.tgz归档文件在第二阶段解包失败:

从.tgz提取.tar有效,但解压缩.tar失败。受影响的只是这一个归档文件。所有其他的。tgz工作得很好。.tar本身没有问题,因为使用7-zip软件解包也可以工作。

经过大量测试,我和一位同事找到了原因:
我们必须调试SevenZipSharp DLL以找到其中的故障。DLL通过读取前16个字节并将其与签名列表进行比较来检测归档文件的类型。这对于大多数类型的存档是正确的,但对于.tar存档是错误的,因为.tar文件头以存档的文件名开头:。签名“ustar”(如果存在)位于地址257(0x0101)

SevenZipSharp知道这一点,并在该地址检查“ustar”,但前提是之前的检测失败。不幸的是,我们的TAR档案名为“x42202.TAR”。.dmg文件()的头由一个“x”组成(多么愚蠢,只使用一个字节作为签名??)。因此,实际上成功地检测到了文件类型,只是检测结果是错误的。
(我知道,链接的维基百科说,.dmg头签名是“koly”,但我通过在互联网上找到的下载的.dmg文件确认了这一点。)

因此,我们修改了
FileSignatureChecker.cs
中的代码,以避免.tar存档的错误存档类型检测。
下面是原始代码和修改后的代码。
代码库是可在中找到的最新SevenZipSharp版本。很明显,它不再处于活动开发中,因为版本号多年来没有改变,如果它仍然处于活动状态,它将在CodePlex退役后移动

更新2018-11-16
修改代码中的错误修复:如果找到,则未返回enSpecialFormat

原始代码

public static InArchiveFormat CheckSignature (Stream stream, out int offset, out bool isExecutable)
{
  offset = 0;
  if (!stream.CanRead)
  {
    throw new ArgumentException ("The stream must be readable.");
  }
  if (stream.Length < SIGNATURE_SIZE)
  {
    throw new ArgumentException ("The stream is invalid.");
  }

  #region Get file signature

  var signature = new byte[SIGNATURE_SIZE];
  int bytesRequired = SIGNATURE_SIZE;
  int index = 0;
  stream.Seek (0, SeekOrigin.Begin);
  while (bytesRequired > 0)
  {
    int bytesRead = stream.Read (signature, index, bytesRequired);
    bytesRequired -= bytesRead;
    index += bytesRead;
  }
  string actualSignature = BitConverter.ToString (signature);

  #endregion

  InArchiveFormat suspectedFormat = InArchiveFormat.XZ; // any except PE and Cab
  isExecutable = false;

  foreach (string expectedSignature in Formats.InSignatureFormats.Keys)
  {
    if (actualSignature.StartsWith (expectedSignature, StringComparison.OrdinalIgnoreCase) ||
        actualSignature.Substring (6).StartsWith (expectedSignature, StringComparison.OrdinalIgnoreCase) &&
        Formats.InSignatureFormats[expectedSignature] == InArchiveFormat.Lzh)
    {
      if (Formats.InSignatureFormats[expectedSignature] == InArchiveFormat.PE)
      {
        suspectedFormat = InArchiveFormat.PE;
        isExecutable = true;
      }
      else
      {
        return Formats.InSignatureFormats[expectedSignature];
      }
    }
  }

  // Many Microsoft formats
  if (actualSignature.StartsWith ("D0-CF-11-E0-A1-B1-1A-E1", StringComparison.OrdinalIgnoreCase))
  {
    suspectedFormat = InArchiveFormat.Cab; // != InArchiveFormat.XZ
  }

  #region SpecialDetect
  try
  {
    SpecialDetect (stream, 257, InArchiveFormat.Tar);
  }
  catch (ArgumentException) { }
  if (SpecialDetect (stream, 0x8001, InArchiveFormat.Iso))
  {
    return InArchiveFormat.Iso;
  }
  if (SpecialDetect (stream, 0x8801, InArchiveFormat.Iso))
  {
    return InArchiveFormat.Iso;
  }
  if (SpecialDetect (stream, 0x9001, InArchiveFormat.Iso))
  {
    return InArchiveFormat.Iso;
  }
  if (SpecialDetect (stream, 0x9001, InArchiveFormat.Iso))
  {
    return InArchiveFormat.Iso;
  }
  if (SpecialDetect (stream, 0x400, InArchiveFormat.Hfs))
  {
    return InArchiveFormat.Hfs;
  }
  #region Last resort for tar - can mistake
  if (stream.Length >= 1024)
  {
    stream.Seek (-1024, SeekOrigin.End);
    byte[] buf = new byte[1024];
    stream.Read (buf, 0, 1024);
    bool istar = true;
    for (int i = 0; i < 1024; i++)
    {
      istar = istar && buf[i] == 0;
    }
    if (istar)
    {
      return InArchiveFormat.Tar;
    }
  }
  #endregion
  #endregion

  #region Check if it is an SFX archive or a file with an embedded archive.
  if (suspectedFormat != InArchiveFormat.XZ)
  {
    #region Get first Min(stream.Length, SFX_SCAN_LENGTH) bytes
    var scanLength = Math.Min (stream.Length, SFX_SCAN_LENGTH);
    signature = new byte[scanLength];
    bytesRequired = (int)scanLength;
    index = 0;
    stream.Seek (0, SeekOrigin.Begin);
    while (bytesRequired > 0)
    {
      int bytesRead = stream.Read (signature, index, bytesRequired);
      bytesRequired -= bytesRead;
      index += bytesRead;
    }
    actualSignature = BitConverter.ToString (signature);
    #endregion

    foreach (var format in new InArchiveFormat[]
    {
                    InArchiveFormat.Zip,
                    InArchiveFormat.SevenZip,
                    InArchiveFormat.Rar,
                    InArchiveFormat.Cab,
                    InArchiveFormat.Arj
    })
    {
      int pos = actualSignature.IndexOf (Formats.InSignatureFormatsReversed[format]);
      if (pos > -1)
      {
        offset = pos / 3;
        return format;
      }
    }
    // Nothing
    if (suspectedFormat == InArchiveFormat.PE)
    {
      return InArchiveFormat.PE;
    }
  }
  #endregion

  throw new ArgumentException ("The stream is invalid or no corresponding signature was found.");
}
public static InArchiveFormat CheckSignature (Stream stream, out int offset, out bool isExecutable)
{
  offset = 0;
  if (!stream.CanRead)
  {
    throw new ArgumentException ("The stream must be readable.");
  }
  if (stream.Length < SIGNATURE_SIZE)
  {
    throw new ArgumentException ("The stream is invalid.");
  }

  #region Get file signature

  var signature = new byte[SIGNATURE_SIZE];
  int bytesRequired = SIGNATURE_SIZE;
  int index = 0;
  stream.Seek (0, SeekOrigin.Begin);
  while (bytesRequired > 0)
  {
    int bytesRead = stream.Read (signature, index, bytesRequired);
    bytesRequired -= bytesRead;
    index += bytesRead;
  }
  string actualSignature = BitConverter.ToString (signature);

  #endregion Get file signature

  InArchiveFormat suspectedFormat = InArchiveFormat.XZ; // any except PE and Cab
  isExecutable = false;

  InArchiveFormat enDetectedFormat = (InArchiveFormat)(-1);
  InArchiveFormat enSpecialFormat = (InArchiveFormat)(-1);

  foreach (string expectedSignature in Formats.InSignatureFormats.Keys)
  {
    if (actualSignature.StartsWith (expectedSignature, StringComparison.OrdinalIgnoreCase) ||
        actualSignature.Substring (6).StartsWith (expectedSignature, StringComparison.OrdinalIgnoreCase) &&
        Formats.InSignatureFormats[expectedSignature] == InArchiveFormat.Lzh)
    {
      if (Formats.InSignatureFormats[expectedSignature] == InArchiveFormat.PE)
      {
        suspectedFormat = InArchiveFormat.PE;
        isExecutable = true;
      }
      else
      {
        enDetectedFormat = Formats.InSignatureFormats[expectedSignature];
        break;
      }
    }
  }

  // Many Microsoft formats
  if (actualSignature.StartsWith ("D0-CF-11-E0-A1-B1-1A-E1", StringComparison.OrdinalIgnoreCase))
  {
    suspectedFormat = InArchiveFormat.Cab; // != InArchiveFormat.XZ
  }

  #region SpecialDetect

  if (SpecialDetect (stream, 257, InArchiveFormat.Tar))
  {
    enSpecialFormat = InArchiveFormat.Tar;
  }
  else if (SpecialDetect (stream, 0x8001, InArchiveFormat.Iso))
  {
    enSpecialFormat = InArchiveFormat.Iso;
  }
  else if (SpecialDetect (stream, 0x8801, InArchiveFormat.Iso))
  {
    enSpecialFormat = InArchiveFormat.Iso;
  }
  else if (SpecialDetect (stream, 0x9001, InArchiveFormat.Iso))
  {
    enSpecialFormat = InArchiveFormat.Iso;
  }
  else if (SpecialDetect (stream, 0x9001, InArchiveFormat.Iso))
  {
    enSpecialFormat = InArchiveFormat.Iso;
  }
  else if (SpecialDetect (stream, 0x400, InArchiveFormat.Hfs))
  {
    enSpecialFormat = InArchiveFormat.Hfs;
  }

  #region Last resort for tar - can mistake

  bool bPossiblyTAR = false;
  if (stream.Length >= 1024)
  {
    stream.Seek (-1024, SeekOrigin.End);
    byte[] buf = new byte[1024];
    stream.Read (buf, 0, 1024);
    bPossiblyTAR = true;
    for (int i = 0; i < 1024; i++)
    {
      bPossiblyTAR = bPossiblyTAR && buf[i] == 0;
    }
  }

  // TAR header starts with the filename of the archive.
  // The filename can be anything, including the Identifiers of the various archive formats.
  // This means that a TAR can be misinterpreted as any type of archive.
  if (enSpecialFormat == InArchiveFormat.Tar
  || bPossiblyTAR)
  {
    var fs = stream as FileStream;
    if (fs != null)
    {
      string sStreamFilename = fs.Name;
      if (sStreamFilename.EndsWith (".tar", StringComparison.InvariantCultureIgnoreCase))
        enDetectedFormat = InArchiveFormat.Tar;
    }
  }

  #endregion Last resort for tar - can mistake

  if (enDetectedFormat != (InArchiveFormat)(-1))
    return enDetectedFormat;
  if (enSpecialFormat != (InArchiveFormat)(-1))
    return enSpecialFormat;

  #endregion SpecialDetect

  #region Check if it is an SFX archive or a file with an embedded archive.

  if (suspectedFormat != InArchiveFormat.XZ)
  {
    #region Get first Min(stream.Length, SFX_SCAN_LENGTH) bytes

    var scanLength = Math.Min (stream.Length, SFX_SCAN_LENGTH);
    signature = new byte[scanLength];
    bytesRequired = (int)scanLength;
    index = 0;
    stream.Seek (0, SeekOrigin.Begin);
    while (bytesRequired > 0)
    {
      int bytesRead = stream.Read (signature, index, bytesRequired);
      bytesRequired -= bytesRead;
      index += bytesRead;
    }
    actualSignature = BitConverter.ToString (signature);

    #endregion Get first Min(stream.Length, SFX_SCAN_LENGTH) bytes

    foreach (var format in new InArchiveFormat[]
    {
                InArchiveFormat.Zip,
                InArchiveFormat.SevenZip,
                InArchiveFormat.Rar,
                InArchiveFormat.Cab,
                InArchiveFormat.Arj
    })
    {
      int pos = actualSignature.IndexOf (Formats.InSignatureFormatsReversed[format]);
      if (pos > -1)
      {
        offset = pos / 3;
        return format;
      }
    }
    // Nothing
    if (suspectedFormat == InArchiveFormat.PE)
    {
      return InArchiveFormat.PE;
    }
  }

  #endregion Check if it is an SFX archive or a file with an embedded archive.

  throw new ArgumentException ("The stream is invalid or no corresponding signature was found.");
}
public static InArchiveFormat CheckSignature(流、out int offset、out bool可执行)
{
偏移量=0;
如果(!stream.CanRead)
{
抛出新ArgumentException(“流必须可读”);
}
if(流长度<签名大小)
{
抛出新ArgumentException(“流无效”);
}
#区域获取文件签名
var signature=新字节[signature_SIZE];
int bytesRequired=签名大小;
int指数=0;
stream.Seek(0,SeekOrigin.Begin);
while(需要字节数>0)
{
int bytesRead=stream.Read(需要签名、索引、字节);
bytesRequired-=bytesRead;
索引+=字节读取;
}
字符串actualSignature=BitConverter.ToString(签名);
#端区
InAchiveFormat suspectedFormat=InAchiveFormat.XZ;//除PE和Cab之外的任何
isExecutable=false;
foreach(格式为字符串expectedSignature.InSignatureFormats.Keys)
{
if(actualSignature.StartsWith(expectedSignature,StringComparison.OrdinalIgnoreCase)||
actualSignature.Substring(6).StartsWith(expectedSignature,StringComparison.OrdinalIgnoreCase)&&
Formats.InSignatureFormats[expectedSignature]==InArchiveFormat.Lzh)
{
if(Formats.InSignatureFormats[expectedSignature]==InArchiveFormat.PE)
{
suspectedFormat=InArchiveFormat.PE;
isExecutable=true;
}
其他的
{
返回格式。InSignatureFormats[expectedSignature];
}
}
}
//许多Microsoft格式
if(实际信号.StartsWith(“D0-CF-11-E0-A1-B1-1A-E1”,StringComparison.OrdinalIgnoreCase))
{
suspectedFormat=InArchiveFormat.Cab;/!=InArchiveFormat.XZ
}
#区域特殊检测
尝试
{
SpecialDetect(stream,257,InArchiveFormat.Tar);
}
捕获(参数异常){}
if(SpecialDetect(流,0x8001,InArchiveFormat.Iso))
{
返回InArchiveFormat.Iso;
}
if(SpecialDetect(流,0x8801,InArchiveFormat.Iso))
{
返回InArchiveFormat.Iso;
}
if(特殊检测(流,0x9001,InArchiveFormat.Iso))
{
返回InArchiveFormat.Iso;
}
if(特殊检测(流,0x9001,InArchiveFormat.Iso))
{
返回InArchiveFormat.Iso;
}
if(SpecialDetect(流,0x400,InArchiveFormat.Hfs))
{
返回InArchiveFormat.Hfs;
}
#区域焦油的最后手段-可能出错
如果(stream.Length>=1024)
{
Seek(-1024,SeekOrigin.End);
字节[]buf=新字节[1024];
读取(buf,0,1024);
bool-istar=true;
对于(int i=0;i<1024;i++)
{
istar=istar&&buf[i]==0;
}
如果(istar)
{
返回InArchiveFormat.Tar;
}
}
#端区
#端区
#区域检查它是SFX归档文件还是带有嵌入式归档文件的文件。
if(suspectedFormat!=InArchiveFormat.XZ)
{
#区域获取第一个最小(stream.Length,SFX\u SCAN\u Length)字节
var scanLength=Math.Min(stream.Length,SFX\u SCAN\u Length);
签名=新字节[扫描长度];
bytesRequired=(int)扫描长度;
指数=0;
stream.Seek(0,SeekOrigin.Begin);
while(需要字节数>0)
{
int bytesRead=stream.Read(需要签名、索引、字节);
bytesRequired-=bytesRead;
索引+=字节读取;
}
actualSignature=BitConverter.ToString(签名);
#端区
foreach(新InArchiveFormat[]中的var格式
{
InArchiveFormat.Zip,
InArchiveFormat.SevenZip,
InArchiveFormat.Rar,
InArchiveFormat.Cab,
InArchiveFormat.Arj
})
{
int pos=actualSignature.IndexOf(Formats.insignatureFormats与[format]相反);
如果(位置>-1)
{
偏移量=位置/3;
返回格式;
}
}
//没什么
if(suspectedFormat==InArchiveFormat.PE)
{
返回InArchiveFormat.PE;
}
}
#端区
抛出新ArgumentException(“流无效或没有对应项