React-Native中如何用zlib解码FlateDecoded PDF流?
重写PDF时FlateDecoded数据zlib解压报"incorrect header check"错误求助
我在重写PDF文件时遇到zlib相关问题:尝试将FlateDecoded数据转换回原始数据时,使用zlib.inflate函数持续出现incorrect header check错误。
我的最终目标是解密FlateDecoded数据流,已经尝试过以下方案但均未解决问题:
- 使用react-native-gzip的
{deflate, inflate} - 使用pako库
- 使用react-zlib-js的zlib
- 将数据转换为UInt8Array后解压
- 将数据转换为十六进制后解压
- 在Swift中尝试同样的操作,得到相同错误
以下是当前的代码片段:
import RNFS from 'react-native-fs'; import RNFetchBlob from 'rn-fetch-blob'; import { PDFDocument, } from 'pdf-lib'; import base64 from 'react-native-base64'; var Buffer = require('buffer').Buffer; import zlib from 'react-zlib-js'; var textEncoding = require('text-encoding'); export function rewritePDF(path) { return new Promise((resolve, reject) => { RNFS.readFile(path.replace('%20', ' '), 'base64').then(data => { const myNewPDFData = base64.decode(data); console.log(data); console.log(myNewPDFData); const pdfObjects = myNewPDFData.split(/(?<=\bobj\b)|(?=\bendobj\b)/g); console.log(pdfObjects); const flateDecodedData = pdfObjects[21]; console.log(flateDecodedData); const pureStream = flateDecodedData.split( /(?<=\bstream\b)|(?=\bendstream\b)/g, )[1]; let input = new Buffer(pureStream); console.log(input); console.log(pureStream); // zlib.inflate(pureStream, function (r, e) { // if (e) { // console.log(e); // return; // } // console.log(r); // }); // const hexPureStream = Buffer.from(pureStream, 'utf8').toString('hex'); // console.log(hexPureStream.slice(2, hexPureStream.length - 2)); // zlib.inflate( // hexPureStream.slice(2, hexPureStream.length - 2), // function (r, e) { // if (e) { // console.log(e); // return; // } // console.log(r); // }, // ), // zlib.deflate('test', function (e, r) { // if (e) { // console.log(e); // return; // } // console.log(new TextDecoder().decode(r)); // zlib.inflate(r, function (err, res) { // if (err) { // console.log(e); // return; // } // console.log(res); // console.log(new TextDecoder().decode(res)); // }); // }); let newData = base64.encode(myNewPDFData); PDFDocument.load(newData).then(async pdfDoc => { const pdfBytes = await pdfDoc.saveAsBase64(); RNFetchBlob.fs .writeFile( path.replace('file://', '').replace('%20', ' '), pdfBytes, 'base64', ) .then(() => resolve(path)) .catch(error => { console.log(error); }); }); }); }); }
我已经查阅过关于zlib头格式、zlib解压失败以及FlateDecoded数据解码的相关资料,恳请各位提供指导、见解或替代方案,帮助我成功解密该数据流。
内容的提问来源于stack exchange,提问作者Knwsrw
相关产品推荐
相关产品推荐

