简体   繁体   English

如何从备份文件解码MySql Blob?

[英]How to decode MySql blob from backup file?

I got a MySql backup file containing a column like: 我有一个MySql备份文件,其中包含如下列:

CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`profilepicture` blob,
)

The content of this row profilepicture is encoded in the backup file to some kind of dump, starting (in a text editor) like this: 此行概要文件图片的内容在备份文件中编码为某种转储,从(在文本编辑器中)开始,如下所示:

INSERT INTO `tbox_account_transaction` VALUES (8,'?\?\?0JFIF\0\0\0\0\0\0[...]');                  

How can I decode this from the .sql backup file in c#? 如何从c#中的.sql备份文件解码此内容? It is not Base64, but I do not recognize what else it might be. 它不是Base64,但我不知道它可能还有什么。 I don't want to restore the backup.sql file to MySql but instead decode the blob from the backup file. 我不想将backup.sql文件还原到MySql,而是从备份文件中解码Blob。

The bytes you are seeing are almost certainly the initial bytes of a JPEG image. 您看到的字节几乎可以肯定是JPEG图像的初始字节。 The readable string "JFIF" starting at the fifth byte is the clue (see https://en.wikipedia.org/wiki/JPEG_File_Interchange_Format ). 从第五个字节开始的可读字符串“ JFIF”是线索(请参阅https://en.wikipedia.org/wiki/JPEG_File_Interchange_Format )。

Many of the other binary bytes are null or else non-printable characters. 其他许多二进制字节为null或其他不可打印的字符。 All the weird \\? 所有的怪异\\? or \\0 sequences are the client's best effort at representing these bytes on your text display. \\0序列是客户尽最大努力在文本显示中表示这些字节。

The easiest way to access this data is to restore the dump file to a MySQL instance (even a local one running on a laptop), and then use SQL in your C# code to access the binary contents of the profilepicture blob. 访问此数据的最简单方法是将转储文件还原到MySQL实例(甚至是笔记本电脑上运行的本地实例),然后在C#代码中使用SQL来访问profilepicture blob的二进制内容。

Either save the data from one blob to a .jpg file as @Tommaso Belluzzo suggests, or else just display it directly, if you can. 按照@Tommaso Belluzzo的建议将数据从一个Blob保存到.jpg文件,或者,如果可以的话,直接将其显示。 I'm not a C# programmer, but it seems there's a Bitmap class for this (see https://msdn.microsoft.com/en-us/library/8tda2c3c(v=vs.85).aspx ). 我不是C#程序员,但是似乎有一个Bitmap类(请参阅https://msdn.microsoft.com/zh-cn/library/8tda2c3c(v=vs.85).aspx )。

It's outside the scope of a Stack Overflow answer to give a tutorial on how to write SQL in a C# application. 提供有关如何在C#应用程序中编写SQL的教程超出了Stack Overflow答案的范围。 There are plenty of resources available for that. 有很多可用的资源。

That looks like a jpg file header. 看起来像是jpg文件标题。 Try the following ( blob is a String typed variable containing the profilepicture ): 尝试以下操作( blob是一个包含profilepictureString变量):

using (MemoryStream stream = new MemoryStream(buffer))
{
    Image image = Image.FromStream(stream, true, true);
    image.Save("C:\\Test.jpg", ImageFormat.Jpeg);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM