【问题标题】:How to detect and separate concatenated files?如何检测和分离级联文件?
【发布时间】:2013-12-24 04:38:17
【问题描述】:

我正在尝试找到一种方法来分隔已连接在一起的两个文件 copy /b file1+file2 file3.

我知道这两个文件中至少一个的 mime 类型和文件类型。

【问题讨论】:

  • 我觉得找备份恢复原文件比较容易...
  • 假设已知文件类型是第一个文件。您如何设想从那里开始?根据您问题中的信息,您没有足够的信息来可靠地执行该任务。
  • 我没有原始文件。因此问题。第一个文件是 .exe,第二个是 .zip

标签: file concatenation cat


【解决方案1】:

使用以下 csharp 代码,您可以根据 zip 文件具有 4 bytes that indicates the local file header 的签名这一事实进行拆分。如果 EXE 在某些地方具有相同的 4 个字节,则此代码将中断。如果你想征服那你必须挖掘PE/COFF header to add up all section sizes

不,copy a stream byte by byte 效率不是很高...

using(var fs = new FileStream(@"exeandzip.screwed", FileMode.Open))
{
    var lfh = new byte[] { 0x50, 0x4b, 0x03, 0x04 }; /* zip local file header signature */
    var match = 0;
    var splitAt = 0;
    var keep = new Queue<int>();
    var b = fs.ReadByte();  
    using(var exe = new FileStream(
                         @"exeandzip.screwed.exe", 
                         FileMode.Create))
    {
        while((b != -1) && (match<lfh.Length))
        {   splitAt++;

            if (b==lfh[match]) 
            {
                match++; 
                keep.Enqueue(b);
            }
            else 
            {
                while(keep.Count>0)
                {
                    exe.WriteByte((byte) keep.Dequeue());
                }
                exe.WriteByte((byte)b);
                match=0;
            }
            b = fs.ReadByte();
        }
    }

    if (match==lfh.Length && b!=-1)
    {
        keep.Enqueue(b);
        splitAt = splitAt-lfh.Length;
        Console.WriteLine(splitAt);
        using(var zip = new FileStream(
                                   @"exeandzip.screwed.zip", 
                                   FileMode.Create))
        { 
            while(keep.Count>0)
            {
                zip.WriteByte((byte) keep.Dequeue());
            }
            b = fs.ReadByte();  
            while(b != -1)
            {  
                zip.WriteByte((byte)b);
                b = fs.ReadByte();
            }
        }
    }   
}

【讨论】:

    【解决方案2】:

    或者你可以使用foremost -i &lt;input file&gt; -o &lt;output directory&gt;

    我什至用这种方式拆分了apple webarchive格式文件

    【讨论】:

      猜你喜欢
      • 2011-05-25
      • 2016-11-21
      • 2015-04-21
      • 2019-04-15
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多