nodejs实现电脑版微信dat文件批量转化为jpg图片

微信聊天的图片都是以.dat格式保存下载的,搜了下网上的资料,只找到了python代码的实现,
这里用nodejs实现了一下。

新版电脑微信存储图片位置:
.\FileStorage\MsgAttach\d793770e32c06bd55098717dff82c5a6\Image

旧版电脑微信存储图片位置:
.\FileStorage\Image\

Nodejs实现批量转换:

const fs = require('fs')
const path = require('path')

const base = 0xFF
const next = 0xD8
const gifA = 0x47
const gifB = 0x49
const pngA = 0x89
const pngB = 0x50

//扫描路劲
const filePath = 'E:/Image/2022-11'

//转化后输出图片的路径
const outPath = 'E:/vxphoto/'

//读取目录下所有.dat文件
const files = fs.readdirSync(filePath)
const datArry = []
files.forEach(function (item) {
  if (path.extname(item) == '.dat') {
    datArry.push(item);
  }
})

// 将所有.dat文件依次转化为.jpg文件,输出路径为上面的outPath 
for (let i = 0; i < datArry.length; i++) {
  convert(datArry[i])
  process.stdout.write(`${i + 1}/${datArry.length} \r`)
}

//.dat转化为.jpg图片
function convert (item) {
  let absPath = path.join(filePath, item);
  let imgPath = path.join(outPath, item + '.jpg');
  fs.readFile(absPath, (_err, content) => {
    if (content?.length) {
      let firstV = content[0]
      let nextV = content[1]
      let jT = firstV ^ base
      let jB = nextV ^ next
      let gT = firstV ^ gifA
      let gB = nextV ^ gifB
      let pT = firstV ^ pngA
      let pB = nextV ^ pngB
      let v = firstV ^ base
      if (jT == jB) {
        v = jT
      } else if (gT == gB) {
        v = gT
      } else if (pT == pB) {
        v = pT
      }

      let bb = content.map(br => {
        return br ^ v
      })
      fs.writeFileSync(imgPath, bb)
    }
  })
}

你可能感兴趣的:(算法,微信图片,dat转jpg)