[C#]擷取Picasa資料庫(_.PMP)內現有的資料
var type = br.ReadInt16();
if (0x1332 != br.ReadInt16())
{
throw new Exception("Incorrect format");
}
if (0x00000002 != br.ReadInt32())
{
throw new Exception("Incorrect format");
}
if (type != br.ReadInt16())
{
throw new Exception("Incorrect format");
}
if (0x1332 != br.ReadInt16())
{
throw new Exception("Incorrect format");
}
var number = br.ReadInt32();
switch (type)
{
case 0x00:
DumpStringField(br, number);
break;
case 0x01:
Dump4ByteField(br, number);
break;
case 0x02:
DumpDateField(br, number);
break;
case 0x03:
DumpByteField(br, number);
break;
case 0x04:
Dump8ByteField(br, number);
break;
case 0x05:
Dump2ByteField(br, number);
break;
case 0x06:
DumpStringField(br, number);
break;
case 0x07:
Dump4ByteField(br, number);
break;
default:
throw new Exception("Incorrect format");
}
}
}
...</pre></div>
namespace WindowsFormsApplication28 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void btnLoad_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() != DialogResult.OK)
return;
textBox1.Text = string.Empty;
var file = openFileDialog1.FileName;
using (var fs = File.OpenRead(file))
{
using (var br = new BinaryReader(fs))
{
var magic = br.ReadBytes(4);
if(magic[0] != 0xcd ||
magic[1] != 0xcc ||
magic[2] != 0xcc ||
magic[3] != 0x3f)
{
throw new Exception("Incorrect format");
}
var type = br.ReadInt16();
if (0x1332 != br.ReadInt16())
{
throw new Exception("Incorrect format");
}
if (0x00000002 != br.ReadInt32())
{
throw new Exception("Incorrect format");
}
if (type != br.ReadInt16())
{
throw new Exception("Incorrect format");
}
if (0x1332 != br.ReadInt16())
{
throw new Exception("Incorrect format");
}
var number = br.ReadInt32();
switch (type)
{
case 0x00:
DumpStringField(br, number);
break;
case 0x01:
Dump4ByteField(br, number);
break;
case 0x02:
DumpDateField(br, number);
break;
case 0x03:
DumpByteField(br, number);
break;
case 0x04:
Dump8ByteField(br, number);
break;
case 0x05:
Dump2ByteField(br, number);
break;
case 0x06:
DumpStringField(br, number);
break;
case 0x07:
Dump4ByteField(br, number);
break;
default:
throw new Exception("Incorrect format");
}
}
}
}
private void DumpStringField(BinaryReader br, int number)
{
var sb = new StringBuilder(1024);
for (long i = 0; i < number; i++)
{
var value = getString(br);//br.ReadString();
sb.AppendFormat("[{0}] {1}", i, value);
sb.Append(Environment.NewLine);
}
textBox1.Text = sb.ToString();
}
private String getString(BinaryReader br)
{
var sb = new StringBuilder();
int c;
while((c = br.Read()) != 0) {
sb.Append((char)c);
}
return sb.ToString();
}
private void DumpDateField(BinaryReader br, int number)
{
var sb = new StringBuilder();
int[] bytes = new int[8];
for (long idx = 0; idx < number; idx++)
{
long ld = 0;
for (int i = 0; i < 8; i++)
{
bytes[i] = br.ReadByte();
var temp = bytes[i];
temp <<= (8 * i);
ld += temp;
}
sb.Append("[" + idx + "] ");
double d = BitConverter.Int64BitsToDouble(ld);
d -= 25569;
long ut = (long)Math.Round(d * 864001 * 10001);
sb.Append(DateTime.FromFileTime(ut));
}
textBox1.Text = sb.ToString();
}
private void DumpByteField(BinaryReader br, int number)
{
var sb = new StringBuilder(1024);
for (long i = 0; i < number; i++)
{
var value = br.ReadByte();
sb.AppendFormat("[{0}] {1}", i, value);
sb.Append(Environment.NewLine);
}
textBox1.Text = sb.ToString();
}
private void Dump2ByteField(BinaryReader br, int number)
{
var sb = new StringBuilder(1024);
for (long i = 0; i < number; i++)
{
var value = br.ReadInt16();
sb.AppendFormat("[{0}] {1}", i, value);
sb.Append(Environment.NewLine);
}
textBox1.Text = sb.ToString();
}
private void Dump4ByteField(BinaryReader br, int number)
{
var sb = new StringBuilder(1024);
for (long i = 0; i < number; i++)
{
var value = br.ReadInt32();
sb.AppendFormat("[{0}] {1}", i, value);
sb.Append(Environment.NewLine);
}
textBox1.Text = sb.ToString();
}
private void Dump8ByteField(BinaryReader br, int number)
{
var sb = new StringBuilder();
int[] bytes = new int[8];
for (long idx = 0; idx < number; idx++)
{
for (int i = 0; i < 8; i++)
{
bytes[i] = br.ReadByte();
}
sb.Append("[" + idx + "] ");
for (int i = 7; i >= 0; i--)
{
String x = Convert.ToString(bytes[i], 16);
if (x.Length == 1)
{
sb.Append("0");
}
sb.Append(x);
}
sb.AppendLine();
}
textBox1.Text = sb.ToString();
}
}
}