最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if file is corrupted with node.js - Stack Overflow

programmeradmin1浏览0评论

There are some ways to check file is corrupted with node.js?

I tried many File System methods, like fs.readFile, fs.open and fs.access but all of them return ok status, and I'm sure my file is corrupted in my tests.

To be more clear, my objective is to check if PDF is readable (not only check if can be generated) and if can be opened. I damaged the file online to test.

There are some ways to check file is corrupted with node.js?

I tried many File System methods, like fs.readFile, fs.open and fs.access but all of them return ok status, and I'm sure my file is corrupted in my tests.

To be more clear, my objective is to check if PDF is readable (not only check if can be generated) and if can be opened. I damaged the file online to test.

Share edited Apr 10, 2023 at 13:48 ark 8483 gold badges10 silver badges32 bronze badges asked May 25, 2017 at 14:40 BrTkCaBrTkCa 4,7834 gold badges27 silver badges45 bronze badges 2
  • 3 First define "corrupted". "Doesn't contain what my brain things it should contain" isn't something a puter can test for. – Quentin Commented May 25, 2017 at 14:41
  • I updated the question @Quentin – BrTkCa Commented May 25, 2017 at 14:46
Add a ment  | 

2 Answers 2

Reset to default 4

You could try to parse it with a tool like this and confirm if it was successful.

To expand on that a little, here's some example code lifted from the link:

let fs = require('fs'),
    PDFParser = require("pdf2json");

let pdfParser = new PDFParser();

pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError) );
pdfParser.on("pdfParser_dataReady", pdfData => {
    fs.writeFile("./pdf2json/test/F1040EZ.json", JSON.stringify(pdfData));
});

pdfParser.loadPDF("./pdf2json/test/pdf/fd/form/F1040EZ.pdf");

Actually, u can use another npm to check file corruption of pdf.

npm i pdf-parse
const pdfParser = require('pdf-parse')
try {
  let bufferData = fs.readFileSync(`${target}/corrupted.pdf`)
  pdfParser(bufferData).then((data) => {
    // do something with data
  }).catch((error) => {
    console.log(error.message)
  })
} catch (error) {
  console.log(error)
}

For corrupted file the error might seem like:
Warning: Indexing all PDF objects
Invalid PDF structure

发布评论

评论列表(0)

  1. 暂无评论