0

I am trying to find a method to separate two files that have been concatenated together using copy /b file1+file2 file3.

I know the mime type and file type of at least one of the two files.

Gerbal
  • 661
  • 6
  • 8
  • I think it is easier to find the backup and restore the original files... – rene Dec 06 '13 at 18:02
  • Suppose the known filetype is the first file. How do you envision to go from there? Based on the info in your question you have not enough information to reliably perform that task. – rene Dec 06 '13 at 18:06
  • I don't have the original files. Hence the question. The first file is an .exe, the second is a .zip – Gerbal Dec 06 '13 at 22:12

2 Answers2

1

With the following csharp code you can do the split based on the fact that the zip file has the signature of 4 bytes that indicates the local file header. This code will break if the EXE has the same 4 bytes some where. If you want to conquer that you have to dig through the PE/COFF header to add up all section sizes

And NO, it is not very efficient to 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();
            }
        }
    }   
}
rene
  • 41,474
  • 78
  • 114
  • 152
0

Or u can use foremost -i <input file> -o <output directory>

I've even split the apple webarchive format file in this way