0

I have a SQL Server 2008 database from an application which stores office file templates.

The files in the database are stored in hex format (0x504B030414000600...).

With a file signature table (https://www.garykessler.net/library/file_sigs.html), I can find out which file format it is: Microsoft Office Open XML Format Documents (OOXML, like DOCX, PPTX, XLSX ...).

How can I export/convert these hex strings into the original files? Maybe with C# ...

With the application itself, I can only export 1 file at a time. It would take days to do this with all files (about 1000).

Thank you

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
3x3cut0r
  • 1
  • 1
  • Preumably the data is in a varbinary column? You can just stream the data out to a file eg using Binarywriter class, or directly from SQL Server using adodb.stream – Stu Jul 26 '22 at 14:14
  • 2
    Hex is not a string!!! Check database and verify the storage type for the column. – jdweng Jul 26 '22 at 14:23
  • If it's `varbinary` then it's not hex, that's just how the viewer is showing it. You can export it as a `byte` array in most languages, and dump that into a file – Charlieface Jul 26 '22 at 14:38
  • If it is stored in a `varchar` field as a hex string (who knows - I'm sure it's been done), then you are going to want to look at: https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa. You say _"With the application itself"_ - what application? Is it scriptable (in a batch file, PowerShell, whatever)? – Flydog57 Jul 26 '22 at 15:49
  • The column is named "BinaryContent (image, null)" ... so it is interestingly in image format. the application is a website. – 3x3cut0r Jul 27 '22 at 06:32

1 Answers1

0
  1. Export column with files from SQL Server to single file (it may be very large, but it shouldn't matter). You can for example right click and export results to CSV file.
  2. Write simple C# console application:
  • use CSV parser to loop over data
  • inside loop you can make simple if statements to determine document file format
  • in every iteration convert hex format to blob file and save it somewhere on your drive
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Patryk
  • 51
  • 4
  • I have exported the table into a csv with the data in it ... but now i am confused what date i have here? is 0x504... a string? binary data? byte array? hex? whats the difference? and in which format do i need to convert that string to write it to a file? – 3x3cut0r Jul 27 '22 at 07:48